import java.awt.*;
import java.awt.event.*;
class WindowGuess extends Frame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = -5001338309081570746L;
int number;
int score1=100,score2=100;
Label 提示条1,提示条2,提示条3,提示条4,提示条5;
TextField 输入框;
Button buttonGetNumber,buttonEnter;
WindowGuess(String s)
{
super(s);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
setLayout(new FlowLayout());
buttonGetNumber=new Button("得到一个随机数");
add(buttonGetNumber);
提示条1=new Label("猜猜看: ");
提示条2=new Label(" 电脑: "+score1);
提示条3=new Label(" 玩家: "+score2);
提示条4=new Label(" ");
提示条5=new Label(" ");
提示条1.setBackground(Color.cyan);
提示条2.setBackground(Color.gray);
提示条3.setBackground(Color.blue);
提示条4.setBackground(Color.WHITE);
提示条5.setBackground(Color.RED);
输入框=new TextField("0",10);
add(提示条1);
add(输入框);
add(提示条5);
buttonEnter=new Button(" 确 定 ");
add(buttonEnter);
add(提示条2);
add(提示条3);
add(提示条4);
buttonEnter.addActionListener(this);
buttonGetNumber.addActionListener(this);
setBounds(500,100,120,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttonGetNumber)
{
number=(int)(Math.random()*100)+1;
提示条1.setText("猜猜看: ");
提示条4.setText(" ");
score1=100;score2=100;
提示条2.setText(" 电脑: "+100);
提示条3.setText(" 玩家: "+100);
}
else if(e.getSource()==buttonEnter)
{
int guess=0;
try
{
guess=Integer.parseInt(输入框.getText());
if(guess==number)
{
提示条1.setText("猜对了! ");
提示条5.setText(" ");
score1=score1-10;
score2=score2+10;
if(score1<=0) {提示条4.setText("恭喜你赢了!");score1=100;score2=100;提示条2.setText("电脑: 0");提示条3.setText("玩家: 200");number=-1;}
if(score2<=0) {提示条4.setText("太遗憾了,你没分了!");score1=100;score2=100;提示条2.setText("电脑: 200");提示条3.setText("玩家: 0");number=-1;}
if(number>=0)
{
提示条2.setText(" 电脑: "+score1);
提示条3.setText(" 玩家: "+score2);
}
}
else if(guess>number)
{
提示条1.setText("猜大了! ");
输入框.setText(null);
if((guess-number)<=5)
{
score1=score1-5;
score2=score2+5;
提示条5.setText("误差在'5'内");
}
else if(((guess-number)<=10)&&((guess-number)>5))
{
score1=score1+0;
score2=score2-0;
提示条5.setText("误差在'10'内");
}
else if(((guess-number)<=20)&&((guess-number)>10))
{
score1=score1+5;
score2=score2-5;
提示条5.setText("误差在'20'内");
}
else if(((guess-number)<=100)&&((guess-number)>20))
{
score1=score1+10;
score2=score2-10;
提示条5.setText("误差在'100'内");
}
if(score1<=0) { 提示条4.setText("恭喜你赢了!");score1=100;score2=100;提示条2.setText("电脑: 0");提示条3.setText("玩家: 200");number=-1;}
if(score2<=0) { 提示条4.setText("太遗憾了,你没分了!");score1=100;score2=100;提示条2.setText("电脑: 200");提示条3.setText("玩家: 0");number=-1;}
if(number>=0)
{
提示条2.setText(" 电脑: "+score1);
提示条3.setText(" 玩家: "+score2);
}
}
else if(guess<number)
{
提示条1.setText("猜小了! ");
输入框.setText(null);
if((number-guess)<=5)
{
score1=score1-5;
score2=score2+5;
提示条5.setText("误差在'5'内");
}
else if(((number-guess)<=10)&&((number-guess)>5))
{
score1=score1+0;
score2=score2-0;
提示条5.setText("误差在'10'内");
}
else if(((number-guess)<=20)&&((number-guess)>10))
{
score1=score1+5;
score2=score2-5;
提示条5.setText("误差在'20'内");
}
else if(((number-guess)<=100)&&((number-guess)>20))
{
score1=score1+10;
score2=score2-10;
提示条5.setText("误差在'100'内");
}
if(score1<=0) { 提示条4.setText("恭喜你赢了!");score1=100;score2=100;提示条2.setText("电脑: 0");提示条3.setText("玩家: 200");number=-1;}
else if(score2<=0) { 提示条4.setText("太遗憾了,你没分了!");score1=100;score2=100;提示条2.setText("电脑: 200");提示条3.setText("玩家: 0");number=-1;}
if(number>=0)
{
提示条2.setText(" 电脑: "+score1);
提示条3.setText(" 玩家: "+score2);
}
}
}
catch(NumberFormatException event)
{
提示条1.setText("请输入数字字符");
}
}
else
{
System.exit(0);
}
}
}
public class Guessnume
{
public static void main(String[] args)
{
new WindowGuess("猜猜看!");
}
}