2021年1月15日星期五

OOP Coordinates in grid Java wont work. How to program?

I dont get this to work. This is OOP. I am supposed to get the (x,y) coordinates in a grid when moving it up,down,left or right. I need the toString to always update for every move i take. I would prefer using arrow keys in the keybord, but i dont know how. If someone know that it would be nice for the program. I mus be able to for instance pressing up, then down, then up without it stopping.

package stateandbehavior;  import java.util.*;  public class Location {        int x;      int y;        public void up() {      boolean run = true;      while (run) {         Scanner tast = new Scanner(System.in);      String tas = tast.nextLine();      if (tas == "W" || tas == "w") {          y-=1;      }else {          run = false;      }      }  }    public void down() {      boolean run = true;      while (run) {      Scanner tast = new Scanner(System.in);      String tas = tast.nextLine();      if(tas == "S" || tas == "s") {          y+=1;      } else {          run = false;      }      }        }    public void right() {      boolean run = true;      while (run) {      Scanner tast = new Scanner(System.in);      String tas = tast.nextLine();      if (tas == "D" || tas == "d") {          x+=1;      }else {          run = false;      }      }  }    public void left() {      boolean run = true;      while(run)      Scanner tast = new Scanner(System.in);      String tas = tast.nextLine();      if(tas == "A" || tas == "a") {          x-=1;      }else {          run = false;      }  }    public int getX() {      return x;  }    public int getY() {      return y;     }    public String toString() {      return "(" + x + ","+ y + ")";  }    public static void main(String[] args) {      Location Loc = new Location();      Loc.up();      Loc.down();      Loc.right();      Loc.left();      Loc.getX();      Loc.getY();      System.out.println(Loc);      }  

}

https://stackoverflow.com/questions/65745512/oop-coordinates-in-grid-java-wont-work-how-to-program January 16, 2021 at 09:04AM

没有评论:

发表评论