3
Reply

Problem with the If statement

Henry Lhteenmaki

Henry Lhteenmaki

Nov 10 2011 12:38 PM
1.5k
Hi !
First of all I would to say, that I´m just a beginner in C#, I may ask  some silly question , I apologize for this convenient.
well I have a program and I want to stop it from executing the rest of the code until the condition witch is var > 0 is true .
the problem after I execute this code , the program still ask me about the second score even the previous score is negative .


And here is the code :


[CODE]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           string name;
           string svalue;
           int x, y, z, aV;
          
           
            // Enter User´s name.
            Console.Write("Enter please your name  : \n");
            name = Console.ReadLine();

            // grap three scores from users.
          
            Console.Write("Enter 1 score : \n ");
            svalue = Console.ReadLine();
            x = int.Parse(svalue);

            /* Testing if the score is less than 0
               negative nubers are not allowed */

            if (x < 0)
          
           

             Console.WriteLine("The score must be a positive number ");
           
           

            else if (x > 0)
           
           
                Console.Write("Enter 2 score : \n ");
                svalue = Console.ReadLine();
                y = int.Parse(svalue);
           

            /* Testing if the score is less than 0
               negative nubers are not allowed */

            if (y < 0 )

           

                Console.WriteLine("The score must be  a positive number ");

           

            else if (y > 0)
           
           
                Console.Write("Enter 3 score : \n ");
                svalue = Console.ReadLine();
                z = int.Parse(svalue);

           


            /* Testing if the score is less than 0
               negative nubers are not allowed */
           
            if ( z < 0 )

           

                Console.WriteLine("The score must be a positive number ");

           

            else if (z > 0)
           
           

            // Dispaying the inputs

            Console.WriteLine("Your name is {0}", name);
            Console.WriteLine("Your score is {0}", x + y + z / 3);
            Console.ReadKey();

           


        }
    }
}
[/CODE]

Answers (3)