如何在main方法中调用第二个类中的方法来执行Java中的操作?

时间:2016-05-20 02:24:54

标签: java class methods

我正在用Java构建一个Interactive-Fiction-Game,但是在使用第二个类在主类中使用时遇到了麻烦。我正在使用Eclipse。 这是我的主要课程:

import java.util.Scanner;
import static java.lang.System.out;

public class mainClass {

    public static void main(String[] args) {
        String cmdIF;
        Scanner input = new Scanner(System.in);
        int x = 0;
        int y = 0;


        out.print("Welcome to the world! Which way do you want to go?");
        String northD = "You walk into a forest.";
        if(x=1) {
            out.print(northD);
        }
        cmdIF = input.nextLine();
        choosePath();   
    }
    public void choosePath(actionClass cmdCenter) {
          actionClass.cmdCenter();
    }
}

包含方法的类:

public class actionClass {
    public void cmdCenter() {
        if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")) { out.println(northD); x++; }
        else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) { out.println(eastD); y++; }
        else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { out.println(southD); x--; }
        else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { out.println(westD); y--; }
        else { out.println("You can't do that."); }

    }
}

每当我执行代码时,我都会收到此错误:

  

线程中的异常" main" java.lang.Error:未解析的编译   问题:mainClass类型中的方法choosePath(actionClass)是   不适用于arguments()

     

在mainClass.main(mainClass.java:14)

如何让这些方法协同工作?

2 个答案:

答案 0 :(得分:0)

cmdCenter()中的actionClass方法更改为声明为:

public void cmdCenter(String cmdIF)

然后将choosePath()更改为

public void choosePath(actionClass action, String cmdIf) {
    action.cmdCenter(cmdIf);
}

并且明显地将main()方法的调用适当地更改为choosePath()。您需要newactionClass的实例传递到choosePath()

答案 1 :(得分:0)

在第一步中,您可以像这样访问

actionClass.cmdCenter();

不是static所以只需像这样cmdCenter 静态

public static void cmdCenter

choosePath()参数,因此您必须像choosePath(actionClass)一样使用它,因为您在static方法中使用它,您必须创建实例或制作choosePath静态

  public static void choosePath(actionClass) 

但您不必从paramater删除choosePath

   //public void choosePath(actionClass cmdCenter) {
     public static void choosePath()

似乎你必须将xy变量传递给cmdCenter,所以它变得像这样

public static void cmdCenter(String cmdIF, int x, int y)

让代码保持正常

您的代码的固定版本:

public class Main {


    static String cmdIF;
    static int x = 0;
    static int y = 0;
    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);


        System.out.print("Welcome to the world! Which way do you want to go?");
        String northD = "You walk into a forest.";
        if(x ==1) {
            System.out.print(northD);
        }
        cmdIF = input.nextLine();
        choosePath();
    }
    public static void choosePath() {
        actionClass.cmdCenter(cmdIF, x, y);
    }
}

public class actionClass {
    public static void cmdCenter(String cmdIF, int x, int y) {
        if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")) { out.println("GO NORTH"); x++; }
        else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) { out.println("GO EAST"); y++; }
        else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { out.println("GO SOUTH"); x--; }
        else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { out.println("GO WEST"); y--; }
        else { System.out.println("You can't do that."); }

    }
}