【问题标题】:while loop in c#c#中的while循环
【发布时间】:2012-11-11 17:23:42
【问题描述】:

我有这个代码:

   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"。
如果我然后 meenter -1,这显示的是什么而不是 AVG:“输入下一个锡安,如果你想退出 点击 -1 \n。”
我该如何解决这个问题,所以当数字为负数或大于百并且点击 -1 时,我将看到 AVG 而不是另一条消息?

【问题讨论】:

    标签: c# loops while-loop average


    【解决方案1】:

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

    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(); 
          }
        }
    }
    

    【讨论】:

    • 嘿,你能在不添加另一个变量的情况下解决问题吗?
    • 是的,我写的只是为了您的理解。您可以替换“if (flag == 1)break;”与“如果(锡安== -1)休息;”并删除标志变量
    • 谢谢,但我是一名学生,我不知道“break”是什么意思,主要问题是如果我输入的数字大于 hunderd os snaller than -1 ,并且if I prees -1 that ishappend " 'cnt++; sum = sum + zion; Console.Write("Enter next zion, if you want to exit tap -1 \n"); zion = int.Parse(Console.ReadLine( ));'" 没有计算平均值,为什么?
    • Break 用于中断循环,例如foreach、do while 和while。对于您在评论中提到的其他内容,我会说您正在将 avg 打印在 while 循环之外,一旦您了解了循环和 if 的概念,您就可以正确管理整个代码。日复一日地练习,你就会得到它。
    【解决方案2】:

    只需在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){}
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2013-02-22
      • 1970-01-01
      • 2015-06-29
      • 2012-07-18
      • 2017-11-14
      • 1970-01-01
      • 2014-11-02
      • 2013-02-08
      相关资源
      最近更新 更多