C
C#12mo 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
SinFluxx12mo ago
its your while statement that is running infinitely
Cheetah
Cheetah12mo ago
ok so how do i make it so it doesnt run infinitly but the code doesnt end after one input
Jimmacle
Jimmacle12mo 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
SinFluxx12mo 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
Cheetah12mo ago
ok but like
SinFluxx
SinFluxx12mo 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
Cheetah12mo ago
i want it to run once every time i write something
Jimmacle
Jimmacle12mo ago
but you want to write something multiple times?
Cheetah
Cheetah12mo ago
yes i wanna make a guessing game
Jimmacle
Jimmacle12mo 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
Cheetah12mo ago
what_ i dont understand
Jimmacle
Jimmacle12mo ago
which part is confusing?
SinFluxx
SinFluxx12mo ago
if the user's guess is wrong you need to ask them for another number?
Cheetah
Cheetah12mo ago
if the users guess is wrong it says higher or lower depending on if the correct number is higher or lower
Jimmacle
Jimmacle12mo ago
right, then what?
Cheetah
Cheetah12mo ago
and u just do that until you guess the number but it keeps saying higher many times
Jimmacle
Jimmacle12mo 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
Cheetah12mo ago
its running the same thing multiple times but idk how to make it run only once
Jimmacle
Jimmacle12mo 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
Cheetah12mo ago
yes
Jimmacle
Jimmacle12mo ago
so all those things should be put in the loop
Cheetah
Cheetah12mo ago
so i add more of the code to the loop??
SinFluxx
SinFluxx12mo 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
Cheetah12mo ago
aight thank you fixed it
Want results from more Discord servers?
Add your server
More Posts