-
Notifications
You must be signed in to change notification settings - Fork 1
/
Movement.java
39 lines (36 loc) · 1.47 KB
/
Movement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
public class Movement{
public static void main(String[] args){
int[] CurrentCoordinate= {0,0};
System.out.println("Input U, D, L, and R to move, input 'bye' to end.");
System.out.println("Current Position:[0,0]");
Scanner scanner = new Scanner(System.in);
String move =" ";
while(!move.equals("bye")){
move = scanner.nextLine();
if (CurrentCoordinate[0]==0 && move.equals("L")){
System.out.println("You are at the most left, cannot go left. Input again:");
} else if (CurrentCoordinate[0]==10 && move.equals("R")){
System.out.println("You are at the most right, cannot go right. Input again:");
} else if (CurrentCoordinate[1]==0 && move.equals("D")){
System.out.println("You are at the most bottom, cannot go down. Input again:");
} else if (CurrentCoordinate[1]==14 && move.equals("U")){
System.out.println("You are at the most top, cannot go up. Input again:");
} else {
if (move.equals("U")){
CurrentCoordinate[1]+=1;
}else if(move.equals("D")){
CurrentCoordinate[1]-=1;
}else if(move.equals("L")){
CurrentCoordinate[0]-=1;
}else if(move.equals("R")){
CurrentCoordinate[0]+=1;
}else {
System.out.println("Illegal input. Input again:");
}
}
System.out.printf("Current Position:[%d,%d]",CurrentCoordinate[0],CurrentCoordinate[1]);
System.out.println();
}
}
}