/*
需求分析:
1.系统生成一个随机数
2.用户键盘输入
3.逻辑比较
*/
import java.util.Random;
import java.util.Scanner;
class Demo2 {
public static void main(String[] args) {
Random ra = new Random();
Scanner sc = new Scanner(System.in);
int guessName = ra.nextInt(50) + 1;
System.out.println(guessName);
for(int i = 0;i<6;i++){
System.out.println("请输入一个整数");
int enterName = sc.nextInt();
if(enterName == guessName){
System.out.println("回答正确");
break;
}else{ //采用嵌套把相联系的事件一起
if(enterName > guessName){
System.out.println("大了");
}else if(enterName < guessName){
System.out.println("小了");
}
if(i == 5){
System.out.println("对不起你没有机会了");
break;
}
System.out.println("你还有"+(5-i)+"次机会");
}
}
}
}
|
|