製作一個程序,允許用戶輸入輸入,以便他們可以獲得簡單的數學科目的幫助。我以爲我已經做到了,所以如果他們進入了一個不在那裏的話題,那麼下面的else語句就不會看到這個話題。任何想法爲什麼當我運行它時,它仍然包括else語句當輸入主題是正確的? http://prntscr.com/ey3qyhC++忽略其他語句
search = "triangle";
pos = sentence.find(search);
if (pos != string::npos)
// source http://www.mathopenref.com/triangle.html
cout << "A closed figure consisting of three line segments linked end-to-end. A 3 - sided polygon." << endl;
search = "square";
pos = sentence.find(search);
if (pos != string::npos)
// source http://www.mathopenref.com/square.html
cout << "A 4-sided regular polygon with all sides equal and all internal angles 90°" << endl;
else
cout << "Sorry, it seems I have no information on this topic." << endl;
case 2:
break;
default:
cout << "Invalid input" << endl;
}
將'search =「square」;'以及之後的所有內容放到'case 2:'中放入else語句中。然後,如果找不到三角形,它只會查找一個正方形。 –
有比正方形或三角形更多的選項,我只是不想包含太多的代碼。我會在每個搜索如三角形和正方形之後放入else語句,並且如上所述進行操作嗎? –
如果您可以更改它,以便if中的所有代碼位於if語句中(例如'search =「square」; pos = sentence.find(search); if(pos!= string :: npos)'into 'if(sentence.find(「square」)!= string :: npos)'),那麼你可以將第一個if語句之後的所有if語句改成else if語句。如果不是那麼每個如果將不得不在其他內部。我會寫一個答案 –