import java.util.Random;
import java.util.Scanner;
public class GuessNumber
{
public static void main(String[] args)
{
//产生随机数
Random random = new Random();
int randomNumber = random.nextInt(100)+1;
System.out.println(randomNumber);
//==============================================
for(int i=0; i<5; i++){
//用户录入数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入您猜的数字(1-100)");
int guessNumber = sc.nextInt();
//逻辑分析
if(guessNumber == randomNumber) {
System.out.println("恭喜你,答对了");
//跳出循环
break;
}else if(guessNumber > randomNumber) {
System.out.println("sry,你猜大了");
}else {
System.out.println("sry,你猜小了");
}
//如果是第5次猜完了,就不再循环了
if(i<4) {
System.out.println("您还有"+(4-i)+"次机会。");
}else {
System.out.println("sry,您没有机会了!");
}
}
//==============================================
System.out.println("欢迎下次再爽!");
}
}
|
|