为什么我必须在循环中输入两次学位?

时间:2019-03-11 19:58:13

标签: c# loops

为什么在循环开始时必须输入两次度数?例如:我第一次输入140时什么也没有发生,当我再次输入140时,它将继续循环。

namespace Uppgift_2___Bastun
{
    class Program
    {
        static int FahrenheitToCelsius(int fahrenheit)
        {
            int celsius = ((fahrenheit - 32) * 5) / 9;
            return celsius;
        }

        static void Main(string[] args)
        {
            int celsius;

            Console.WriteLine("Hello! Welcome to the sauna!");
            Console.WriteLine();
            Console.WriteLine("Please enter your desired degrees in Fahrenheit: ");
            do
            {
                int fahrenheit = Convert.ToInt32(Console.ReadLine());
                celsius = FahrenheitToCelsius(fahrenheit);
                Console.WriteLine("The sauna is now set to " + fahrenheit + " degrees Fahrenheit, which equals to " + celsius + " degrees Celsius.");

                if (celsius < 25)
                {
                    Console.WriteLine("Way too cold! Turn the heat up: ");
                    Console.ReadLine();
                }
                else if (celsius < 50)
                {
                    Console.WriteLine("Too cold, turn the heat up: ");
                    Console.ReadLine();
                }
                else if (celsius < 73)
                {
                    Console.WriteLine("Just a little bit too cold, turn the heat up a little to reach the optimal temperature: ");
                    Console.ReadLine();
                }
                else if (celsius == 75)
                {
                    Console.WriteLine("The optimal temperature has been reached!");
                }
                else if (celsius > 77)
                {
                    Console.WriteLine("Too hot! Turn the heat down: ");
                    Console.ReadLine();
                }
                else
                    Console.WriteLine("The sauna is ready!");
                {
                }
            } while (celsius < 73 || 77 < celsius);
            Console.ReadLine();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题之一是,在阅读每个if语句中的行之后,您什么都不做。它只是重复并在这里再次进行读取行:

int fahrenheit = Convert.ToInt32(Console.ReadLine());

因此,在第一个循环之后,您将执行两次ReadLine()。

从if / else语句中删除ReadLine,它将起作用。