java猜数字300到800的范围
时间: 2023-10-17 11:33:01 浏览: 32
好的,以下是一个简单的Java猜数字游戏,数字范围为300到800:
```java
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
public static void main(String[] args) {
Random rand = new Random();
int secretNum = rand.nextInt(501) + 300; // 生成300到800之间的随机数
Scanner input = new Scanner(System.in);
int guess;
int count = 0;
boolean win = false;
System.out.println("我已经想好一个数字,它在300到800之间。你有10次机会猜。");
while (count < 10) {
System.out.print("猜一个数字:");
guess = input.nextInt();
count++;
if (guess < secretNum) {
System.out.println("你猜的数字太小了。");
} else if (guess > secretNum) {
System.out.println("你猜的数字太大了。");
} else {
win = true;
break;
}
}
if (win) {
System.out.println("恭喜你!你猜对了,你用了" + count + "次机会。");
} else {
System.out.println("很遗憾,你没有猜对。正确的数字是" + secretNum + "。");
}
input.close();
}
}
```
希望能帮助到你!