2017-03-06 54 views
0
Console.Clear(); 
string choice; 

Console.WriteLine("Welcome to Costa coffee\n"); 
Console.WriteLine("1:> Latte\n2:> Cappuccino\n3:> Espresso\n4:> Double espresso"); 
Console.WriteLine("\nPlease select a coffee by pressing 1-4"); 
choice = Console.ReadLine(); 


if (choice == "1") 
{ 
    Console.WriteLine("\nYou have selected: latte"); 
} 
if (choice == "2") 
{ 
    Console.WriteLine("\nYou have selected: Cappuccino"); 
} 
if (choice == "3") 
{ 
    Console.WriteLine("\nYou have selected: Espresso"); 
} 
if (choice == "4") 
{ 
    Console.WriteLine("\nYou have selected: Double espresso"); 
} 

else if (choice !="1" || choice !="2" || choice !="3" || choice !="4") 
{ 
    Console.WriteLine("Incorrect value, please try again"); 
} 

我試圖讓程序,如果選擇不等於1,2,3,4那麼它會顯示「不正確的值,請再試一次」然而,當我按下任何東西時仍然有效,但在按1,2,3或4時仍然顯示此錯誤消息。任何想法?C#if語句!=多個值的

+0

它贏得了我做了嗎?如果你選擇了4,它會* *做到這一點,如果你選擇1,2或3.提示:你認爲「其他」是一種替代?如果哪個條件不符合,會發生什麼? (我建議使用switch語句,而不是,請介意你...) –

+0

只是最後一個別的別人{你的東西}和鏈別的ifs。 – TheNoob

+0

''選擇!=「1」&&選擇!=「2」&...' – Rob

回答

0

您的前三條if語句與最後兩條語句無關。您應該在if for choice == 2choice == 3之前添加else。這樣,這一切都成爲一個大的if/else語句。

1

中平凡調整你的代碼(但有更好的方法來寫):

Console.Clear(); 
string choice; 

Console.WriteLine("Welcome to Costa coffee\n"); 
Console.WriteLine("1:> Latte\n2:> Cappuccino\n3:> Espresso\n4:> Double espresso"); 
Console.WriteLine("\nPlease select a coffee by pressing 1-4"); 
choice = Console.ReadLine(); 


if (choice == "1") 
{ 
    Console.WriteLine("\nYou have selected: latte"); 
} 
else if (choice == "2") 
{ 
    Console.WriteLine("\nYou have selected: Cappuccino"); 
} 
else if (choice == "3") 
{ 
    Console.WriteLine("\nYou have selected: Espresso"); 
} 
else if (choice == "4") 
{ 
    Console.WriteLine("\nYou have selected: Double espresso"); 
} 
else 
{ 
    Console.WriteLine("Incorrect value, please try again"); 
} 
2

推薦switch在這種情況下:

var myConsoleString = ""; 

switch (choice) 
{ 
    case "1": myConsoleString = "\nYou have selected: latte"; break; 
    case "2": myConsoleString = "\nYou have selected: Cappuccino"; break; 
    case "3": myConsoleString = "\nYou have selected: Espresso"; break; 
    case "4": myConsoleString = "\nYou have selected: Double espresso"; break; 
    default: myConsoleString = "\nIncorrect value, please try again"; break; 
} 

Console.WriteLine(myConsoleString); 
+1

對於整數你不需要括號,但除此之外你打敗了我。還有很好的選擇,不要在交換機內使用Console.WriteLine – Edward

+0

我相信原來的海報正在用字符串,但我同意。所以它是爲他量身定製的(或她!):) – HouseCat

+0

'int choice = Convert.ToInt32(Console.ReadLine());'雖然需要錯誤檢查。 – Edward

0

幹版:

var coffee = null; 

switch (choice) 
{ 
    case "1": coffee = "Latte"; break; 
    case "2": coffee = "Cappuccino"; break; 
    case "3": coffee = "Espresso"; break; 
    case "4": coffee = "Double espresso"; break; 
} 

Console.WriteLine(coffee == null ? "Incorrect value, please try again" : $"You have selected: {coffee}"); 
0

在你的代碼有兩件事是錯誤的:

  1. else if塊只在choice == "4"失敗時執行,如果所有條件都失敗,您希望它運行。
  2. 條件本身不檢查你真的想檢查什麼,你想顯示錯誤消息,如果選擇不是1 沒有2 沒有3 沒有4.但是,您正在使用運算符。所以你可以通過使用AND來解決這個問題,但是它不能解決整個問題,反正它不是一個好的解決方案。

你需要的是一個如果 - 否則,如果 - 其他結構,而不是連續如果的。

if (choice == "1") 
{ 
    Console.WriteLine("\nYou have selected: latte"); 
} 
else if (choice == "2") 
{ 
    Console.WriteLine("\nYou have selected: Cappuccino"); 
} 
else if (choice == "3") 
{ 
    Console.WriteLine("\nYou have selected: Espresso"); 
} 
else if (choice == "4") 
{ 
    Console.WriteLine("\nYou have selected: Double espresso"); 
} 
else 
{ 
    Console.WriteLine("Incorrect value, please try again"); 
} 

這比if的鏈更好,因爲其他條件只會在條件失敗之前被評估。例如,如果choice == "2"如果choice == "1"爲真沒有意義。如果所有的條件都失敗了,那麼else塊將被執行,這是你首先想要的。請注意,沒有必要編寫一個與所有條件相反的條件,因此有一個else語句用於此目的。

0

我討厭重複我自己,所以我會以減少重複的方式來解決這個問題。

通過使有效選項的字典,生成消息的邏輯變得更清晰,新方案的加入是其選擇作出決定的邏輯不太交織:

var possibleChoices = new Dictionary<string,string>{ 
    {"1", "Latte"}, 
    {"2", "Cappuccino"}, 
    {"3", "Espresso"}, 
    {"4", "Double espresso"} 
}; 

string value; 

var message = possibleChoices.TryGetValue(choice, out value) 
    ? string.Format("You have selected: {0}", value) 
    : "Incorrect value, please try again"; 

//In your code, the fall-through case misses the leading \n . 
//By avoiding repetition, consistency in formatting is achieved 
//with a single line of code 
Console.WriteLine("\n{0}", message); 
0

你可以使用字典讓事實「1」是「拿鐵」和其他值更具體,這將允許您使用.Contains()方法,以替代多個if的或switch聲明:

Console.Clear(); 
string choice; 
Dictionary<string, string> choiceToCoffee = new Dictionary<string, string>() 
{ 
    { "1", "Latte" }, 
    { "2", "Cappuccino" }, 
    { "3", "Espresso" }, 
    { "4", "Double Espresso" }, 
}; 
Console.WriteLine("Welcome to Costa coffee\n"); 
foreach (var kvp in choiceToCoffee) 
{ 
    var thisChoice = kvp.Key; 
    var thisCoffee = kvp.Value; 
    Console.WriteLine(thisChoice + ":> " + thisCoffee); 
} 
Console.WriteLine("\nPlease select a coffee by pressing 1-4"); 
choice = Console.ReadLine(); 

if (choiceToCoffee.ContainsKey(choice)) 
{ 
    Console.WriteLine("\nYou have selected: " + choiceToCoffee[choice]); 
} 
else 
{ 
    Console.WriteLine("Incorrect value, please try again"); 
}