代码如下:
import java.util.Date;
import java.util.Scanner;
public class GuessNumberGames {
static long nowstart;
public static void main(String[] args) {
int number=(int)(Math.random()*10+1);
int temp=number;
System.out.println("请猜一个10以内的数:");
GuessNumberGames rt=new GuessNumberGames();
int in = rt.Sn();
nowstart = new Date().getTime();
Boolean yn = rt.Compare(temp, in);
while(!yn){
System.out.println("请重新输入:");
int in1 = rt.Sn();
yn = rt.Compare(temp, in1);
}
System.out.println("正确数字是:"+number);
}
public Boolean Compare(int temp,int number){
Boolean yesno = null;
if(temp==number){
System.out.println("猜对了!");
long nowend = new Date().getTime();
System.out.println("一共用时"+(nowend-nowstart)/1000+"秒");
yesno=true;
}else if(numberSystem.out.println("猜小了");
yesno=false;
}else if(number>temp){
System.out.println("猜大了");
yesno=false;
}
return yesno;
}
public int Sn(){
Scanner sc = new Scanner(System.in);
int in=sc.nextInt();
return in;
}
}
满意请采纳!