title: arithmetic expression Math.random in java
body: <p>the purpose of this program is to let the user try to guess a number up to 20 randomly chosen by the computer. The user inputs their guess. The computer then tells the user if their guess is too high or too low. The user keeps inputting their guess until theu guess correctly. Then they are told how many guesses it took them. The problem is that I am not understanding whether the arithmetic expression in the program generates random numbers from 1 to 20 because when I am inputting numbers from 1 to 20, the pop-up says 'number is too big'.</p>
  
  <pre><code>package pkTopic6Exercise13;
  import javax.swing.*;
  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.awt.Font;
  public class Topic6Exercise13 
  {
  private GuessingGame MyGuessingGame = new GuessingGame();
  private Topic6Exercise13()
  {//constructor      
  }
  public static void main(String[] args) 
  {
      Topic6Exercise13 myTopic6Exercise13 = new Topic6Exercise13();
      myTopic6Exercise13.go();
  }
  private void go()
  {
      T6Ex13GUIin myT6Ex13GUIin = new T6Ex13GUIin();
  }
  private class GuessingGame
  {
      private int NumToGuess=0;
      private int Guess=0;
      private int NumGuesses=0;
      private GuessingGame()
      {//constructor          
      }
      private void GenerateNum()
      {
          NumToGuess = 1+(int)(20*Math.random());
      }
      private String CheckNum()
      {
          NumGuesses = NumGuesses + 1;
          if (Guess &gt; NumToGuess)
          {
              JOptionPane.showMessageDialog(null, Guess + " is too BIG!");
              return "NotGuessed";
          }
          else if (Guess &lt; NumToGuess)
          {
              JOptionPane.showMessageDialog(null, Guess + " is too SMALL!");
              return "NotGuessed";
          }
          else
          {
              JOptionPane.showMessageDialog(null, "Well done! " + Guess +" is correct, it took " + NumGuesses + " goes.");
              return "Guessed";
          }
      }
  }
  private class T6Ex13GUIin extends JFrame
  {
      private JLabel lblGuess;
      private JTextField txfGuess;
      private JButton btnPickNumber;
      private JButton btnQuit;
  
      private T6Ex13GUIin()
      {//constructor
          Font fontDialog = new Font("Dialog", Font.BOLD,24);
          this.setSize(1000,500);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setLocation(100,0);
          this.setTitle("Guessing Game");
          this.setLayout(null);
          lblGuess = new JLabel("Type Guess:");
          lblGuess.setFont(fontDialog);
          lblGuess.setBounds(100,200,200,50);
          lblGuess.setVisible(false);
          this.add(lblGuess);
          txfGuess = new JTextField("");
          txfGuess.setBounds(300,200,100,50);
          txfGuess.setFont(fontDialog);
          txfGuess.setVisible(false);
          ReturnListener MyReturnListener = new ReturnListener();
          txfGuess.addKeyListener(MyReturnListener);
          this.add(txfGuess);
          ClickListener MyClickListener = new ClickListener();
          btnPickNumber = new JButton("Pick Number");
          btnPickNumber.setBounds(100,100,750,50);
          btnPickNumber.setFont(fontDialog);
          btnPickNumber.addActionListener(MyClickListener);
          this.add(btnPickNumber);
          btnQuit = new JButton("Quit");
          btnQuit.setBounds(750,300,100,50);
          btnQuit.setFont(fontDialog);
          btnQuit.addActionListener(MyClickListener);
          this.add(btnQuit);
          this.setVisible(true);
      }
      private class ClickListener implements ActionListener
      {
          private ClickListener()
          {//constructor              
          }
          public void actionPerformed(ActionEvent e)
          {               
              if (e.getSource() == btnPickNumber)
              {
                  MyGuessingGame.GenerateNum();
                  MyGuessingGame.NumGuesses=0;
                  btnPickNumber.setVisible(false);
                  lblGuess.setVisible(true);
                  txfGuess.setVisible(true);
                  txfGuess.setText("");
              }
              if (e.getSource() == btnQuit)
              {
                  System.exit(0);
              }
          }
      }
      private class ReturnListener implements KeyListener
      {
          private ReturnListener()
          {//constructor
          }
          public void keyPressed(KeyEvent e)
          {
          }
          public void keyReleased(KeyEvent e)
          {
          }
          public void keyTyped(KeyEvent e)
          {
              if (e.getKeyChar() == '\n')
              {
                  MyGuessingGame.Guess = 
                  Integer.parseInt(txfGuess.getText());
                  if (MyGuessingGame.CheckNum().equals("Guessed"))
                  {
                      lblGuess.setVisible(false);
                      txfGuess.setVisible(false);
                      btnPickNumber.setVisible(true);
                  }
                  else
                  {
                      txfGuess.setText("");
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>