Почему я должен вводить градусы дважды в цикле? - PullRequest
0 голосов
/ 11 марта 2019

Почему я должен вводить градусы дважды, когда начинается цикл?Например: при первом вводе 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 голосов
/ 11 марта 2019

Одна из проблем заключается в том, что после прочтения строки в каждом из операторов if вы ничего с этим не делаете. Он просто повторяется и снова выполняет readline:

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

Итак, после первого цикла вы выполняете ReadLine () дважды.

Удалите ReadLine из операторов if / else, и он будет работать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...