C
C#•3y ago
BigggMoustache

while() loop exits after first run [Answered]

int targetNumber = RandomNumberGenerator();
Console.WriteLine($"TargetNumber = {targetNumber}");
MainLoop();

void MainLoop()
{

bool parseSuccess = false;
int answerInt = 0;

while(!parseSuccess && answerInt != targetNumber)
{
parseSuccess = int.TryParse(Texting(), out answerInt);
if (parseSuccess && answerInt == targetNumber)
{
Console.WriteLine("Success!");
EndGame();
}
if (parseSuccess && answerInt != targetNumber)
{
Console.WriteLine("Wrong!");
}
if (!parseSuccess)
{
Console.WriteLine("That's not a valid numeral!");
}
}

}

string Texting()
{
Console.Write("Guess a number between 1 and 10: ");
string ?answer = Console.ReadLine();
if (string.IsNullOrEmpty(answer))
{
Console.WriteLine("Actually enter something!");
Texting();
}
return answer;
}

int RandomNumberGenerator()
{
Random random = new Random();
int randomInt = random.Next(1, 11);
return randomInt;
}

void EndGame()
{
Console.WriteLine("Would you like to play again");
string input = Console.ReadLine();
if (input == "yes")
{
targetNumber = RandomNumberGenerator();
MainLoop();
}
}
int targetNumber = RandomNumberGenerator();
Console.WriteLine($"TargetNumber = {targetNumber}");
MainLoop();

void MainLoop()
{

bool parseSuccess = false;
int answerInt = 0;

while(!parseSuccess && answerInt != targetNumber)
{
parseSuccess = int.TryParse(Texting(), out answerInt);
if (parseSuccess && answerInt == targetNumber)
{
Console.WriteLine("Success!");
EndGame();
}
if (parseSuccess && answerInt != targetNumber)
{
Console.WriteLine("Wrong!");
}
if (!parseSuccess)
{
Console.WriteLine("That's not a valid numeral!");
}
}

}

string Texting()
{
Console.Write("Guess a number between 1 and 10: ");
string ?answer = Console.ReadLine();
if (string.IsNullOrEmpty(answer))
{
Console.WriteLine("Actually enter something!");
Texting();
}
return answer;
}

int RandomNumberGenerator()
{
Random random = new Random();
int randomInt = random.Next(1, 11);
return randomInt;
}

void EndGame()
{
Console.WriteLine("Would you like to play again");
string input = Console.ReadLine();
if (input == "yes")
{
targetNumber = RandomNumberGenerator();
MainLoop();
}
}
21 Replies
BigggMoustache
BigggMoustacheOP•3y ago
$code
MODiX
MODiX•3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
BigggMoustache
BigggMoustacheOP•3y ago
BlazeBin - whxylayrrrvk
A tool for sharing your source code with the world!
BigggMoustache
BigggMoustacheOP•3y ago
I am not sure why the while() conditions are being met and ending the loop :[
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
yeah I just ran through it
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
i'll look again omg I'm a dunce
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
I forgot the way the operators work I guess
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
yeah misremembering the operator && thanks Strange users can't delete their own useless threads like this. 🙄 oh well thanks again.
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
Oh yeah that is great advice thanks. frustration gets the better sometimes, especially when dumb problems.
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
Oh wow I didn't knowmm that was a condition yeah tyvm
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord•3y ago
✅ This post has been marked as answered!
BigggMoustache
BigggMoustacheOP•3y ago
I meant nuking the thread because it's essentially useless for everyone lol. Thanks again.
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP•3y ago
Huh. Well thanks that's actually interesting to me.
Want results from more Discord servers?
Add your server