我正在构建一个使用第二类处理命令的交互式小说游戏。当我键入命令时,它会发送到第二类进行处理。问题是当我运行代码时,我的值(int xint y)没有传递回主类。如何将这些值传递回主类,以便打印northD

主班:

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

    public class Main {

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

            Scanner input = new Scanner(System.in);

            out.println("Welcome to the world! Which way do you want to go?");
            cmdIF = input.nextLine();
            choosePath();
            if(x==1 || y == 0) {
             northD = "You have entered the woods.";
                out.print(northD);
            }
        }
        public static void choosePath() {
            actionClass.cmdCenter(cmdIF, x, y);
        }
    }


第二类:

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

最佳答案

您可以返回一个整数数组(最好是一个对象)。这是最简单的方法(我认为)

主班:

public class Main {

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

    Scanner input = new Scanner(System.in);

    System.out.println("Welcome to the world! Which way do you want to go?");
    cmdIF = input.nextLine();
    choosePath();
    if(x==1 || y == 0) {
     northD = "You have entered the woods.";
        System.out.println(northD);
    } else {
        System.out.println("You have entered the home");
    }
}
public static void choosePath() {
    //Method return an array of integer
    int[] newPos = actionClass.cmdCenter(cmdIF, x, y);
    //GETX
    System.out.println(newPos[0]);
    //GETY
    System.out.println(newPos[1]);
    //set X
    x = newPos[0];
    //set Y
    y = newPos[1];
}
}


活动课:

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

 //New array, first position x, second position y
 int[] res = {x,y};
 //Return it
 return res;
}

}


输出:

Welcome to the world! Which way do you want to go?
EAST
0
1
You have entered the home


更多输出:

Welcome to the world! Which way do you want to go?
NORTH
1
0
You have entered the woods.


带物体

一种更复杂的方法是使用自定义对象。所以新课:

public class xyObj {

    public int x;
    public int y;

    //Set x and y
    public xyObj(int x,int y){
        this.x=x;
        this.y=y;
    }
    //get x
    public int getX(){
        return x;
    }
    //get y
    public int getY(){
        return y;
    }
}


现在,活动类返回此对象:

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

     //new xyObj setting x and y
     xyObj ret = new xyObj(x, y);
     //return it
     return ret;
 }

}


我们还必须修改choosePath方法

public static void choosePath() {
    xyObj xyPos = actionClass.cmdCenter(cmdIF, x, y);
    System.out.println(xyPos.getX());
    System.out.println(xyPos.getY());
    x = xyPos.getX();
    y = xyPos.getX();
}


输出相同!祝好运!

10-02 06:46