-1

试图把它变成多人游戏......需要最多 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;
  }
}
4

1 回答 1

3

你做了一个类,这是面向对象编程的第一步

但是,你所有的方法都是static,这是一个限制。

您必须删除除staticmain 之外的所有关键字并实例化您的游戏 5 次。

这是 OO 的第二步:实例。

完成后,您会看到一些细节需要是唯一的,您将创建第二个类,实例化一次。此类将5 个实例有关。

自己走这条路……

于 2012-10-24T21:59:05.357 回答