Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I loop my dice game program 3 times?
    primarykey
    data
    text
    <p>I'm trying to create a game that rolls 2 set of dice, three times in a row. It has the user guess a number between 2-12 just once. If that one guess matches any of the three rolls he/she wins, otherwise he/she loses. I have another class to display results and I have a counter for how many loops it's been through. It comes out 0 if the user correctly guessed it, otherwise it comes out as 1. I'm guessing the loop just loops once so if anyone can point out what I'm doing wrong to make it so it loops three times(and stopping if the user gets the answer right).</p> <pre><code>import javax.swing.JOptionPane; /** * @author Marcus * */ public class Dice { int randomDieNum1;//random number generator for dice int randomDieNum2;//random number generator for dice private final int MINVALUE1 = 1, //minimum die value MAXVALUE1 = 6;//maximum die value private final int MINVALUE2 = 1, //minimum die value MAXVALUE2 = 6;//maximum die value int userNum = Integer.parseInt(JOptionPane.showInputDialog(null, "Guess a number between 1-12", "Guess a Number", JOptionPane.INFORMATION_MESSAGE));//gets user input String result ; //results int start = 0 ; //counter to see how many turns were taken public Dice() { for (int i = 1 ; i &lt;= 3; i++) randomDieNum1 = ((int)(Math.random()* 100) % MAXVALUE1 + MINVALUE1); randomDieNum2 = ((int)(Math.random()* 100) % MAXVALUE2 + MINVALUE2); int total = randomDieNum1 + randomDieNum2; if (randomDieNum1 + randomDieNum2 != userNum) { result = "You did not guess the \n number correctly"; ++ start; } else if (randomDieNum1 + randomDieNum2 == userNum) { result = randomDieNum1 + "+" + randomDieNum2 + "=" + total + "\n" + "You guessed the number correctly"; } else { result = "You Did not guess the number correctly"; } } public String get() //used in another class to display count { String temp; temp = "" + start; return temp; } } </code></pre> <p><strong>EDIT</strong> Thanks guys. I added both suggestions and added a break to stop the loop after the user gets the answer right. This is what it looks like:</p> <pre><code>public Dice() { for (int i = 1 ; i &lt;= 3; i++) {randomDieNum1 = ((int)(Math.random()* 100) % MAXVALUE1 + MINVALUE1); randomDieNum2 = ((int)(Math.random()* 100) % MAXVALUE2 + MINVALUE2); int total = randomDieNum1 + randomDieNum2; if (randomDieNum1 + randomDieNum2 == userNum) {result = randomDieNum1 + "+" + randomDieNum2 + "=" + total + "\n" + "You guessed the number correctly"; ++ turns; // break; //stops the loop if condition is meet } else if(randomDieNum1 + randomDieNum2 != userNum) { result = "You did not guess the \n number correctly\n\n"; ++ turns; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload