❔ 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
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 conditionsconditions are boolean expressions, and those can always be combined into another boolean expression as SinFluxx showed above 🙂
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
Sure
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
sure
thats easy enough, but how to implement it depends on your code
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
kinda, yeah
you'd just check if the stored value is larger than your current value before overwriting it
yeah true should i share mine? its in swedish but im going to translate it
I think its fine 🙂
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);
}
$code
its very confusing i know haha
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/wrap it in a code block please
so its readable
okok
!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}");
not sure why the bot failed, but the code is fine
im trying to understand your code
okay, so you must declare
rekord
before you can use it
sure, anything in particular you dont understand?int smallest is basically rekord?
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-999hmm 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
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 savingyeah true thanks alot for your help! im gonna try and do it by myself you gave me the path to follow
👍
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
tack så mycket för hjälpen
vassego
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.