C
C#16mo ago
Cheetah

How to make If statement in loop only run once

i have three if statements and when i write a number it runs infinitetly even tho its if
using System;
using System.Runtime.InteropServices;

namespace Guessing_game
{
internal class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int num = rnd.Next(1, 100);
Console.WriteLine(num);
Console.WriteLine("Guess the number");
string Guess = Console.ReadLine();

int IntGuess = int.Parse(Guess);

Console.WriteLine(IntGuess);

int on = 0;

while (on < 1)
{
if (num > IntGuess)
{
Console.WriteLine("Higher");
}

if (num < IntGuess)
{
Console.WriteLine("Lower");
}

if (num == IntGuess)
{
Console.WriteLine("Good job");

}
}
}
}
}
using System;
using System.Runtime.InteropServices;

namespace Guessing_game
{
internal class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int num = rnd.Next(1, 100);
Console.WriteLine(num);
Console.WriteLine("Guess the number");
string Guess = Console.ReadLine();

int IntGuess = int.Parse(Guess);

Console.WriteLine(IntGuess);

int on = 0;

while (on < 1)
{
if (num > IntGuess)
{
Console.WriteLine("Higher");
}

if (num < IntGuess)
{
Console.WriteLine("Lower");
}

if (num == IntGuess)
{
Console.WriteLine("Good job");

}
}
}
}
}
24 Replies
SinFluxx
SinFluxx16mo ago
its your while statement that is running infinitely
Cheetah
CheetahOP16mo ago
ok so how do i make it so it doesnt run infinitly but the code doesnt end after one input
Jimmacle
Jimmacle16mo ago
you need to put everything that you want to repeat inside the while loop right now your loop runs forever because the value of on never changes inside it
SinFluxx
SinFluxx16mo ago
well you've set the condition for it to run while (on < 1) so you need to make that condition false once you're finished
Cheetah
CheetahOP16mo ago
ok but like
SinFluxx
SinFluxx16mo ago
like @jimmacle said as well all the relevant code needs to be inside your while loop as well (i.e. where you're getting the guess from the user)
Cheetah
CheetahOP16mo ago
i want it to run once every time i write something
Jimmacle
Jimmacle16mo ago
but you want to write something multiple times?
Cheetah
CheetahOP16mo ago
yes i wanna make a guessing game
Jimmacle
Jimmacle16mo ago
if you only want something to run once just don't put it in its own loop put the entire process that you want to repeat in there so if you want to guess numbers until you get it right, you probably want your loop to include asking for guesses
Cheetah
CheetahOP16mo ago
what_ i dont understand
Jimmacle
Jimmacle16mo ago
which part is confusing?
SinFluxx
SinFluxx16mo ago
if the user's guess is wrong you need to ask them for another number?
Cheetah
CheetahOP16mo ago
if the users guess is wrong it says higher or lower depending on if the correct number is higher or lower
Jimmacle
Jimmacle16mo ago
right, then what?
Cheetah
CheetahOP16mo ago
and u just do that until you guess the number but it keeps saying higher many times
Jimmacle
Jimmacle16mo ago
do that until you guess the number
"that" is what should be in the loop right, walk through your code and see if you can figure out why that's happening
Cheetah
CheetahOP16mo ago
its running the same thing multiple times but idk how to make it run only once
Jimmacle
Jimmacle16mo ago
but you don't want it to only run once, you're just looping around the wrong things you want something to run until they guess the right number, right?
Cheetah
CheetahOP16mo ago
yes
Jimmacle
Jimmacle16mo ago
so all those things should be put in the loop
Cheetah
CheetahOP16mo ago
so i add more of the code to the loop??
SinFluxx
SinFluxx16mo ago
yes, think about what you need to repeat you ask me to guess a number I give you a number you tell me it's wrong then...?
Cheetah
CheetahOP16mo ago
aight thank you fixed it
Want results from more Discord servers?
Add your server