+ 1

how to make the "writeline" in a loop run only once

int num = 0; Random example = new Random(); num = example.Next(1, 1001); String X = Console.ReadLine(); int Y = Convert.ToInt32(X); if (Y>num) { Console.WriteLine("Larger"); } else if(Y<num) { Console.WriteLine("Smaller"); } else { Console.WriteLine(Y); } Console.Read();

7th Mar 2017, 3:53 AM
Tracyzhao
Tracyzhao - avatar
3 Antworten
+ 5
At first there wasn't code...now: : There's no loop here. : Can you add the loop you want to use? : Also, should the loop continue after printing or do you want to break out of it?
7th Mar 2017, 4:02 AM
Kirk Schafer
Kirk Schafer - avatar
+ 4
The answer you posted does what you want already...by default...because 1002 is > than the maximum value the Next() function generates. I'm guessing this wasn't a question then...
7th Mar 2017, 4:31 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
int num = 0; Random example = new Random(); num = example.Next(1, 1001); String X = Console.ReadLine(); int Y = Convert.ToInt32(X); do { if (Y > num) Console.WriteLine("Larger"); else if (Y < num) Console.WriteLine("Smaller"); else Console.WriteLine(Y);break; } while (num ==1002);
7th Mar 2017, 4:10 AM
Tracyzhao
Tracyzhao - avatar