import java.awt.*;
import java.awt.event.*;public class a extends Frame implements ActionListener{
private int theReal=46,times=0;
private boolean notClearOutput=true;
private Label time=new Label("次数:");
private Label comment=new Label("评论:");
private Button enter=new Button("确定/重猜");
private TextField _time=new TextField(2);
private TextField output=new TextField(16);
private TextField _comment=new TextField(16);
private Label input=new Label("请输入你猜的数字");
private void setup(){
Panel top=new Panel();
top.setLayout(new FlowLayout());
top.add(input);top.add(output);top.add(enter);
Panel middle=new Panel();
middle.add(time);middle.add(_time);middle.add(comment);middle.add(_comment);
setLayout(new BorderLayout());
add("North",top);add("Center",middle);
}
public a(){
super("猜数字游戏");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setup();
setSize(400,300);
setVisible(true);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if((e.getSource()==enter)&&(!output.getText().equals(""))&¬ClearOutput){
if(Integer.valueOf(output.getText())==theReal){
times+=1;
output.setText("哈哈,你猜对了耶!");
if(times<=5){
_comment.setText("Yout're so clever!");
}
else if(times>5&×<=10){
_comment.setText("You're not so lucky!");
}
else{
_comment.setText("You need more practice!");
}
_time.setText(String.valueOf(times));
}
if(Integer.valueOf(output.getText())>theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太大了!");
}
if(Integer.valueOf(output.getText())<theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太小了!");//此句为执行,为什么啊?
}
}
if((e.getSource()==enter)&&(!output.getText().equals(""))&&!notClearOutput){
output.setText("");
output.requestFocus();
notClearOutput=!notClearOutput;
}
}
public static void main(String args[]){
a g=new a();
}
}这是个诡异的东西,为什么我输入的数字若大于被猜的数字有提示,而如果输入的数字小于被猜数字,则什么提示都没有。但是计数器times有变化。请教!
解决方案 »
- 请问能在GridLayout的指定位置添加一个组件吗?
- 怎么把1到9999的数字转成 4位字符串,左边补0 如 0001 0002 0003
- [求助]关于继承的问题
- 請問一個zip版的tomcat配置問題
- 为什么最大float数是0x7f800000 ???
- Java中字符串匹配
- 紧急求援:如何得到一double型数与一分数之积(结果的小数位数与double型数的小数位数相同)
- help!!!
- 如何查询得到系统数据源的列表?
- 我装了Visualage for Java 4.0 pro, 但是不知道为什么帮助文件都不能看,请问是不是下载的没有帮助?
- java中的float与double
- MySql + Java请教。。
if (Integer.valueOf(output.getText()) < theReal) {
times += 1;
notClearOutput = !notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太小了!");//此句为执行,为什么啊?
}
}
if ((e.getSource() == enter) && (!output.getText().equals(""))
&& !notClearOutput) {
output.setText("");
output.requestFocus();
notClearOutput = !notClearOutput;
}
notClearOutput = !notClearOutput;
将notClearOutput 复制成了false;
然后if ((e.getSource() == enter) && (!output.getText().equals(""))
&& !notClearOutput) {
条件成立,将文本框设置成了output.setText("");
public void actionPerformed(ActionEvent e){
if((e.getSource()==enter)&&(!output.getText().equals(""))&¬ClearOutput){
if(Integer.valueOf(output.getText())==theReal){
times+=1;
output.setText("哈哈,你猜对了耶!");//这里,看到了没
if(times<=5){
_comment.setText("Yout're so clever!");
}
else if(times>5&×<=10){
_comment.setText("You're not so lucky!");
}
else{
_comment.setText("You need more practice!");
}
_time.setText(String.valueOf(times));
}
if(Integer.valueOf(/*还有这里,看到了没 start*/output.getText()/* end*/)>theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太大了!");
}
if(Integer.valueOf((/*还有这里*/output.getText())<theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太小了!");//此句为执行,为什么啊?
}
}
if(Integer.valueOf(output.getText())>theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太大了!");
return;
}
if(Integer.valueOf(output.getText())<theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太小了!");//此句为执行,为什么啊?
return;
}
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太大了!");
return;
}
if(Integer.valueOf(output.getText())<theReal){
times+=1;
notClearOutput=!notClearOutput;
_time.setText(String.valueOf(times));
output.setText("Sorry,你猜的数太小了!");//此句为执行,为什么啊?
return;
}
要return一下