I was trying to write the following code to allow continuous coin toss and exits when E is entered. Not sure if do-while loop is the correct way to do it continuously or i should use another method.
do {
guess = sc.next();
tossGenerator(guess);
}while(!guess.equals("E")||!guess.equals("e"));
So, did I phrase the code wrongly because I can't get out of the do loop or a different method should be used. Please help. Thanks.
Change && to ||:
} while (!guess.equals("E") && !guess.equals("e"));
Or rearrange it like this:
} while (!(guess.equals("E") || guess.equals("e")));
Alternatively you can use String.equalsIgnoreCase() and eliminate the conjunction:
} while (!guess.equalsIgnoreCase("e"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With