I am trying to run a do while loop where the condition for running is when a string that is "yes". I initialize it as yes and then I reassign it from a scanner, but if I reassign it to "yes" the loop ends, when I need it to keep running. What am I doing wrong?
Here is the program. I know its probably inefficient but this was how I know how to do it(I’m still learning)
package Program06;
import java.util.Scanner;
public class Program06 {
public static void main(String[] args) {
System.out.println("Welcome to Computer Dice");
System.out.println("-----------------------------------------");
System.out.println("You will first roll your dice");
System.out.println("");
System.out.println("Next the outcome of your roll will");
System.out.println("be determined:");
System.out.println("");
System.out.println("");
System.out.println("any Quad and you Win");
System.out.println("any Triple and you Win");
System.out.println("any TwoPair and you Win");
System.out.println("any HighPair ( 4’s )and you Win");
System.out.println("anything else and you Lose");
System.out.println("-----------------------------------------");
System.out.println("");
int d1, d2, d3, d4;
String playAgain = "yes";
Scanner play = new Scanner(System.in);
int count = 0;
String roundOut;
int quadCount = 0;
int tripleCount = 0;
int twoPairC = 0;
int highPairC = 0;
int lossCount = 0;
int winCount = 0;
do {
d1 = (int)(Math.random() * 4) + 1;
d2 = (int)(Math.random() * 4) + 1;
d3 = (int)(Math.random() * 4) + 1;
d4 = (int)(Math.random() * 4) + 1;
count++;
System.out.println("Player");
System.out.println("----------");
System.out.println("You rolled:" + d1 + " " + d2 + " " + d3 + " " + d4);
if (d1 == d2 && d2 == d3 && d3 == d4) {
roundOut = "You got a Quad, you win!";
quadCount++;
winCount++;
}
else if ((d1 == d2 && d2 == d3) || (d2 == d3 && d3 == d4) ||(d3 == d4 && d4 == d1) || (d4 == d1 && d1 == d2)) {
roundOut = "You got a Triple, you win!";
tripleCount++;
winCount++;
}
else if ((d1 == d2 && d3 == d4 && !(d1 == d4)) || (d1 == d4 && d3 == d2 && !(d1 ==d2)) || (d1 == d3 && d2 == d4 && !(d1 == d2))) {
roundOut = "You got a Two Pair, you win!";
twoPairC++;
winCount++;
}
else if ((d1 == d2 && d1 == 4)||(d1 == d3 && d1 == 4)||(d1 == d4 && d1 == 4)||(d2 == d3 && d2 == 4)||(d2 == d4 && d2 == 4)||(d3 == d4 && d3 == 4)) {
roundOut = "You got a High Pair, you win!";
highPairC++;
winCount++;
}
else {
roundOut = "You lost this round.";
lossCount++;
}
System.out.println(roundOut);
System.out.println("Do you want to play again? Yes or no?");
playAgain = play.nextLine();
playAgain.toLowerCase();
playAgain.toString();
System.out.println(playAgain);
} while (playAgain == "yes");
System.out.println("Computer Dice Results");
System.out.println("---------------------");
System.out.println("You played " + count + " rounds");
System.out.println("");
System.out.println("Rounds Won: " + winCount);
System.out.println("You rolled " + quadCount + " quads, " + tripleCount + " triples, " + twoPairC + " two pairs, and " + highPairC + " high pairs.");
System.out.println("Rounds lost: " + lossCount);
System.out.println("---------------------");
play.close();
}
}
>Solution :
you should compare two string with equal(), instead of == which compares two string memory address. in your case, you should use playAgain.equal("yes").
the reason why loop keep running when you initialize playAgain with "yes" is string pool caching same string, but string read from scanner will create a new string object in any event instead of using the cache from string pool.
i recommend you to learn the different between == and equal, things about string pool is an advanced knowlegde, and beeginner don’t need to master