我有以下程序,用户的编号在0到100之间。计算机必须尝试使用二进制搜索算法进行猜测。
运行时会创建.class文件,但是如何将这些类放在单独的java文件中。
public class GuessGame extends JFrame //implements ActionListener
{
private JButton newGameButton, highButton, lowButton, correctButton, exitButton;
private JTextField guessBox;
private JLabel initialTextLabel, enterLabel;
private JTextArea commentTextArea;
private JScrollPane scroll;
//guess is equal to half the highest number
private int guess=50, high = 101, low = 0, tries;
public GuessGame()
{
super("Guessing Game");
//add buttons etc.
//set the layout manager
setLayout(new FlowLayout());
//add components
//set default jframe size
setSize(400, 300);
newGameButtonHandler nghandler = new newGameButtonHandler();
newGameButton.addActionListener(nghandler);
exitButtonHandler exithandler = new exitButtonHandler();
exitButton.addActionListener(exithandler);
highButtonHandler higherhander = new highButtonHandler();
highButton.addActionListener(higherhander);
lowButtonHandler lowerhander = new lowButtonHandler();
lowButton.addActionListener(lowerhander);
correctButtonHandler correcthander = new correctButtonHandler();
correctButton.addActionListener(correcthander);
}//end of GuessGame constructor
//highButtonHandler class
class highButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
high = guess;
guess = low + (guess - low) / 2;
tries++;
commentTextArea.append("Is the number " + guess + " too small, too high or correct.\n");
}
}//end of high class