简体   繁体   English

具有 1 个以上条件/条件类型 C# 的 while 循环条件

[英]while loop conditions with more than 1 condition/condition type C#

Trying to get my while loop to have more than 1 condition , basically User inputs a height in numbers and the code continues etc. the height data will be pass down the code for later calculations.试图让我的 while 循环有 1 个以上的条件,基本上用户输入一个数字高度,代码继续等等。高度数据将传递给代码以供以后计算。

Currently I have set it to when user doesn't enter a number (such as the Letter 'H') it enters a while loop for an error message.目前我已将其设置为当用户未输入数字(例如字母“H”)时,它会进入 while 循环以获取错误消息。 HOWEVER i want there to also be the condition that the height entered cannot be more than 3 and if say user enters 5, it also enters the loop for the error message.但是,我还希望输入的高度不能超过 3 的条件,如果说用户输入 5,它也会进入错误消息的循环。 Currently when entering the loop user can retry and enter a number ( can enter letters will keep going back).目前当进入循环用户可以重试并输入一个数字(可以输入字母会继续返回)。 in my while condition i have tried to add && (heightM >3) - (while (double.TryParse(userInput, out double heightM)&& (heightM <3) == false) - but doesnt seem to do what i want. in this case it ignores the tryparse, if user inputs 4 it loops correctly but if a letter , application crashes.在我的 while 条件中,我尝试添加 && (heightM >3) - (while (double.TryParse(userInput, out double heightM)&& (heightM <3) == false) - 但似乎没有做我想要的。在在这种情况下,它会忽略 tryparse,如果用户输入 4,它会正确循环,但如果是一个字母,应用程序会崩溃。

Im very new - still learning , sorry if this is a simple question : /我很新 - 仍在学习,抱歉,如果这是一个简单的问题:/

double height;
string userInput = Console.ReadLine().ToLower();
while (double.TryParse(userInput, out double heightM) && (heightM<3) == false)
{
    Console.Clear();
    Console.WriteLine("Incorrect value or input");
    Console.WriteLine("");
    Console.WriteLine("Enter Height in Meters only up to Maxmium of 3 meters");
    userInput = Console.ReadLine().ToLower();   
}

height = double.Parse(userInput);

Using a while(true) loop, we can check the conditions and break out of the loop, or the loop will continue forever until a correct value is inputted.使用while(true)循环,我们可以检查条件并break循环,否则循环将一直持续到输入正确的值。

double height;
double maxHeight = 3;
while (true)
{
    var userInput = Console.ReadLine();
    if (double.TryParse(userInput, out var heightM) && heightM <= maxHeight)
    {
        height = heightM;
        break;
    }

    Console.Clear();
    Console.WriteLine($"Please enter Height in Meters only up to maximum of {maxHeight} meters");
}
Console.WriteLine(height);

Parsing a string that is not a number will result in the out variable set to 0, so your logic is flawed.解析不是数字的字符串将导致 out 变量设置为 0,因此您的逻辑存在缺陷。 Instead you should change it to相反,您应该将其更改为

double heightM;
string userInput = Console.ReadLine().ToLower();
while (!double.TryParse(userInput, out heightM) || (heightM > 3))
{
    ....
}

The first change is used to have a single hejghtM variable, then the second change is how do we test the result of TryParse, if the parsing fails we could immediately enter the retry code.第一个变化是使用单个 hejghtM 变量,第二个变化是我们如何测试 TryParse 的结果,如果解析失败我们可以立即输入重试代码。 So the third change is using the ||所以第三个变化是使用 || instead of && and this leads to the fourth change where we test for value bigger than three to enter the retry loop.而不是 && ,这导致了第四个变化,我们测试大于 3 的值以进入重试循环。

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

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