Nave Tseva
Nave Tseva

Reputation: 401

while loop in c#

I have this code:

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

The problem here is that if in the beginning I enter a negative or bigger than hundred number, I will get this message: "zion can be between 0 to 100 only! \nyou can rewrite the zion here, or Press -1 to see the avg\n".
If I then meenter -1, this what that shows up instead of the AVG: "Enter next zion, if you want to exit tap -1 \n."
How can I solve this problem so when the number is negative or bigger than hundred and than tap -1 I will see the AVG an not another message?

Upvotes: 1

Views: 998

Answers (2)

Vivek Muthal
Vivek Muthal

Reputation: 1015

You can just add a flag variable and it's done.

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

Upvotes: 1

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Just enclose this code that you don't want to be executed in if statement like this

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){}
}

Upvotes: 1

Related Questions