title: Wrong response in order to users input?
body: <p>I have created a game in Java where the user get to choose 1, 2 or 3 dices and wins if the sum equals to 12. The game continues until the user type in the charater q. For every other character, texts or numbers (except 1,2, 3) the game says unvalid value and prompts the user again to choose a dice or type q to quit.</p>
  <ul>
  <li>If the sum is &gt; 12 and the user has used all three dices, it counts as a loss.</li>
  <li>When the sum = 12, it counts as a win.</li>
  <li>When the user has won or used all 3 dices, a new round begin automatically and the user is now free to use all dices again.</li>
  <li>The user can only use each dice once in each round, so the user cant for example choose dice no 1 three times in a row.</li>
  </ul>
  <p>I think that is where the problem lies. Sometimes when I run the program it doesnt act like its supposed to.
  If I for example types 1 and then types a random character (for example h) it says ”unvalid value” and then that ”I already has chosen dice no 1”.</p>
  <p>How do I fix it?</p>
  <p><a href="https://i.stack.imgur.com/Ig8DZ.png" rel="nofollow noreferrer">This is a picture of what the system response can look like</a></p>
  <pre><code>import java.util.Scanner;
  
  public class Tärningsspel
  {
    public static void main(String[] args)
     {
        
        //Declaration of variables int diceNo1;
        int diceNo2;
        int diceNo3;
  
        int sum;
        int numberOfWins;
        int numberOfLosses;
        int currentTurn;
  
        int userNumber;
        String userText;
        char userCharacter;
  
        String textToQuit;
  
        final int MAX_TURNS = 3;
        final int WINNING_SUM = 12;
        final char CHARACTER_TO_QUIT = 'q';
  
        // Values when the game starts
        userCharacter = 'a';
        userNumber = 0;
        userText = &quot;a&quot;;
        diceNo1 = 0;
        diceNo2 = 0;
        diceNo3 = 0;
        sum = 0;
        currentTurn = 0;
        numberOfWins = 0;
        numberOfLosses = 0;
        textToQuit = &quot;q&quot;;
  
        System.out
              .println(&quot;Welcome to the game-12! You are going to choose 1-3 dices and try to get the total sum of 12&quot;);
  
        Scanner userInput = new Scanner(System.in);
  
        do
        {
           System.out.println(&quot;Type which dice (1-3) you choose. Type q to quit the game&quot;);
  
           if (userInput.hasNextInt())
           {
              userNumber = userInput.nextInt();
  
              if (userNumber &gt; 3 || userNumber &lt; 1)
                 System.out.println(&quot;Unvalid value&quot;);
           }
           
           else 
           {
              userText = userInput.next();
           
           if (userText.equals(textToQuit)) 
           {
              System.out.println(&quot;The game has quit&quot;);
              userCharacter = CHARACTER_TO_QUIT;
           }
           
           else 
              System.out.println(&quot;Unvalid value&quot;);
           }
              
           switch (userNumber)
           {
           case 1:
           {
              if (diceNo1 != 0)
                 System.out.println(&quot;You has already chosen dice no 1&quot;);
  
              else
              {
                 diceNo1 = (int) (Math.random() * 6) + 1;
  
                 System.out.println(&quot;You choosed dice no 1 and rolled number &quot; + diceNo1);
  
                 currentTurn = currentTurn + 1;
                 sum = diceNo1 + diceNo2 + diceNo3;
  
                 if (sum == WINNING_SUM)
                    numberOfWins = numberOfWins + 1;
  
                 else if (currentTurn == MAX_TURNS &amp;&amp; sum &gt; WINNING_SUM)
                    numberOfLosses = numberOfLosses + 1;
  
                 System.out.println(&quot;Throw nr:&quot; + currentTurn + &quot; Sum:&quot; + sum + &quot; #Wins:&quot; + numberOfWins + &quot; #Losses:&quot;
                       + numberOfLosses);
  
                 if (currentTurn == MAX_TURNS || sum == WINNING_SUM)
                 {
                    diceNo1 = 0;
                    diceNo2 = 0;
                    diceNo3 = 0;
                    sum = 0;
                    currentTurn = 0;
                    System.out.println(&quot;New round starts!&quot;);
                 }
              }
           }
              break;
  
           case 2:
           {
              if (diceNo2 != 0)
                 System.out.println(&quot;You has already chosen dice no 2&quot;);
  
              else
              {
                 diceNo2 = (int) (Math.random() * 6) + 1;
  
                 System.out.println(&quot;You choosed dice no 2 and rolled number &quot; + diceNo2);
  
                 currentTurn = currentTurn + 1;
                 sum = diceNo1 + diceNo2 + diceNo3;
  
                 if (sum == WINNING_SUM)
                    numberOfWins = numberOfWins + 1;
  
                 else if (currentTurn == MAX_TURNS &amp;&amp; sum &gt; WINNING_SUM)
                    numberOfLosses = numberOfLosses + 1;
  
                 System.out.println(&quot;Throw nr:&quot; + currentTurn + &quot; Sum:&quot; + sum + &quot; #Wins:&quot; + numberOfWins + &quot; #Losses:&quot;
                       + numberOfLosses);
  
                 if (currentTurn == MAX_TURNS || sum == WINNING_SUM)
                 {
                    diceNo1 = 0;
                    diceNo2 = 0;
                    diceNo3 = 0;
                    sum = 0;
                    currentTurn = 0;
                    System.out.println(&quot;New round starts!&quot;);
                 }
              }
           }
              break;
  
           case 3:
           {
              if (diceNo3 != 0)
                 System.out.println(&quot;You has already chosen dice no 3&quot;);
  
              else
              {
                 diceNo3 = (int) (Math.random() * 6) + 1;
  
                 System.out.println(&quot;You choosed dice no 3 and rolled number &quot; + diceNo3);
  
                 currentTurn = currentTurn + 1;
                 sum = diceNo1 + diceNo2 + diceNo3;
  
                 if (sum == WINNING_SUM)
                    numberOfWins = numberOfWins + 1;
  
                 else if (currentTurn == MAX_TURNS &amp;&amp; sum &gt; WINNING_SUM)
                    numberOfLosses = numberOfLosses + 1;
  
                 System.out.println(&quot;Throw nr:&quot; + currentTurn + &quot; Sum:&quot; + sum + &quot; #Wins:&quot; + numberOfWins + &quot; #Losses:&quot;
                       + numberOfLosses);
  
                 if (currentTurn == MAX_TURNS || sum == WINNING_SUM)
                 {
                    diceNo1 = 0;
                    diceNo2 = 0;
                    diceNo3 = 0;
                    sum = 0;
                    currentTurn = 0;
                    System.out.println(&quot;New round starts!&quot;);
                 }
              }
           }
              break;
           }
        } while (userCharacter != CHARACTER_TO_QUIT);
     }
  }
  </code></pre>