【问题标题】:I am having the compile error "cannot find symbol - method getPlayer()"我遇到编译错误“找不到符号 - 方法 getPlayer()”
【发布时间】:2016-02-10 13:26:27
【问题描述】:

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

【问题讨论】:

  • getPlayer()Nim 类的方法。你需要一个Nim 的实例来调用它。
  • if (whoseTurn = false) 是个问题。应该是if (whoseTurn == false),但我会提示你它也可以写成if (!whoseTurn)

标签: java methods cannot-find-symbol


【解决方案1】:

您的类要么没有特定的构造函数,要么没有在您的代码中显示。 To learn more about Construcotrs, please read the documentation here。简单来说,Constructor是一种允许JVM组织内存和初始化对象的方法。 默认对象构造函数是一个无参数构造函数,它将每个子变量设置为默认值。

例子:

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

Car aCar = new Car();

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

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

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

【讨论】:

  • 这些都是cmet而不是问题的答案。
  • 是的,还没有为那部分编写任何代码。我想先编译这个问题
  • @cricket_007 问题是is this the only problem you see? Or are there other issues that would cause this program to fail to compile or work properly. ... 有一个不存在的构造函数,有一个非布尔值作为表达式,以及一个未启动的变量比较都是问题。
  • 当然可以,但是您可以通过解释这些问题的原因以及如何解决这些问题来更新您的答案,而不是简单地指出它们。例如,也许 OP 不知道构造函数是什么或为什么需要构造函数,或者区别在于使用静态方法和非静态方法等。
猜你喜欢
  • 2021-02-28
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多