Java输出一个小游戏
时间: 2024-03-07 13:20:57 浏览: 43
好的,您想要我展示一个 Java 输出的小游戏,以下是一个简单的 Java 小游戏代码:
```
import java.util.Scanner;
public class GuessTheNumber {
public static void main(String[] args) {
int secretNumber = (int) (Math.random() * 100);
int tries = 0;
int guess = -1;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Guess the Number!");
System.out.println("Guess a number between 0 and 100.");
while (guess != secretNumber) {
System.out.print("Enter your guess: ");
guess = input.nextInt();
if (guess < secretNumber) {
System.out.println("Too low! Try again.");
tries++;
} else if (guess > secretNumber) {
System.out.println("Too high! Try again.");
tries++;
} else {
System.out.println("Congratulations! You guessed the number in " + tries + " tries.");
}
}
input.close();
}
}
```
这是一个猜数字小游戏,玩家需要猜出程序生成的随机数是多少。程序会提示玩家猜的数字是偏高还是偏低,并记录玩家猜测的次数,直到玩家猜中为止。
阅读全文