java简单代码实现猜数字游戏------------------------------------------
import java.util.Random;
import java.util.Scanner;
public class NumGame{
public static void main(String[] args){
Random randomnum=new Random();
Scanner num=new Scanner(System.in);
int n=randomnum.nextInt(100);
System.out.println(n);
int i=0;
System.out.println("你只有5次机会");
while(true){
i++;
int number=num.nextInt();
if(number>n){
if((5-i)!=0){
System.out.println("大了,还剩"+(5-i)+"次机会");
}
else{
System.out.println("大了,机会已用完");
break;
}
}
else if(number<n){
if((5-i)!=0){
System.out.println("小了,还剩"+(5-i)+"次机会");
}
else{
System.out.println("小了,机会已用完");
break;
}
}
else{
System.out.println("正确!");
break;
}
if(i>4){
System.out.println("机会已用完");
break;
}
}
}
}