Reputation: 101
Below is my code
namespace ProgrammingTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("GUESS THE ANSWER");
Console.WriteLine("Please enter the input");
int input1 = (int.Parse(Console.ReadLine()));
while (input1 != 4)
{
Console.WriteLine("Try agian");
}
if (input1 == 4)
{
Console.WriteLine("You are a winner");
Console.ReadLine();
}
else if (input1 < 4)
{
Console.WriteLine("TOOOOO low");
Console.ReadLine();
}
else if (input1 > 4)
{
Console.WriteLine("TOOOO high");
Console.ReadLine();
}
}
}
}
Here i am putting while condition but still unable to proceed.It's going infinite loop
Here i want to apply a condition that if the user puts a number other than 4 then it should come to starting and user should again enter the number. Now its happening that its closing the console app. How to put the while loo
Upvotes: 1
Views: 2612
Reputation: 780
I have modified your code, please check
namespace ProgrammingTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("GUESS THE ANSWER");
Console.WriteLine("Please enter the input");
int input1 = 0;
while (input1 != 4)
{
input1 = (int.Parse(Console.ReadLine()));
if (input1 == 4)
{
Console.WriteLine("You are a winner");
Console.ReadLine();
}
else if (input1 < 4)
{
Console.WriteLine("TOOOOO low");
}
else if (input1 > 4)
{
Console.WriteLine("TOOOO high");
}
}
}
}
}
Remove Console.Readline() , so that you dont need to enter doble time
Upvotes: 0
Reputation: 60503
add
input1 = (int.Parse(Console.ReadLine()));
at the end of your while loop, or input1 will never be changed.
But that won't be enough.
You should do (not fine, but working)
static void Main(string[] args)
{
Console.WriteLine("GUESS THE ANSWER");
int answer;
var success = false;
Console.WriteLine("Please enter the input");
while (!success) {
var userInput = Console.ReadLine();
if (!Int32.TryParse(userInput, out answer))
Console.WriteLine("You must enter an integer");
else {
if (answer < 4)
Console.WriteLine("Too low");
else if (answer > 4)
Console.WriteLine("Too high");
else {
Console.WriteLine("Grats !");
success = true;
}
}
if (!success)
Console.WriteLine("Try again !");
}
}
Upvotes: 2
Reputation: 1040
I corrected your indentation and you can see your while block contains only the Console.WriteLine and not the input check so the input1 won't change and your loop is infinite.
If you remove the } under Console.WriteLine and you put it to the end you will be able to stop the loop with 4 typed in input.
Upvotes: 0
Reputation: 292625
while (input1 != 4)
{
Console.WriteLine("Try agian");
}
In the code above, the value of input1
never changes, so it loops forever
Upvotes: 0
Reputation: 16149
You're only changing the value of input1
before the loop starts. Since you're never changing it again, it will continue to loop forever.
EDIT : You might want to consider doing the following for your loop:
int input1 = 0;
while (input1 != 4)
{
input1 = (int.Parse(Console.ReadLine()));
if (input1 == 4)
{
Console.WriteLine("You are a winner");
}
else if (input1 < 4)
{
Console.WriteLine("TOOOOO low");
Console.WriteLine("Try agian");
}
else if (input1 > 4)
{
Console.WriteLine("TOOOO high");
Console.WriteLine("Try agian");
}
}
Upvotes: 2
Reputation: 2037
You have an extra }
right above the if (input1 == 4)
that is closing the while loop, so the while loop never exits because it has no code inside of it. Move that curly brace down below the if else
.
Upvotes: -1
Reputation: 29233
Well, input1
doesn't change anywhere inside the loop. So, naturally, if the loop is entered once, it's entered every time.
Upvotes: 0