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开发者_JAVA百科 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);
for the second number should be
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);
精彩评论