我做一個配方分配程序,將根據打印的食譜更高效:使代碼通過減少if語句
- 食品
- 需要煮的時間的香料水平
我可能會在路上追加更多。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input = "";
string recipe1 = "A mild recipe that takes 10 mins";
string recipe2 = "A mild recipe that takes 20 mins";
string recipe3 = "A medium recipe that takes 10 mins";
string recipe4 = "a mild recipe that takes 20 mins";
cout << "Hello to the recipe dispenser 2000" << endl << "I will now begin with some questions to get the perfect recipe for you" << endl << "Do you like your food mild, medium, or hot?" << endl;
getline (cin, input);
if(input == "mild")
cout << "Would you like a recipe that takes 10 or 20 mins?" << endl;
getline (cin, input);
if(input == "10" or input == "10 mins")
cout << recipe1 << endl;
}
但是我現在的代碼看起來效率很低,因爲我必須寫出總共6個if語句才能完成代碼。
有什麼辦法可以縮短這個嗎?
例如,通過爲每個配方添加一些標籤或其他東西,如[10, mild]
至recipe1
,則代碼將根據標籤輸出響應。
任何想法表示讚賞。
這是否編譯? –
您的代碼首先不是正確的。帶'or'關鍵字的if語句總是會計算爲true,因爲它是'(input ==「10」)or(「10 mins」)',你的意思是'(input ==「10」or input == 「10分鐘」)' –
'#define或||'或者可能使用具有特定擴展名的編譯器? – JVApen