簡體   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