//终于写好了,试试吧……有问题再问
import java.awt.*;
import java.awt.event.*;
public class CaiShuZi
{ public static void main(String args[])
{ new Gui("猜数字游戏"); }
}
class Gui extends Frame implements ActionListener
{
static int number;
static int guessnumber;
static TextField tx;
static Label lb3,lb1,lb2;
static Button bt3,bt1,bt2;
Gui(String s)
{ super(s);
lb1=new Label("单击该按钮可以产生一个0-100之间的随机数:");
bt1=new Button("产生随机数");
lb2=new Label("输入一个数后单击判断即显示判断结果:");
tx=new TextField(10);
bt2=new Button("判断");
bt2.setEnabled(false);
bt3=new Button("答案");
lb3=new Label(" ");
setLayout(new FlowLayout());
add(lb1);
add(bt1);
add(lb2);
add(tx);
add(bt2);
add(lb3);
add(bt3);
bt1.addActionListener(this) ;
bt2.addActionListener(this) ;
bt3.addActionListener(this) ;
addWindowListener(new Winad()) ;
setSize(300,200);
setVisible(true);
}
//重写ActionListener接口中的方法
public void actionPerformed(ActionEvent e)
{ if(e.getActionCommand()=="产生随机数")
{
for(int i=0;i<10;i++)
number=number+(int)(Math.random()*10)+1;
bt2.setEnabled(true);
}
if(e.getActionCommand()=="判断")
{
try{
guessnumber=Integer.parseInt(tx.getText());
if(number==guessnumber)
lb3.setText("猜对了");
else if(number
else
lb3.setText("猜小了");
}catch(Exception e1){lb3.setText("输入错误或为空!");}
}
if(e.getActionCommand()=="答案")
lb3.setText(String.valueOf(number));
}
}
class Winad extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
}
import java.util.Random;
import java.util.Scanner;
public class Du {
public static void main(String[] args) {
int rightNum = new Random().nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int input = 0;
do{
System.out.print("清猜数字(1-100)!");
input = scanner.nextInt();
if(input > rightNum){
System.out.println("猜大了!");
}else if(input < rightNum){
System.out.println("猜小了!");
}
}while(input != rightNum);
System.out.println("猜对了" + rightNum);
}
}
---------------
这也行