Guess a number program with Java

前端 未结 6 1636
情话喂你
情话喂你 2021-01-28 12:06

I am trying to create a program in Java in which the computer randomly guesses a number between 1-100 and allows the user to guess to the number.

If the number is lower

6条回答
  •  终归单人心
    2021-01-28 12:39

    import java.util.Random;
    
    public static void main(String[] args)
    {
    
        int a = 1 + (int)(Math.random()*99);
        int guess = 0;
        int count = 0;
    
        while(guess != a)
        {
            guess = Integer.parseInt(JOptionPane.showInputDialog("Enter a Number"));
            count++;
            if (guess > a)
            {
             JOptionPane.showMessageDialog(null,"lower!");
            }
           else if (guess < a)
            {
             JOptionPane.showMessageDialog(null,"higher!");
            }
        }
             JOptionPane.showMessageDialog(null,"Congratulations. You guessed the number with " +count+ " tries");
        }
    

    }

提交回复
热议问题