如何将值传递回Java中的类?

时间:2016-05-20 16:08:11

标签: java class

我正在构建一个使用第二类来处理命令的互动小说游戏。当我键入命令时,它将被发送到第二个类进行处理。问题是当我运行代码时,我的值(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."); }
 }
}

3 个答案:

答案 0 :(得分:1)

您可以返回一个整数数组(或更好的对象)。这是最简单的方式(在我看来)

主要课程:

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();
}

相同的输出!祝你好运!

答案 1 :(得分:1)

因此,在方法 cmdCenter(...)中,您将添加如下返回语句:

 public static int[] cmdCenter(String cmdIF, int x, int y) {
       .....//your code

       return new int[]{x,y};
  }

将返回一个数组,其中包含 x在单元格0,y在单元格1

所以基本上你的方法是public,static,not void(它返回和int [] [整数数组]等..)

*查看如何在java here

中使用return语句

答案 2 :(得分:1)

这对于原始类型(int,long,...)是不可能的,因为它们是按值传递的。

OOP解决方案是将变量封装到对象中。

您可以在此处使用的另一个解决方案是将静态变量引用到cmdCenter

也许this post可能会帮助您理解。