import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
public class Nicki extends Applet{
private static final long serialVersionUID = 1L;
private Button ok;
private int num=32;
private int resu=0;
private boolean isRig=false;
private TextField iPut;
public Nicki(){
this.setLayout(null);
ok=new Button("OK");
ok.setActionCommand(getName());
ok.setBounds(150, 150, 40, 20);
iPut=new TextField();
this.add(iPut);
iPut.setBounds(100, 150, 40, 20);
this.add(ok);
ok.addActionListener(new ButtonAction(this));
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLACK);
g.drawString("Please guess a number ", 10, 20);
g.drawString("between 1 and 100", 10, 40);
if(isRig==false&&resu!=0){
if(resu>num){
g.drawString(""+resu+" is too big !", 10, 100);
}else if(resu g.drawString(resu+"is too small !", 10, 100); } }else if(isRig==true){ g.setColor(Color.GREEN); g.drawString("Yes,"+resu+" is the right number", 10, 80); g.drawString("Your are great! ", 10, 100); g.setColor(Color.red); g.drawString(resu+"!", 70, 120); } iPut.setText(""); g.drawString("Input the number:", 0, 150); } public void ButtonActionPerformed(ActionEvent e){ if(e.getActionCommand().equals("panel0")){ resu=Integer.parseInt(iPut.getText()); if(num==resu){ isRig=true; }else{ isRig=false; } repaint(); } } } class ButtonAction implements java.awt.event.ActionListener{ Nicki su; public ButtonAction(Nicki bun){ this.su=bun; } public void actionPerformed(ActionEvent e) { su.ButtonActionPerformed(e); } }
随手写了 不想测试了,自己测吧,输入的异常也自己处理好了
double i = 0.0;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入一个数字(1-100)");
i = sc.nextDouble();
if(i>32){
System.out.println("你输入的数字大了");
}else if(i<32){
System.out.println("你输入的数字小了");
}else{
System.out.println("恭喜你答对了");
System.out.println("是否还要继续 1继续 2退出");
if(sc.nextInt()==1){
continue;
}else{
return;
}
}
}