试图把它变成多人游戏......需要最多 5 名玩家。我已经有了基础,只是不知道如何使它适用于多人游戏。我尝试过的方法不是存储每个人的猜测。这是精简版。
import java.util. * ;
public class randomNumber {
public static void hint(int answer, int guess) {
if(answer < guess) {
System.out.println("Incorrect! \n TOO HIGH!.");
} else if(answer > guess) {
System.out.println("Incorrect! \n TOO LOW!.");
} else if(answer == guess) {
System.out.println("YOU GOT IT!");
}
}
public static void main(String[] args) {
System.out.println("How many Players will be playing?");
int players = players();
int player = 0;
player++;
System.out.println("Player " + player + " Enter a number");
int guess = guess();
int answer = random();
int count = 0;
while (guess != answer) {
hint(answer, guess);
count = count(count);
guess = guess();
}
hint(answer, guess);
System.out.println("It took " + count(count) + " tries!");
}
public static int random() {
return(int)(Math.random() * 100) + 1;
}
public static int players() {
Scanner my_input = new Scanner(System. in );
return my_input.nextInt();
}
public static int guess() {
Scanner my_input = new Scanner(System. in );
return my_input.nextInt();
}
public static int count(int c){
c++;
return c;
}
}