When I run this code for the quad, (as stated in the comments) it prints out
“Quad- Congrats, you win!” and it also prints out
"Junker- Sorry, you lose!”
It has this same problem for the triple, where it will print the triple statement and also the junker statement after it.
However, for the two-pair, it only prints "Two-Pair- Congrats you win!” and no junker, which is what I want.
So my question is that why does it still print the junker line after the “congrats you win” even though the junker line is in an else statement? It should only be executing the else if none of the other statements are true.
import java.util.Scanner; public class ProblemwithProgram06 { public static void main(String []args) { Scanner stdIn= new Scanner(System.in); int number; int reroll=0; int wins=0; int loses=0; int rounds=0; String play; int d1, d2, d3, d4; // Instructions for game System.out.println(" Welcome to Computer Dice "); System.out.println("---------------------------------"); System.out.println("You will first roll your dice \n"); System.out.println("You are then allowed to re-roll up to \ntwo of your dice \n"); System.out.println("Finally the outcome of your roll is \nto be determined: \n"); System.out.println("Any Quad and you receive 108 Wins"); System.out.println("Any Triple and you receive 6 Wins"); System.out.println("Any Two-Pair and you receive 4 Wins"); System.out.println("Anything else and you receive 1 Lose"); System.out.println("-------------------------------------\n \n"); do //beginning of do loop { System.out.println(" Player "); System.out.println("----------"); d1 = (int)(Math.random() * 6) + 1; d2 = (int)(Math.random() * 6) + 1; d3 = (int)(Math.random() * 6) + 1; d4 = (int)(Math.random() * 6) + 1; System.out.println(" " + d1 + " " + d2 + " " + d3 + " " + d4 + "\n"); do { System.out.print("Please enter the number of dice to re-roll [0,2] : "); number=stdIn.nextInt(); System.out.print("\n"); } while (number >2 || number <0); for (int i=1; i <= number; ++i) { //beginning of for loop System.out.print(""); do { System.out.print("Please enter the number of the die to re-roll [1,4] : "); reroll=stdIn.nextInt(); System.out.print("\n"); } while (!(reroll >= 1 && reroll <= 4)); if (reroll == 1) { d1 = (int)(Math.random() * 6) + 1; } if (reroll == 2) { d2 = (int)(Math.random() * 6) + 1; } if (reroll == 3) { d3 = (int)(Math.random() * 6) + 1; } if (reroll == 4) { d4 = (int)(Math.random() * 6) + 1; } } //end of for loop do { System.out.println(" Player "); System.out.println("----------"); System.out.println(" " + d1 + " " + d2 + " " + d3 + " " + d4 + "\n"); break; } while (number >0); //**************************************************** //This is the start of my problem //Quad if (d1==d2 && d2==d3 && d3==d4) { System.out.println("Quad- Congrats, you win! \n"); wins= wins +108; } // Two-Pair else if ((d1==d2 && d2!=d3 && d3==d4) || (d1==d3 && d3!=d2 && d2==d4) || (d1==d4 && d4!=d3 && d3==d2)) { System.out.println("Two-Pair- Congrats you win! \n"); wins= wins +4; } //Triple else if ((d1==d2 && d2==d3 && d4!=d1) || (d1==d2 && d2==d4 && d3!=d2) || (d1==d3 && d3==d4 && d2!=d3) || (d2==d3 && d3==d4 && d1!=d4)) { System.out.println("Triple- Congrats you win! \n"); wins= wins +6; } //Junker else { System.out.println("Junker- Sorry, you lose! \n"); loses= loses +1; } // This is the end of the problem //***************************************** do { System.out.print("Do you wish to play again [y, n] : "); play=stdIn.next(); } while (!play.equals("y") && (!play.equals("n"))); ++rounds; System.out.print("\n \n"); } while (play.equals ("y")); //end of do loop System.out.println("Computer Dice Results"); System.out.println("-----------------------"); System.out.println("You played " + rounds + " rounds"); System.out.println("Wins: " + wins); System.out.println("Loses: " + loses); System.out.println("-----------------------"); stdIn.close(); } }