Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Created by huynq on 2/3/18.
- */
- public class NestedLoop {
- public static void main(String[] args) {
- Scanner keyboardScanner = new Scanner(System.in);
- int n = 5;
- int m = 4;
- int x = 3;
- int y = 1;
- while(true) {
- for (int j = 0; j < m; j ++) {
- for (int i = 0; i < n; i++) {
- if (i == x && j == y) {
- System.out.print("P ");
- } else {
- System.out.print("* ");
- }
- }
- System.out.println();
- }
- // 1. Ask user which direction?
- System.out.print("Your move (W, A, S, D)? ");
- // QuaN dep TRai => QUAN DEP TRAI
- // QuaN dep TRai => quan dep trai
- String direction = keyboardScanner.nextLine();
- // 2. Direction => update x, y
- if (direction.equalsIgnoreCase("W")) {
- y--;
- }
- else if(direction.equalsIgnoreCase("S")) {
- y++;
- }
- else if(direction.equalsIgnoreCase("A")) {
- x--;
- }
- else if(direction.equalsIgnoreCase("D")) {
- x++;
- }
- // Clamp
- if (y < 0) y = 0;
- if (y >= m) y = m - 1;
- if (x < 0) x = 0;
- if (x >= n) x = n-1;
- // 3. Re-print map
- }
- }
- }
Add Comment
Please, Sign In to add comment