这是一个通过GUI现实猜数字小游戏的程序,    通过输入数字按下guess按键,在result Textfield输出相应结果现在程序有写问题   如何控制循环?如何实现重新游戏的功能?import javax.swing.*;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class CH06EX15 extends JFrame {
   
private JLabel GuessL,ResultL; private JTextField GuessTF, ResultTF;    private JButton GuessB, exitB,playB;    private GuessButtonHandler gbHandler;
    private PlayagainButtonHandler pbHandler;
    private ExitButtonHandler ebHandler;
    
    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;
    
    public CH06EX15()
    {
     GuessL = new JLabel("Enter the number you guess: ",
                 SwingConstants.RIGHT);        ResultL = new JLabel("Result: ",
                   SwingConstants.RIGHT);    //Create four text fields
       GuessTF = new JTextField(10);
       ResultTF = new JTextField(10);    //create Guess Button
      GuessB = new JButton("Guess");
      gbHandler = new GuessButtonHandler();
      GuessB.addActionListener(gbHandler);    //Create Exit Button
      exitB = new JButton("Exit");
      ebHandler = new ExitButtonHandler();
      exitB.addActionListener(ebHandler);   //Create play again Button
     playB= new JButton("play again");
     pbHandler = new PlayagainButtonHandler();
     playB.addActionListener(pbHandler);    //Set the title of the window
setTitle("Guess Game");    //Get the container
Container pane = getContentPane();    //Set the layout
pane.setLayout(new GridLayout(4,3));    //Place all items created
pane.add(GuessL);
pane.add(GuessTF);
pane.add(ResultL);
pane.add(ResultTF);
pane.add(GuessB);
pane.add(playB);
pane.add(exitB);  //set the size of the window and display it
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}private class GuessButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int num,guess,diff,i=0;
String outputStr="";
num=(int)(Math.random()* 100);
boolean done;
done = false;
while(!done)
    {
    
     // Input your guess
guess = Integer.parseInt(GuessTF.getText());
i=i+1;
diff=num-guess;

if (diff==0)
{
outputStr= "You guessed the correct number."+"\n"
+"The times you guess are "+ i +"\n"
+"Do you want to play it again?";

done= true;

}
else
{if(Math.abs(diff) >= 50)
if(guess>num)
       outputStr= "The guess is very high.";
else  
outputStr= "The guess is very low.";
    else
     if((Math.abs(diff) < 50)&&(Math.abs(diff) >= 30)) 
     if(guess>num)
           outputStr= "The guess is high.";
     else
     outputStr= "The guess is low.";
        else
         if((Math.abs(diff) < 30)&&(Math.abs(diff) >= 15))
         if(guess>num)
         outputStr= "The guess is 15 to 30 higher.";
         else
         outputStr= "The guess is 15 to 30 lower.";
         else
         if(guess>num)
         outputStr="The guess is 1 to 15 higher.";
         else
         outputStr= "The guess is 1 to 15 lower.";

     }

done= false;
ResultTF.setText("" + outputStr);

    } //end while
ResultTF.setText("" + outputStr);
}
}private class PlayagainButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e){

}
}private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

public static void main(String[] args)
{
    CH06EX15 rectObject = new CH06EX15();
}
}

解决方案 »

  1.   

    我怎么感觉你的这个代码这么熟悉啊?那个Math.abs方法。你想要实现循环控制的话,很简单,在人家猜对了的时候,弹出一个提示框:让人家再猜,之后你再随机生成一个数,再比对一个简单的办法,当这个人输入“Q”的时候就退出,否则就一直while循环。不难的,动手吧,呵呵。
      

  2.   

    num=(int)(Math.random()* 100); 
    这个应该是个实例变量,不应该是个局部变量
    每猜一次都有个提示,如果不对继续猜,直到猜对为止
    每猜一次done都应该=true或跳出循环
      

  3.   

    我需要对这个 猜数字游戏说明下子:  系统随机生成1-100的数字,让用户去猜,直到猜对~~  要统计处猜的次数和有重新开始游戏的功能~~
    下面是我编的,我现在想用GUI去优化这个程序~~ 不知道怎么优化好~~  我的想法是输入后点击guess,将结果在result Textfield输出~~ 但是实现的时候有很多问题,所以想求助下import javax.swing.JOptionPane;public class CH05EX15 {

    public static void main(String[] args) 
        {

    //declare the variables

    int num,guess,diff,i=0;
    String inputStr,outputStr="";
    num=(int)(Math.random()* 100);
    boolean done;

        done= false;

        while(!done)
        {
        
         // Input your guess
    inputStr = JOptionPane.showInputDialog
            ("Enter the number you guess.");

    //if the input is wrong, let the system exit
    if(inputStr == null||inputStr.equals(""))
    System.exit(0);
        
    guess = Integer.parseInt(inputStr);
    i=i+1;
    diff=num-guess;

    if (diff==0)
    {
    outputStr= "You guessed the correct number."+"\n"
    +"The times you guess are "+ i +"\n"
    +"Do you want to play it again?";

    int response= JOptionPane.showConfirmDialog(null, outputStr,"Thank You!", 
    JOptionPane.YES_NO_OPTION);
    if(response == JOptionPane.YES_OPTION)
    {
    //reset i and num
    num=(int)(Math.random()* 100);
    i=0;
    done = false;
    }
    else
        done= true;
    }
    else
    {if(Math.abs(diff) >= 50)
    if(guess>num)
           outputStr= "The guess is very high.";
    else  
    outputStr= "The guess is very low.";
        else
         if((Math.abs(diff) < 50)&&(Math.abs(diff) >= 30)) 
         if(guess>num)
               outputStr= "The guess is high.";
         else
         outputStr= "The guess is low.";
            else
             if((Math.abs(diff) < 30)&&(Math.abs(diff) >= 15))
             if(guess>num)
             outputStr= "The guess is 15 to 30 higher.";
             else
             outputStr= "The guess is 15 to 30 lower.";
             else
             if(guess>num)
             outputStr="The guess is 1 to 15 higher.";
             else
             outputStr= "The guess is 1 to 15 lower.";

    JOptionPane.showMessageDialog(null, outputStr,
    "Result",
    JOptionPane.INFORMATION_MESSAGE);
         }

        } //end while
    System.exit(0);             
    }
    }
      

  4.   

    具体逻辑我就先不说了,先说说guess这个按钮的处理函数:
     
    private class GuessButtonHandler implements ActionListener 

    public void actionPerformed(ActionEvent e) 

    int num,guess,diff,i=0; 
    String outputStr=""; 
    num=(int)(Math.random()* 100); 
    boolean done; 
    done = false; 
    while(!done) 
        { 
        
        // Input your guess 
    guess = Integer.parseInt(GuessTF.getText()); 
    i=i+1; 
    diff=num-guess; if (diff==0) 
    这里怎么能家while循环呢,用户点一次按钮,就是猜一次,你就应该给用户返回一次提示,
    然后用户再改数猜,
    如果你一点按钮就进入循环(实际上是个死循环,因为用户已经没有机会再输入新的数字了)。
    所以,先把这里的while(!done)删掉。
      

  5.   

    正常情况下,这种游戏都有一个start按钮,可以让这个按钮进行随机数的生成,让后每点击一次guess就进行一下判断。而且从新游戏也调用start按钮的方法。
      

  6.   

    给楼主参考一下:
    http://blog.csdn.net/YidingHe/archive/2009/05/03/4145644.aspx这里是编写过程的录像:
    http://yidinghesarchives.googlecode.com/files/GuessNumber.mkv