C#if statement!= to multiple value' s

时间:2017-03-06 22:49:38

标签: c# if-statement statements

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时仍然显示此错误信息。任何想法?

7 个答案:

答案 0 :(得分: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 :(得分: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 :(得分:0)

你的前三个if语句独立于最后两个。您应该在choice == 2choice == 3的if之前添加其他内容。这样一切都变成了一个大的if / else语句。

答案 3 :(得分:0)

DRY版本:

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}");

答案 4 :(得分:0)

在您的代码中有两个错误:

  1. else if块仅在choice == "4"失败时执行,如果所有条件都失败,则希望它运行。
  2. 条件本身并不检查您确实要检查的内容,如果选项不是1 不是2 且不是3 不是4.但您使用的是 OR 运算符。所以你可以通过使用AND来解决这个问题,但它不会解决整个问题,反正它也不会是一个好的解决方案。
  3. 您需要的是 if - else if else - else 结构而不是连续如果&#39 ; S

    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 of chain更好,因为只有在条件失败之前,才会评估其他条件。例如,如果choice == "2" 为真,则无需检查是choice == "1"。如果所有条件都失败,那么将执行else块,这是你想要的第一个地方。请注意,不需要编写与所有条件相反的条件,为此目的有一个else语句。

答案 5 :(得分:0)

我讨厌重复自己,所以我会接近这个以减少重复。

通过制作有效选择的字典,生成消息的逻辑变得更加清晰,新选项的添加与决定选择的逻辑交织得不那么简单:

def balanced_partition(lst):
    idx = 0
    S1 = 0
    S2 = 0
    result_partition=[None]*len(lst)
    while idx < len(lst):
        new_S1 = S1 + lst[idx]
        new_S2 = S2 + lst[idx]
        if abs(new_S1 - S2) < abs(new_S2 - S1):
            result_partition[idx] = 1
            S1 = new_S1
        else:
            result_partition[idx] = 2
            S2 = new_S2
        idx += 1
    print("final sums s1 = {S1} and s2 = {S2} ".format(S1=S1, S2=S2))
    return result_partition

答案 6 :(得分:0)

您可以使用字典来表示“1”是“拿铁”而其他值更具体,这样您就可以使用.Contains()方法代替多个ifswitch陈述:

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");
}