public static void main(String[] args) {
int guessNum = (int)(Math.random()*100) + 1;
Scanner sc = new Scanner(System.in);
System.out.println("猜数字开始啦!" + '\n' + "请输入一个1到100的整数:");
int flag = 0;
while (true)
{
int result = sc.nextInt();
flag++; //记录猜的次数
if (result > guessNum)
{
System.out.println("你猜大了");
}else if (result < guessNum)
{
System.out.println("你猜小了");
}else {
System.out.println("猜对了");
System.out.println("你猜了" + flag +"次");
break;
}
if (flag == 5)//只能猜5次
{
System.out.println("你猜了" + flag +"次");
System.out.println("GAME OVER");
break;
}
}
|
|