java猜数字游戏复杂版
时间: 2023-10-18 13:03:25 浏览: 91
好的,下面是一个复杂版的Java猜数字游戏:
```java
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int min = 1;
int max = 100;
int range = max - min + 1;
int numToGuess = (int) (Math.random() * range) + min;
int numGuesses = 0;
int guess = 0;
int[] guesses = new int[10];
System.out.println("I am thinking of a number between " + min + " and " + max + ".");
System.out.println("You have 10 guesses to find the number.");
while (numGuesses < 10) {
System.out.print("Enter your guess: ");
guess = input.nextInt();
if (guess < min || guess > max) {
System.out.println("Your guess is out of range. Please guess again.");
continue;
}
guesses[numGuesses] = guess;
numGuesses++;
if (guess == numToGuess) {
System.out.println("Congratulations! You guessed the number in " + numGuesses + " guesses.");
break;
} else if (guess < numToGuess) {
System.out.println("Your guess is too low.");
} else {
System.out.println("Your guess is too high.");
}
}
if (numGuesses == 10) {
System.out.println("Sorry, you have run out of guesses. The number was " + numToGuess + ".");
}
System.out.println("Your guesses were:");
for (int i = 0; i < numGuesses; i++) {
System.out.print(guesses[i] + " ");
}
}
}
```
这个游戏会在1到100之间随机选一个数字,然后让玩家猜这个数字。玩家有10次机会猜测,如果在10次之内猜中了,游戏就结束。如果10次之内没有猜中,游戏就失败。在游戏结束之后,程序会打印出玩家的猜测记录。
阅读全文