JAVA程序设计——猜数字游戏
跨越百年的美丽教案-贡献的意思
          JAVA程序设计——猜数字游戏 
1.实训项目的内容 
程序运行时自动产生一个1-100之间的随机数,让游戏者来猜这个数。当从键盘接收到游戏
者输入
的数据后,程序给出的相应的提示信息,游戏者根据提示不断从键盘输入数据,直到
猜中。另外程序还提
供了“重新开始”和“退出”的功能,可供游戏者重复进行游戏。 
2.实训项目要求 
1.
建立Java程序,使用键盘输入流提供用户输入所猜数据; 
2.
使用()产生一个100以内的随机数; 
3. 使用一个循环从键盘输入数据,并和产生的随机数判断
是否大小关系,给出相应提示,
循环结束条件为猜中产生的随机数; 
4.
判断是否继续游戏;要求用户输入信息; 
5. 添加外层循环判断是否继续游戏; 
6.
在项目报告中说明键盘输入的基本语句。 
7. 在项目报告中写出for语句的执行过成。
8. 在项目报告中分析while与do-while之间的区别和联系
3.实训项目的具体实现(本页不够可以另加页) 
import .*;
import Event; 
import Listener; 
import
nt; 
import tener; 
import Event; 
import
Listener; 
 
 
public class
GuessNumberFrame extends Frame implements
ActionListener,WindowListener,KeyListener{
 
 
 
 
 
 
 
 
 
 
 
 
 
Button
buttonGetNumber,buttonEnter; 
Label labelShow;
TextField textInput; 
int realNumber;
public GuessNumberFrame() { 
 
 
 
 
 
 
 
 
super(猜数字游戏);
e(200,180); 
ation(300,300);
kground(ray); 
int
width=aultToolkit().getScreenSize().width; 
int
height=aultToolkit().getScreenSize().height;
ation((width-300)2,(height-310)2);
izable(false); 
out(new
FlowLayout(1,5,15)); 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
 
 
buttonGetNumber=new
Button(得到一个随机数); 
 
(buttonGetNumber);
ionListener(this); 
 
labelShow=new
Label(欢迎使用,猜数字游戏:,); 
kground();
(labelShow); 
 
textInput=new
TextField(10); 
(textInput);
Listener(this); 
 
buttonEnter=new
Button(确定); 
(buttonEnter); 
ionListener(this); 
dowListener(this); 
ible(true); 
public void
actionPerformed(ActionEvent e) { 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
if(rce()==buttonGetNumber){ 
 
 
} 
if(rce()==buttonEnter){ 
 
 
 
 
 
 
 
 
 
 
 
 
try{ 
 
 
 
 
 
 
 
 
 
 
 
int guess=nt(t());
if(guess==realNumber){ 
 
} 
else if
(guess>realNumber) { 
 
 
} 
else if
(guess
 
} 
t(猜小了);
 tFocus(); 
t(猜大了); 
tFocus();
t(猜对了); 
 realNumber=(int)(()*100)+1; 
t(请输入你的猜测); 
 tFocus(); 
}catch
(NumberFormatException e1) { 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
 
 
 
 
} 
 
 
 
} 
 
} 
 
t(请重新输入数字); 
public void
keyPressed(KeyEvent e) { 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
 
public
void windowClosing(WindowEvent e) { 
 
}
 
public void windowActivated(WindowEvent
e) {} 
public void windowClosed(WindowEvent e)
{} 
public void windowDeactivated(WindowEvent
e) {} 
public void
windowDeiconified(WindowEvent e) {} 
public
void windowIconified(WindowEvent e) {} 
public
void windowOpened(WindowEvent e) {} 
public
void keyTyped(KeyEvent e) {} 
(0);
if(Code()==_ENTER){ 
 try{ 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
} 
 
 
 
 
 
 
 
 
 
 
 
 
int
guess=nt(t()); 
if(guess==realNumber){ 
} 
else if (guess>realNumber) { 
 
} 
else if (guess
} 
t(猜小了); 
tFocus(); 
t(猜大了);
tFocus(); 
t(猜对了); 
}catch
(NumberFormatException e1) { 
 t(请重新输入数字);
} 
 
    public void
keyReleased(KeyEvent e) {} 
class
GuessNumberFrame_ex{ 
 public static void
main(String[] args){ 
 
 
 
}
new GuessNumberFrame();