简体   繁体   English

Java无法在单独的文件上找到特定的方法

[英]Java cannot find a specific method on a seperate file

Im using Visual Studio Code, and I just create a folder with the "project" inside, i dont like to use netbeans, eclipse, etc for small programs. 我使用的是Visual Studio Code,我只是在其中创建了一个带有“项目”的文件夹,我不喜欢将netbeans,eclipse等用于小型程序。

So I'm creating a small program, that i doubted would work, but it did for the mos part, it creates a rpg like character and a 2nd one and creates a simulated battle in which one character wins based on one stat. 因此,我正在创建一个小程序,我怀疑它是否会起作用,但它确实适用于mos部分,它创建了一个像角色扮演角色一样的rpg和第二个角色,并创建了一个模拟战斗,其中一个角色基于一个属性获胜。 Everything seems to work but when I call the method Battle(oumar, aisha); 一切似乎都正常,但是当我调用方法Battle(oumar, aisha); , which takes two characters and has them battle, it gets an error saying Main.java:6: error: cannot find symbol Battle(oumar, aisha); ^ symbol: method Battle(Character,Character) location: class Main ,它需要两个字符并使它们战斗,它会显示一条错误消息Main.java:6: error: cannot find symbol Battle(oumar, aisha); ^ symbol: method Battle(Character,Character) location: class Main Main.java:6: error: cannot find symbol Battle(oumar, aisha); ^ symbol: method Battle(Character,Character) location: class Main

So since im not so experienced im not sure what the problem really is. 因此,由于我不是那么有经验,所以我不确定真正的问题是什么。 Here is the code for the main class: 这是主类的代码:

    public class Main {
    public static void main(String args[]) {

        Character oumar = new Character("Oumar", 10);
        Character aisha = new Character("Aisha", 9);
        Battle(oumar, aisha);

    }


}

and here is the second class within a seperate file (still in the same folder) 这是单独文件中的第二类(仍在同一文件夹中)

public class Character {

    String name;
    int BattlePower;
    int wins;
    Character one;
    Character two;

    public Character(String name, int BattlePower) {
        this.name = name;
        this.wins = wins;
        System.out.println("New character: "+ name);
        this.BattlePower = BattlePower;
        this.wins = wins;
        System.out.println(name + "has a Battle Power of " + BattlePower);
    }

    public void Battle(Character one, Character two) {

        this.one = one;
        this.two = two;

        if (one.BattlePower > two.BattlePower ) {
            System.out.print("Character " + one + " has won the Battle!");
            one.wins++;
            System.out.print("Character one now has " + wins + " wins!");
        }
        else if (two.BattlePower > one.BattlePower) {
            System.out.print("Character " + two + " has won the Battle!");
            two.wins++;
            System.out.print("Character two now has " + wins + " wins!");
        }
        else {

            System.out.print("The two characters have tied!");

        }    
    }    
}

Any help would be great, as well as any tips that might help me in the future. 任何帮助都将是巨大的,以及将来可能对我有帮助的任何提示。

I believe you would need to use 我相信您将需要使用

oumar.Battle(oumar, aisha)

or 要么

aisha.Battle(oumar, aisha)

as the method is inside of a non static class so it can't be called without an object reference. 因为该方法位于非静态类的内部,所以没有对象引用就无法调用该方法。

If you want to call it like 如果你想这样称呼

Battle(oumar, aisha)

you'll need to move it to the main class. 您需要将其移至主类。

Also, this issue should be present on those other IDEs as well. 另外,在其他IDE上也应该存在此问题。

You can put a no-arg constructor inside the Character class like public Character(){}. 您可以在Character类中放置一个无参数的构造函数,例如public Character(){}。 Then in the Main.java you can use the no-arg constructor to create an object of the Character class and then invoke the method Battle like Character ch1 = new Character(); 然后在Main.java中,可以使用no-arg构造函数创建Character类的对象,然后调用Battle方法,例如Character ch1 = new Character();。 ch1.Battle(oumar,aisha). ch1.Battle(乌马尔,阿伊莎)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM