简体   繁体   中英

C# control flow

I made a number which asks for 2 numbers with C# and responds with the corresponding message for the case. how come it doesnt work for the second number ? Regardless what I enter for the second number , I am getting the message "your number is in the range 0-10".

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myInput;  // declaring the type of the variables
            int myInt;

            string number1;
            int number;


            Console.WriteLine("enter a number");
            myInput = Console.ReadLine(); //muyInput is a string  which is entry input
            myInt = Int32.Parse(myInput); // myInt converts the string into an Integer

            if (myInt > 0)
                Console.WriteLine("Your number {0} is greater than zero.", myInt);
            else if (myInt < 0)
                Console.WriteLine("Your number {0} is  less  than zero.", myInt);
            else
                Console.WriteLine("Your number {0} is equal zero.", myInt);

            Console.WriteLine("enter another number");
            number1 = Console.ReadLine(); 
            number = Int32.Parse(myInput); 

            if (number < 0 || number == 0)
                Console.WriteLine("Your number {0} is  less  than zero or equal zero.", number);
            else if (number > 0 && number <= 10)
                Console.WriteLine("Your number {0} is  in the range from 0 to 10.", number);
            else
                Console.WriteLine("Your number {0} is greater than 10.", number);

            Console.WriteLine("enter another number");

        }
    }    
}

number = Int32.Parse(myInput); should read number = Int32.Parse(number1);

number = Int32.Parse(myInput);

第二个数字应该是

number = Int32.Parse(number1);

Because you are parsing the same string twice

number1 = Console.ReadLine(); 
number = Int32.Parse(myInput); 

Should be

number1 = Console.ReadLine(); 
number = Int32.Parse(number1); 

What are you typing in as the first number? After "enter another number" you assign number1 to the value from the console, but you never use that, you actually use the value from myInput

number1 = Console.ReadLine(); 
number = Int32.Parse(myInput); 

In your second test your evaluating the first input, it should be

Int32.Parse(number1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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