我有编译错误“找不到符号 - 方法getPlayer()”

时间:2015-11-09 19:42:55

标签: java methods cannot-find-symbol

我应该写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();
    }
}

1 个答案:

答案 0 :(得分:0)

您的课程没有特定的构造函数,或者您的代码中没有。 To learn more about Construcotrs, please read the documentation here。简单来说,构造函数是一种允许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)方法。

显示的代码的另一个问题是,您正在使用对象方法来创建对象... 方法必须是静态的(read about it here),或者它必须不依赖于您正在创建的对象。要简化程序,只需在将值用于变量之前保存该值,然后在对象构造中使用该变量。甚至可以创建一个像public static String getJohn(){return "John"}这样的基本方法,然后你的代码就会超越这一点。

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