while while循环没有以所需的方式执行

时间:2018-02-28 05:16:07

标签: c# c#-4.0

我正在尝试做一个do ... while循环,以防止用户以华氏度为单位输入温度以外的值。如果用户输入非整数值,则循环将返回到“温度是多少(华氏度):”并让用户再次尝试输入正确的输入。但是,当我执行以下代码时,它只允许我输入错误消息“无效输入”旁边的值,以将输入值传输到摄氏度。

using System;

namespace SimpleTempConversion
{
    public class FahrenheitToCelsiusV2
    {
        public static void Main()
        {
            // Declare a variable of type 'double' to hold the temperature obtained from the user

            double f;

            // Declare a variable of type 'double' to hold the converted temperature
            double c;

            // Declare a bool to record if the user has entered in a valid number yet
            string fahren;
            do
            {// Display "What is the temperature (in degrees Fahrenheit): "
                Console.Write("What is the temperature (in degrees Fahrenheit): ");

                // Use TryParse() to read the Fahrenheit temperature. Set the bool variable
                // declared earlier to the result of TryParse()
                fahren = Console.ReadLine();
                f = double.TryParse(fahren);
                double.TryParse(fahren, out f);

                // Check the bool variable to see if TryParse() failed to parse
                if (f == false)
                {
                    // Display "Invalid input" on a line by itself.
                    Console.Write("Invalid input");
                }

                // The code should loop while the input is not valid
            } while (f == false);

            // Convert the Fahrenheit temperature into degrees Celsius
            c = (5.0 / 9.0) * (f - 32);

            // Display "The temperature is (temperature here) degrees Celsius"
            Console.WriteLine("The temperature is " + c + " degrees Celsius");

            // Prompt the user to press enter to close the window
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }
    }
}

输出:

What is the temperature (in degrees Fahrenheit): 

ssd

Invalid input

What is the temperature (in degrees Fahrenheit): 

12

What is the temperature (in degrees Fahrenheit): 

Invalid input12

The temperature is -11.1111111111111 degrees Celsius
Press enter to exit.

OUTPUT

2 个答案:

答案 0 :(得分:1)

我使你的代码缩短了一点,你可以使用!double.TryParse(Console.ReadLine(), out f)作为while循环条件并删除不必要的变量和if子句:

public static void Main()
{
    double f;

    double c;

    Console.Write("What is the temperature (in degrees Fahrenheit): ");

    while (!double.TryParse(Console.ReadLine(), out f))
    {
        Console.WriteLine("Invalid input");
        Console.Write("What is the temperature (in degrees Fahrenheit): ");
    }

    c = (5.0 / 9.0) * (f - 32);

    Console.WriteLine("The temperature is " + c + " degrees Celsius");

    Console.WriteLine("Press enter to exit.");

    Console.ReadLine();
}

您可以在此处测试我的代码:https://dotnetfiddle.net/IIY6bH

我机器上的输出:

enter image description here

答案 1 :(得分:0)

您使用变量f进行了太多操作。你应该拥有2个独立的变量,一个用于值,另一个用于解析是否成功。

bool parseOk = false;
do
{
    ...
    parseOk = double.TryParse(fahren, out f);
    ...
}
while(!parseOk)