How to pass a value back into a class in Java?

SputnicK

I am building an Interactive Fiction game that uses a second class to deal with commands. When I type a command it is sent to the second class to be processed. The problem is when I run the code my values (int x and int y) are not being passed back into the main class. How do I pass these values back into the main class so that northD will print?

Main class:

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

Second class:

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."); }
 }
}
Francesco Serra

You can return an array of integer (or better an object). This the simplest way (in my opinion)

Main class:

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];
}
}

Activity class:

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

}

Output:

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

More output:

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

With an object

A more complex way is to use a custom object. So new class:

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

Now activity class return this object:

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

}

We have to modify also the choosePath method

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

Same output! Good luck!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java Pass By Value交换方法

来自分类Dev

How to pass global volatile unsigned char to class

来自分类Dev

How to Pass a Value From a Window to a UserControl in WPF

来自分类Dev

Django Templates: How to Pass PK/IDs of Displayed Objects Back to the Views.py file from the Template?

来自分类Dev

MS-Access VBA: How to set field value back to old value if Form_Error event is triggered

来自分类Dev

How to pass interpolated value as an argument into a javascript function in angularjs

来自分类Dev

Instantiating a class and then pass it to setInterval

来自分类Dev

Pass value to fragment

来自分类Dev

push_back(value)自动减少1

来自分类Dev

How to use public class frome .java file in other processing tabs?

来自分类Dev

How to import a custom java class to my Antlr Grammar?

来自分类Dev

How to dynamically add interface to existing Java class in Groovy

来自分类Dev

How to get name of the object's from a different class in Java

来自分类Dev

Surround Game in Java using enum class - how to find a winner

来自分类Dev

how to send data back with dismissViewController

来自分类Dev

How to manipulate the back button of a browser

来自分类Dev

如何在Java中制作echo 1> / sys / class / gpio / gpio18 / value

来自分类Dev

How to Access private parameterized method of private inner class with in a static class in JAVA

来自分类Dev

Can I pass a Class type as a procedure parameter

来自分类Dev

Is it possible to get String value back from its hash code?

来自分类常见问题

Subclassing a Java Builder class

来自分类Dev

Java class to Clojure map

来自分类Dev

Java中的List <Class>

来自分类Dev

.class转换为.java?

来自分类Dev

Java泛型.class

来自分类Dev

Java 错误 - .class 预期

来自分类Dev

How to pass variables into NodeJS modules?

来自分类Dev

How to pass arguments as tuple to odeint?

来自分类Dev

How to pass a hidden recaptcha with mechanize?

Related 相关文章

  1. 1

    Java Pass By Value交换方法

  2. 2

    How to pass global volatile unsigned char to class

  3. 3

    How to Pass a Value From a Window to a UserControl in WPF

  4. 4

    Django Templates: How to Pass PK/IDs of Displayed Objects Back to the Views.py file from the Template?

  5. 5

    MS-Access VBA: How to set field value back to old value if Form_Error event is triggered

  6. 6

    How to pass interpolated value as an argument into a javascript function in angularjs

  7. 7

    Instantiating a class and then pass it to setInterval

  8. 8

    Pass value to fragment

  9. 9

    push_back(value)自动减少1

  10. 10

    How to use public class frome .java file in other processing tabs?

  11. 11

    How to import a custom java class to my Antlr Grammar?

  12. 12

    How to dynamically add interface to existing Java class in Groovy

  13. 13

    How to get name of the object's from a different class in Java

  14. 14

    Surround Game in Java using enum class - how to find a winner

  15. 15

    how to send data back with dismissViewController

  16. 16

    How to manipulate the back button of a browser

  17. 17

    如何在Java中制作echo 1> / sys / class / gpio / gpio18 / value

  18. 18

    How to Access private parameterized method of private inner class with in a static class in JAVA

  19. 19

    Can I pass a Class type as a procedure parameter

  20. 20

    Is it possible to get String value back from its hash code?

  21. 21

    Subclassing a Java Builder class

  22. 22

    Java class to Clojure map

  23. 23

    Java中的List <Class>

  24. 24

    .class转换为.java?

  25. 25

    Java泛型.class

  26. 26

    Java 错误 - .class 预期

  27. 27

    How to pass variables into NodeJS modules?

  28. 28

    How to pass arguments as tuple to odeint?

  29. 29

    How to pass a hidden recaptcha with mechanize?

热门标签

归档