为什么我在c#中的do-while循环不起作用?

问题描述 投票:-3回答:1

我的程序是在有人输入温度之前,直到他们输入999的地方,温度必须在-20到130之间。一旦输入999,就应该计算输入的温度总量和平均温度。我不确定这段代码在哪里出问题,我确实在循环方面遇到了一些麻烦。任何帮助表示赞赏!

    static void Main(string[] args)
    {
        int temp = 0, total = 0, sum = 0;
        double avg;
        string = tempString;

        WriteLine("Enter daily high temperatures, to stop program enter 999.");
        tempString = ReadLine();
        temp = Convert.ToInt32(tempString);

        do
        {
            if (temp >= 20 && temp <= 130)
            {
                WriteLine("Enter daily high temperatures, to stop program enter 999");
                ReadLine();
                total++;
            }
            else
            {
                WriteLine("Valid temperatures range from -20 to 130. Please reenter temperature.");
                ReadLine();
            }
        } while (temp != 999);
       sum += temp;
        avg = sum / total;
        WriteLine("The number of temperatures entered: {0} /n The average temperature is: {1}.", total, avg);

    }
c# do-while
1个回答
0
投票
将您的代码更新为以下内容-
© www.soinside.com 2019 - 2024. All rights reserved.