我编写了一个控制台应用程序,可在用户输入数字时进行打印。当用户按1时,控制台将打印相应if
语句中指定的文本。我该如何工作?
int keypress; // Variable to hold number
ConsoleKeyInfo UserInput = Console.ReadKey(); // Get user input
// We check input for a Digit
if (char.IsDigit(UserInput.KeyChar))
{
keypress = int.Parse(UserInput.KeyChar.ToString()); // use Parse if it's a Digit
if (keypress = 1)
{
Console.WriteLine("6$ Cheese added to your cart");
}
else (keypress = 2){
Console.WriteLine("2$ Bread added to your cart");
}
else (keypress = 3){
Console.WriteLine("1$ cookie added to your cart");
}
}
else
{
keypress = -1; // Else we assign a default value
Console.WriteLine("Number you entered isn't in the grocery list, please retry");
}
答案 0 :(得分:8)
您使用赋值运算符=
代替==
。因此,在if条件中使用=
更改==
。
此外,您需要在if
之后添加新的else
,因为您有新的情况,
你的代码看起来像这样:
int keypress; // Variable to hold number
ConsoleKeyInfo UserInput = Console.ReadKey(); // Get user input
// We check input for a Digit
if (char.IsDigit(UserInput.KeyChar))
{
keypress = int.Parse(UserInput.KeyChar.ToString()); // use Parse if it's a Digit
if (keypress == 1)
{
Console.WriteLine("6$ Cheese added to your cart");
}
else if (keypress == 2)
{
Console.WriteLine("2$ Bread added to your cart");
}
else if (keypress == 3)
{
Console.WriteLine("1$ cookie added to your cart");
}
}
else
{
keypress = -1; // Else we assign a default value
Console.WriteLine("Number you entered isn't in the grocery list, please retry");
}
您也可以使用switch
来实现相同目标。
if (char.IsDigit(UserInput.KeyChar))
{
keypress = int.Parse(UserInput.KeyChar.ToString()); // use Parse if it's a Digit
switch (keypress)
{
case 1:
Console.WriteLine("6$ Cheese added to your cart");
break;
case 2:
Console.WriteLine("2$ Bread added to your cart");
break;
case 3:
Console.WriteLine("1$ cookie added to your cart");
break;
default:
break;
}
}
else
{
keypress = -1; // Else we assign a default value
Console.WriteLine("Number you entered isn't in the grocery list, please retry");
}
答案 1 :(得分:2)
而不是" IF"使用" elseIF",使用" if(keypress == 1)"而不是" if(keypress = 1)"但更好的方法是切换案例"看下面的例子:
ConsoleKeyInfo UserInput = Console.ReadKey(); // Get user input
// We check input for a Digit
if (char.IsDigit(UserInput.KeyChar))
{
keypress = int.Parse(UserInput.KeyChar.ToString()); // use Parse if it's a Digit
switch(keypress)
{
case = 1:
Console.WriteLine("6$ Cheese added to your cart");
break;
case = 2:
Console.WriteLine("2$ Bread added to your cart");
break;
case = 3:
break;
Console.WriteLine("1$ cookie added to your cart");
default:
keypress = -1; // Else we assign a default value
Console.WriteLine("Number you entered isn't in the grocery list, please retry");
break;
}
}
答案 2 :(得分:1)
=
是一个赋值运算符。
==
是一个相等运算符。
使用条件语句时需要使用==
。
答案 3 :(得分:1)
您正在使用赋值运算符'='而不是等于运算符'=='。
keypress = 1;
以上将尝试将keypress指定为值1。
keypress == 1;
以上代码将评估keypress是否等于1.
答案 4 :(得分:0)
这里有一些事情:
// As others have indicated, this should be "=="
// Or, even better, convert this whole thing to a "switch" statement
if (keypress = 1)
{
Console.WriteLine("6$ Cheese added to your cart");
}
// Should be "else if"
else (keypress = 2){
Console.WriteLine("2$ Bread added to your cart");
}
// Should also be "else if"
else (keypress = 3){
Console.WriteLine("1$ cookie added to your cart");
}