Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# If语句c中的If语句_C# - Fatal编程技术网

C# If语句c中的If语句

C# If语句c中的If语句,c#,C#,我编写了一个控制台应用程序,当用户输入数字时打印。当用户按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 =

我编写了一个控制台应用程序,当用户输入数字时打印。当用户按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");
}

使用赋值运算符=而不是==。因此,在if条件中使用==更改

你还需要在else之后添加新的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 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");
}
=是赋值运算符。 ==是一个相等运算符


使用条件语句时需要使用==。

您使用的是赋值运算符“=”而不是相等运算符“=”

keypress = 1;
上面将尝试将按键设置为值1

keypress == 1;

如果keypress等于1,上述代码将进行计算。

如果不使用elseIF,请使用ifkeypress==1而不是ifkeypress=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;
            }
          }
这里有几件事:

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

您能解释一下您的异常情况吗?如果keypress=1错误CS0029无法隐式地将类型“int”转换为“bool”,您不使用equal运算符,请使用==。使用single=时,您将为变量赋值=用于赋值,而==用于比较。您试图在执行比较==时执行赋值。