C
C#13mo ago
levia_tano

❔ multiple conditions do while loop

im new to programming and was wondering if you could add multiple conditions to the do while loop like for example do { whatever the code is } while (condition) can you add another condition here or only one?
34 Replies
SinFluxx
SinFluxx13mo ago
Sure: while(condition1 && condition2) if both conditions are required to be true, or if only one condition needs to be true: while(condition1 || condition2) and obviously you can add more than two conditions
Pobiega
Pobiega13mo ago
conditions are boolean expressions, and those can always be combined into another boolean expression as SinFluxx showed above 🙂
levia_tano
levia_tanoOP13mo ago
i have another question wich isnt related but its bugging me alot im doing a number guessing game right and doing that you can play again too
Pobiega
Pobiega13mo ago
Sure
levia_tano
levia_tanoOP13mo ago
i cant seem to get to how i can make the program show the record of the smallest guesses the user needed to guess right like for example first time they play they need 9 guesses, second time 3 and third time 5 guesses. i want the program to show that thei record was 3 guesses
Pobiega
Pobiega13mo ago
sure thats easy enough, but how to implement it depends on your code
levia_tano
levia_tanoOP13mo ago
is it possible to make like a int value show the smalles value ever recorded of another int value ig? idk im very new sorry if im saying dumb stuff
Pobiega
Pobiega13mo ago
kinda, yeah you'd just check if the stored value is larger than your current value before overwriting it
levia_tano
levia_tanoOP13mo ago
yeah true should i share mine? its in swedish but im going to translate it
Pobiega
Pobiega13mo ago
No description
Pobiega
Pobiega13mo ago
I think its fine 🙂
levia_tano
levia_tanoOP13mo ago
lolll Random random = new Random(); int max = 100; int min = 1; int gissning; int gissningar = 0; int slumptalet; String svaret; bool spelaIgen = true; int spelomgångar = 0; while (spelaIgen) { slumptalet = random.Next(min, max); Console.WriteLine(slumptalet); gissningar = 0; spelomgångar++; rekord = do { Console.WriteLine("skriv talet"); gissning = Convert.ToInt32(Console.ReadLine()); if (gissning > slumptalet) { Console.WriteLine("Ditt tal är för stor"); } if (gissning < slumptalet) { Console.WriteLine("Ditt tal är för litet"); } gissningar++; } while (gissning != slumptalet && gissningar != 4); Console.WriteLine("Game Over"); Console.WriteLine(gissningar); Console.WriteLine("Vill du spela igen Ja/Nej"); svaret = Console.ReadLine(); if (svaret == "Ja") { spelaIgen = true; } else { Console.WriteLine("rekordet: " ); spelaIgen = false; Console.WriteLine(spelomgångar); }
Pobiega
Pobiega13mo ago
$code
levia_tano
levia_tanoOP13mo ago
its very confusing i know haha
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Pobiega
Pobiega13mo ago
wrap it in a code block please so its readable
levia_tano
levia_tanoOP13mo ago
okok
Pobiega
Pobiega13mo ago
!e int smallest = 1000; for (int i = 0; i < 100; i++) { int randomNumber = Random.Shared.Next(1, 1000); if (randomNumber < smallest) { smallest = randomNumber; } } Console.WriteLine($"Smallest number in 1000 rerolls was... {smallest}");
MODiX
MODiX13mo ago
Pobiega
REPL Error
An error occurred while sending a request to the REPL service. This may be due to a StackOverflowException or exceeding the 30 second timeout. Details: An error occurred while sending the request.
Tried to execute
levia_tano
levia_tanoOP13mo ago
// internal class Program
{
private static void Main(string[] args)
{
Random random = new Random();
int max = 100;
int min = 1;
int gissning;
int gissningar = 0;
int slumptalet;
String svaret;
bool spelaIgen = true;
int spelomgångar = 0;


while (spelaIgen)
{
slumptalet = random.Next(min, max);
Console.WriteLine(slumptalet);
gissningar = 0;
spelomgångar++;
rekord =

do
{

Console.WriteLine("skriv talet");
gissning = Convert.ToInt32(Console.ReadLine());

if (gissning > slumptalet)
{
Console.WriteLine("Ditt tal är för stor");
}
if (gissning < slumptalet)
{
Console.WriteLine("Ditt tal är för litet");
}

gissningar++;


}
while (gissning != slumptalet && gissningar != 4);

Console.WriteLine("Game Over");
Console.WriteLine(gissningar);
Console.WriteLine("Vill du spela igen Ja/Nej");
svaret = Console.ReadLine();

if (svaret == "Ja")
{
spelaIgen = true;
}
else
{
Console.WriteLine("rekordet: " );
spelaIgen = false;
Console.WriteLine(spelomgångar);
}

}


}
}
// internal class Program
{
private static void Main(string[] args)
{
Random random = new Random();
int max = 100;
int min = 1;
int gissning;
int gissningar = 0;
int slumptalet;
String svaret;
bool spelaIgen = true;
int spelomgångar = 0;


while (spelaIgen)
{
slumptalet = random.Next(min, max);
Console.WriteLine(slumptalet);
gissningar = 0;
spelomgångar++;
rekord =

do
{

Console.WriteLine("skriv talet");
gissning = Convert.ToInt32(Console.ReadLine());

if (gissning > slumptalet)
{
Console.WriteLine("Ditt tal är för stor");
}
if (gissning < slumptalet)
{
Console.WriteLine("Ditt tal är för litet");
}

gissningar++;


}
while (gissning != slumptalet && gissningar != 4);

Console.WriteLine("Game Over");
Console.WriteLine(gissningar);
Console.WriteLine("Vill du spela igen Ja/Nej");
svaret = Console.ReadLine();

if (svaret == "Ja")
{
spelaIgen = true;
}
else
{
Console.WriteLine("rekordet: " );
spelaIgen = false;
Console.WriteLine(spelomgångar);
}

}


}
}
Pobiega
Pobiega13mo ago
not sure why the bot failed, but the code is fine
int smallest = 1000;

for (int i = 0; i < 100; i++)
{
int randomNumber = Random.Shared.Next(1, 1000);
if (randomNumber < smallest)
{
smallest = randomNumber;
}
}

Console.WriteLine($"Smallest number in 100 rerolls was... {smallest}");
int smallest = 1000;

for (int i = 0; i < 100; i++)
{
int randomNumber = Random.Shared.Next(1, 1000);
if (randomNumber < smallest)
{
smallest = randomNumber;
}
}

Console.WriteLine($"Smallest number in 100 rerolls was... {smallest}");
levia_tano
levia_tanoOP13mo ago
im trying to understand your code
Pobiega
Pobiega13mo ago
okay, so you must declare rekord before you can use it sure, anything in particular you dont understand?
levia_tano
levia_tanoOP13mo ago
int smallest is basically rekord?
Pobiega
Pobiega13mo ago
sure its just an int variable that starts with a high number we then loop 100 times, each time we create a random number if that random number is smallest than smallest, we store that as the new smallest number then, after the loop, we print smallest, which is the lowest number we generated out of 100 attempts, 1-999
levia_tano
levia_tanoOP13mo ago
hmm but i dont think thats what i need i need the smallest amount of guesses the user needed not the smallest random number generated or am i reading your code wrong
Pobiega
Pobiega13mo ago
The concept is the same I'm not going to do your assignment for you, but you should be able to understand the code I showed and adapt it to work for your needs you dont use random numbers, so throw that part out. You dont loop 100 times, so throw that part out etc the core idea is to use if to check the new number against the current record before saving
levia_tano
levia_tanoOP13mo ago
yeah true thanks alot for your help! im gonna try and do it by myself you gave me the path to follow
Pobiega
Pobiega13mo ago
👍
levia_tano
levia_tanoOP13mo ago
nothing, i cant come up with anything i fell like i lack some knowledge about saving a value but i tried googling and didnt find anything can you give me a hint, i dont want you to solve the assignment just something because i dont feel like the if would work here
Pobiega
Pobiega13mo ago
// at the top
int rekord = 9999; // big number, so that the user will beat it easily
...
// where the player beat the game
if(guessesUsed < rekord)
{
rekord = guessesUsed;
}
...

//at the end of the game
Console.WriteLine($"Your best game used {rekord} guesses!");
// at the top
int rekord = 9999; // big number, so that the user will beat it easily
...
// where the player beat the game
if(guessesUsed < rekord)
{
rekord = guessesUsed;
}
...

//at the end of the game
Console.WriteLine($"Your best game used {rekord} guesses!");
levia_tano
levia_tanoOP13mo ago
tack så mycket för hjälpen
Pobiega
Pobiega13mo ago
vassego
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server