简体   繁体   English

如果(Integer Console.Readline“”或null)不起作用

[英]If (Integer Console.Readline “” or null) doesn't work

I can't figure out after searching and searching (you know how it goes) why this code doesn't work. 在搜索和搜索(您知道它的运行方式)之后,我不知道为什么此代码不起作用。

I just want it to work like this 我只希望它像这样工作

if (Nummer == "") {
    Console.WriteLine("0");
}

That's it, and it doesn't work. 就是这样,它不起作用。 I have been searching for an hour and a half. 我一直在寻找一个半小时。 Can't understand why there is a simple basic explanation. 无法理解为什么有一个简单的基本解释。 I only found how to fix it with a string or something, then I tried to convert it and it still didn't work. 我只找到了如何用字符串之类的东西修复它,然后我尝试将其转换,但仍然无法正常工作。 Can someone help me please? 有人能帮助我吗?

I appreciate your patience for my limited knowledge. 感谢您对我的有限知识的耐心配合。 Thanks for your time 谢谢你的时间

static void Main(string[] args)
{
    bool herhaal = true;

    do
    {                
        Console.Write("Geef een getal : ");
        int Nummer = Convert.ToInt16(Console.ReadLine());           

        if (Console.ReadLine() == "" && Console.ReadLine() == null)
        {
            Console.WriteLine("0");
        }

        double kw = Math.Pow(Nummer, 2);

        Console.WriteLine("Kwadraat van {0} is: {1}", Nummer, kw + Environment.NewLine);
    } while (herhaal);
}
static void Main(string[] args)
{
    int Nummer;
    bool herhaal = true;
    do
    {                
        Console.Write("Geef een getal : ");          
        //only read from the Console ONCE per loop iteration, and always read to a string first
        string input = Console.ReadLine(); 

        //TryParse better than Convert for converting strings to integers
        if (!int.TryParse(input, out Nummer))        
        {
            Console.WriteLine("0");
        }
        else  //only do the second part if the conversion worked
        {
            double kw = Math.Pow(Nummer, 2);
            Console.WriteLine("Kwadraat van {0} is: {1}\n", Nummer, kw);
        }

    } while (herhaal);
}

To do this from a WinForms app, as attempted in the comments: 要从WinForms应用程序执行此操作,如注释中所尝试:

private void button1_Click(object sender, EventArgs e)
{
    double aantalgroep;
    if (!double.TryParse(textBox1.Text, out aantalgroep))        
    {
        textBox1.Text = "0";
    }
    else 
    {
        double kw = Math.Pow(aantalgroep, 2);
        textBox1.Text = String.Format("Kwadraat van {0} is: {1}", aantalgroep, kw);
    }
}

The Console.ReadLine() method 'reads' one line of user input, and stores in whatever the variable you assign it to. Console.ReadLine()方法“读取”一行用户输入,并将其分配给的变量存储在其中。 Here's more info . 这是更多信息

So, your following line of code reads the number you inputs and stores in Nummer . 因此,下面的代码行将读取您输入的数字并将其存储在Nummer

int Nummer = Convert.ToInt16(Console.ReadLine());           

But then you go on and do two more Console.ReadLine() method calls in your if statement, so what the if statement really does is try to read from the console two more times, and see if the first read is "" and if the second read is null , which is not your desired behavior. 但是接着您继续在if语句中再执行两次Console.ReadLine()方法调用,因此if语句的真正作用是尝试再次从控制台读取两次,并查看第一次读取的内容是否为"" 以及第二次读取为null ,这不是您想要的行为。

What you want to do is read once, and compare whatever you read. 您想要做的是阅读一次,然后比较阅读的内容。 Going by your code it looks like you want to output the square of the number entered by the user, so you probably should check for more than just Nummer == "" , since if the user entered an alphabet character, that would also result in an error. 按照您的代码进行操作,看起来您想输出用户输入的数字的平方,因此您可能应该检查的不只是Nummer == "" ,因为如果用户输入了字母字符,这也会导致一个错误。 So using int.TryPrase() is a better option for you. 因此,使用int.TryPrase()对您来说是一个更好的选择。

static void Main(string[] args)
{
    bool herhaal = true;
    do
    {
        Console.Write("Geef een getal : ");
        string Nummer = Console.ReadLine();

        if (int.TryParse(Nummer, out int result))
        {
            double kw = Math.Pow(result, 2);
            Console.WriteLine("Kwadraat van {0} is: {1}", Nummer, kw + Environment.NewLine);
            herhaal = false;
        }
        else
        {
            Console.WriteLine("0");
        }

    } while (herhaal);

    Console.ReadLine();
}

According to the C# Documentation , Console.ReadLine does the following 根据C#文档Console.ReadLine执行以下操作

Reads the next line of characters from the standard input stream. 从标准输入流中读取下一行字符。

This means that every time you call Console.ReadLine , a new line will be read from the console. 这意味着,每次调用Console.ReadLine ,都会从控制台读取新行。 From what I can see in your code, this isn't the behavior that you want. 从您的代码中可以看到,这不是您想要的行为。 To fix your problem, you should store the result of Console.ReadLine in a variable and use that variable instead of the ReadLine method. 要解决您的问题,您应该将Console.ReadLine的结果存储在一个变量中,并使用该变量而不是ReadLine方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM