Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exiting do-while loop with string input in java

Tags:

java

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.

like image 505
stigan Avatar asked Mar 31 '26 15:03

stigan


1 Answers

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"));
like image 117
John Kugelman Avatar answered Apr 02 '26 05:04

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!