Guest User

Untitled

a guest
Feb 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Created by huynq on 2/3/18.
  5. */
  6. public class NestedLoop {
  7. public static void main(String[] args) {
  8. Scanner keyboardScanner = new Scanner(System.in);
  9.  
  10. int n = 5;
  11. int m = 4;
  12.  
  13. int x = 3;
  14. int y = 1;
  15.  
  16. while(true) {
  17.  
  18. for (int j = 0; j < m; j ++) {
  19. for (int i = 0; i < n; i++) {
  20. if (i == x && j == y) {
  21. System.out.print("P ");
  22. } else {
  23. System.out.print("* ");
  24. }
  25. }
  26. System.out.println();
  27. }
  28.  
  29. // 1. Ask user which direction?
  30. System.out.print("Your move (W, A, S, D)? ");
  31. // QuaN dep TRai => QUAN DEP TRAI
  32. // QuaN dep TRai => quan dep trai
  33. String direction = keyboardScanner.nextLine();
  34.  
  35. // 2. Direction => update x, y
  36. if (direction.equalsIgnoreCase("W")) {
  37. y--;
  38. }
  39. else if(direction.equalsIgnoreCase("S")) {
  40. y++;
  41. }
  42. else if(direction.equalsIgnoreCase("A")) {
  43. x--;
  44. }
  45. else if(direction.equalsIgnoreCase("D")) {
  46. x++;
  47. }
  48.  
  49.  
  50. // Clamp
  51. if (y < 0) y = 0;
  52. if (y >= m) y = m - 1;
  53.  
  54. if (x < 0) x = 0;
  55. if (x >= n) x = n-1;
  56.  
  57. // 3. Re-print map
  58. }
  59. }
  60. }
Add Comment
Please, Sign In to add comment