繁体   English   中英

C#中的while循环

[英]while loop in c#

我有这个代码:

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _121119_zionAVGfilter_Nave
{
    class Program
    { 
        static void Main(string[] args)
        {
            int cnt = 0, zion, sum = 0;
            double avg;
            Console.Write("Enter first zion \n");
            zion = int.Parse(Console.ReadLine());
            while (zion != -1)
            {
               while (zion < -1 || zion > 100)
            {
                Console.Write("zion can be between 0 to 100 only! \nyou can rewrite the zion here, or Press -1 to see the avg\n");
                zion = int.Parse(Console.ReadLine());
            }

                cnt++;
                sum = sum + zion;
                Console.Write("Enter next zion, if you want to exit tap -1 \n");
                zion = int.Parse(Console.ReadLine());


            }
            if (cnt == 0)
            {
                Console.WriteLine("something doesn't make sence");
            }
            else
            {
                avg = (double)sum / cnt;
                Console.Write("the AVG is {0}", avg);

            }
       Console.ReadLine(); }
    }
}

这里的问题是,如果一开始我输入一个负数或大于百的数字,我会得到这样的消息:“zion 只能在 0 到 100 之间!\\n你可以在这里重写 zion,或者按 -1 来查看平均\\n”。
如果然后我输入 -1,则会显示而不是 AVG:“输入下一个锡安,如果您想退出,请点击 -1 \\n。”
我该如何解决这个问题,以便当数字为负数或大于百并点击 -1 时,我会看到 AVG 而不是另一条消息?

只需将您不想在这样的if语句中执行的代码括起来

if(zion != -1)
{
      cnt++;
      sum = sum + zion;
      Console.Write("Enter next zion, if you want to exit tap -1 \n");
      zion = int.Parse(Console.ReadLine());
      if (cnt != 0){}
}

您只需添加一个标志变量即可完成。

namespace _121119_zionAVGfilter
{
    class Program
    { 
        static void Main(string[] args)
    {
        int cnt = 0, zion, sum = 0;
        double avg;
        int flag = 0;
        Console.Write("Enter first zion \n");
        zion = int.Parse(Console.ReadLine());
        while (zion != -1)
        {                 
            while (zion < -1 || zion > 100)
            {
                Console.Write("zion can be between 0 to 100 only! \nyou can rewrite the zion here, or Press -1 to see the avg\n");
                zion = int.Parse(Console.ReadLine());
                if(zion== -1)
                    flag = 1;
            }                
            cnt++;
            sum = sum + zion;
            if (flag == 1)
                break;
            Console.Write("Enter next zion, if you want to exit tap -1 \n");
            zion = int.Parse(Console.ReadLine());
            if (cnt != 0) { }

        }
        if (cnt == 0)
        {
            Console.WriteLine("something doesn't make sence");
        }
        else
        {
            avg = (double)sum / cnt;
            Console.Write("the AVG is {0}", avg);                
        }            
        Console.ReadLine(); 
      }
    }
}

暂无
暂无

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

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