这是一个通过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();
}
}
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();
}
}
解决方案 »
- struts2下的“Exception starting filter struts2”错误的问题
- 复杂的离职问题
- [分享]自定义格式生成随机HASH
- 如何用replace把/换成//如:Str.replaceAll("\","\\")
- 问一个很菜的java传值问题。
- 为什么我写的applet在网页下看不到效果,在命令行却可以?
- 如何使弹出广告不受上网助手拦截?
- getClass().getResourceAsStream("/a.xml")总是报空指针,求解
- 类型转化 时候的 精度丢失问题!
- 关于包种类的归属
- 软件开发,有前台用户界面(GUI)和后台逻辑等部分。当今的软件GUI很多都是很绚丽(是用专业UI软件设计的),那么用UI设计的软件界面与后台的程序编程是怎么整合到一块的呢(两者用的不同软件做的啊)??
- 再问public protected 和 private
这个应该是个实例变量,不应该是个局部变量
每猜一次都有个提示,如果不对继续猜,直到猜对为止
每猜一次done都应该=true或跳出循环
下面是我编的,我现在想用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);
}
}
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)删掉。
http://blog.csdn.net/YidingHe/archive/2009/05/03/4145644.aspx这里是编写过程的录像:
http://yidinghesarchives.googlecode.com/files/GuessNumber.mkv