这是我写的一个小程序,是用一个随机数和一个输入的数做比较的一个程序,程序里有一个重新开始的按钮,我想一按下重新开始按钮就可以重新取一个随机数,但是实现不了,各位高手能帮我看看应该怎么弄吗?谢谢你们了//这是一个小型的猜数字的程序。import java.awt.*;
import java.awt.event.*;public class Game extends Frame {
//定义了一些属性和组件
static double a = Math.round((Math.random()*100) / 1);
double tn;
int count = 0;
Frame f = new Frame("Game");
Panel p = new Panel();
Label la1 = new Label("这个数是从1-100的一个整数。");
Label la2 = new Label("你能猜到这个数是几吗?可以在下面的输入框中试试");
Label la3 = new Label("");
TextField t = new TextField("",15);
Button buttonok = new Button("确定");
Button buttonrestart = new Button("重新开始");
public Game() {
f.add(p);
p.add(la1);
p.add(la2);
p.add(t);
p.add(buttonok);
p.add(buttonrestart);
p.add(la3);
f.setSize(260,500);
f.setVisible(true);
//f.setLayout(new GridLayout(3,2,5,5));
/*f.add(la1,BorderLayout.NORTH);
f.add(la2,BorderLayout.NORTH);
f.add(t,BorderLayout.CENTER);
f.add(buttonok,BorderLayout.CENTER);
f.add(buttonrestart,BorderLayout.CENTER);
f.add(la3,BorderLayout.SOUTH);
f.show();*/
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
buttonok.addActionListener(new ButtonListenerok());
buttonrestart.addActionListener(new ButtonListenerrestart());
}
class ButtonListenerok implements ActionListener {
public void actionPerformed(ActionEvent e){
double a = Math.round((Math.random()*100) / 1);
try {
tn = (Double.parseDouble(t.getText()));
if(tn > 100) {
la3.setText("输入的数不能大于100");
}
else if(a == tn) {
la3.setText("恭喜你!你答对了");
}
else if(tn < a) {
count++;
la3.setText("你已经是第"+count+"输入了,不过别灰心,只是小了一点点");
}
else if(tn>a) {
count++;
la3.setText("你已经是第"+count+"输入了,不过别灰心,只是大了一点点");
}
}catch(Exception q){
t.setText("这里只能输入数字!");
}
}
}
class ButtonListenerrestart implements ActionListener {
public void actionPerformed(ActionEvent e) {
double a = Math.round((Math.random()*100) / 1);
try{
tn = (Double.parseDouble(t.getText()));
count = 0;
t.setText("");
la3.setText("重新开始");
}catch(Exception q) {
t.setText("这里只能输入数字!");
}
}
}
public static void main (String [] args) {
Game g = new Game();
System.out.println(a);
}
}
import java.awt.event.*;public class Game extends Frame {
//定义了一些属性和组件
static double a = Math.round((Math.random()*100) / 1);
double tn;
int count = 0;
Frame f = new Frame("Game");
Panel p = new Panel();
Label la1 = new Label("这个数是从1-100的一个整数。");
Label la2 = new Label("你能猜到这个数是几吗?可以在下面的输入框中试试");
Label la3 = new Label("");
TextField t = new TextField("",15);
Button buttonok = new Button("确定");
Button buttonrestart = new Button("重新开始");
public Game() {
f.add(p);
p.add(la1);
p.add(la2);
p.add(t);
p.add(buttonok);
p.add(buttonrestart);
p.add(la3);
f.setSize(260,500);
f.setVisible(true);
//f.setLayout(new GridLayout(3,2,5,5));
/*f.add(la1,BorderLayout.NORTH);
f.add(la2,BorderLayout.NORTH);
f.add(t,BorderLayout.CENTER);
f.add(buttonok,BorderLayout.CENTER);
f.add(buttonrestart,BorderLayout.CENTER);
f.add(la3,BorderLayout.SOUTH);
f.show();*/
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
});
buttonok.addActionListener(new ButtonListenerok());
buttonrestart.addActionListener(new ButtonListenerrestart());
}
class ButtonListenerok implements ActionListener {
public void actionPerformed(ActionEvent e){
double a = Math.round((Math.random()*100) / 1);
try {
tn = (Double.parseDouble(t.getText()));
if(tn > 100) {
la3.setText("输入的数不能大于100");
}
else if(a == tn) {
la3.setText("恭喜你!你答对了");
}
else if(tn < a) {
count++;
la3.setText("你已经是第"+count+"输入了,不过别灰心,只是小了一点点");
}
else if(tn>a) {
count++;
la3.setText("你已经是第"+count+"输入了,不过别灰心,只是大了一点点");
}
}catch(Exception q){
t.setText("这里只能输入数字!");
}
}
}
class ButtonListenerrestart implements ActionListener {
public void actionPerformed(ActionEvent e) {
double a = Math.round((Math.random()*100) / 1);
try{
tn = (Double.parseDouble(t.getText()));
count = 0;
t.setText("");
la3.setText("重新开始");
}catch(Exception q) {
t.setText("这里只能输入数字!");
}
}
}
public static void main (String [] args) {
Game g = new Game();
System.out.println(a);
}
}
这样不就可以完成了么?
可是为什么不行呢?