0

我应该在写nim的游戏。我无法找出如何修复编译错误

找不到符号 - 方法 getPlayer()

另外,这是您看到的唯一问题吗?或者是否有其他问题会导致该程序无法编译或正常工作。

import java.util.Scanner;
import java.util.Random;

public class Nim {
    private int n;
    private int compMode;
    private int numberLeft;
    private int numberTaken;
    private boolean whoseTurn;
    private String inputName;
    private String name;
    private String play;
    private boolean yes;
    Scanner in = new Scanner(System.in);
    Random num = new Random();

    public void setState() {
        numberLeft = 100;

        numberTaken = numberLeft;
    }

    public String getPlayer() {
        inputName = in.next("");
        inputName = name;
        return name;
    }

    public void getCompPlay() {
        compMode = num.nextInt(2);
        if (compMode == 0) System.out.println("The computer is in smart mode");

        if (compMode == 1) System.out.println("The computer is in random mode");
    }

    public void playGame() {
        if (whoseTurn == true) {
            System.out.println(name + "It is your turn...");
            System.out.printf("Please enter the number you wish to take from the pile (Must be less than " + (numberLeft / 2) + "): ");
            numberTaken = in.nextInt();
            numberLeft -= numberTaken;
            System.out.println("The number left is " + numberLeft);
            whoseTurn = false;
        }

        if (whoseTurn == false) {
            System.out.println("It is the computer's turn...");
            if (compMode == 0) {
                numberLeft = smartComputer(numberLeft);
                System.out.println("The number left is " + numberLeft);
            }

            if (compMode == 1) {
                numberLeft -= num.nextInt(numberLeft / 2);
                System.out.println("The number left is " + numberLeft);
            }
            whoseTurn = true;
            return;
        }

        if (yes == true) {

        }

        if (numberLeft <= 1) {
            if (whoseTurn = false) {
                System.out.println("You Win!");
            } else {
                System.out.println("You're horrible...you lost to a computer.");
            }
        }

        if (numberLeft <= 1) {
            if (whoseTurn = false) {
                System.out.println("You Win!");
            } else {
                System.out.println("you lost to a computer.");
            }
        }

    }

    public static int smartComputer(int num) {
        int power = 2;
        while (power < num) {
            power *= 2;
        }
        power /= 2;
        num = power - 1;
        return num;
    }

    public boolean playAnother() {
        System.out.println("/nPlay Again? (y/n)");
        play = in.next("");
        if (play.equalsIgnoreCase("y")) return true;
        else return false;
    }

    public void displayTotals() {}
}

这是我的测试仪

public class NimTester {
    public static void header() {
        System.out.println("Eric Magnusson");
        System.out.println("AP Comp Sci");
        System.out.println("Game of Nim (P6.16)");
    }
    public static void main() {
        Nim nim = new Nim(getPlayer(), getCompPlay());

        do {
            nim.setState();
            nim.playGame();
            nim.printWinner();
        } while (playAnother());
        nim.displayTotals();
    }
}
4

1 回答 1

0

您的类要么没有特定的构造函数,要么没有在您的代码中显示。要了解更多关于构造器的信息,请阅读此处的文档。简单来说,Constructor是一种允许JVM组织内存和初始化对象的方法。默认对象构造函数是一个非参数构造函数,它将每个子变量设置为默认值。

例子:

Class Car{
   public Car(){
      System.out.print("car is built.");
   }
}

Car aCar = new Car();

因此,您所拥有的是Nim nim = new Nim(getPlayer(), getCompPlay());您的Nim班级必须有一个public Nim(String x, String y)方法。

显示的代码的另一个问题是,您正在使用对象方法来创建对象......该方法必须是静态的(在此处阅读),或者它必须不依赖于您正在创建的对象。为了简化您的程序,只需在将其用于变量之前保存该值,然后在对象构造中使用该变量。或者甚至创建一个静态方法,例如:public static String getJohn(){return "John"},然后您的代码将在该点之后工作。

代码的其他问题与可读性有关,请阅读 Java 命名约定,了解如何命名变量,它们将有助于维护,并允许其他程序员为您提供帮助。

于 2015-11-09T19:53:25.150 回答