I don't 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 keyboard, but I don't know how. If someone know that it would be nice for the program. I must be able to - for example - press 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);
}
}