2013-10-02 169 views
3

所以我想說讓int Posx;擁有一個數字,然後當用戶瀏覽一個菜單時,根據他們選擇的內容,將添加1,0或-1到int Posx。我不能讓Posx在添加或減去之後保留數字。我試圖通過數組瀏覽,我想保留2個整數作爲標記,讓我知道我現在在數組中關於x和y的位置。我怎樣才能讓計數器在while循環中工作?

tl; dr如何在do/while循環中保留一個計數器?

這裏是哪裏出了問題:

//this variable will hold if we found or not the string in the puzzle 
    boolean found = true; 

    do {  

     //Print out the array 
     for (int x = 0; x < maze.length; x++) { 
      for (int y = 0; y < maze[0].length; y++) { 
       System.out.print(maze[x][y]); 
      } 
      System.out.println(); 
     } 

     System.out.printf("You may:\n1) Move up\n2) Move down\n3) Move left\n4) Move right\n0) Quit\nYour Choice (0-4):\n"); 
     int usrAns = sc2.nextInt(); 

     if (usrAns == 0){ 
      System.out.println("Bye!\n"); 
      found = false; 
      break; 
     }   

     //arrays to hold the direction. 
     int[] movx ={ -1, 0, 0, 1}; 
     int[] movy ={ 0, -1, 1, 0}; 

     //Array to hold the position. 
     int Posx = 0; 
     int Posy = 0;  

     if (usrAns == 1){ 
      if(check(Posx, Posy, maze, movx[1], movy[1])){ 
        System.out.println("Cannot move past cave boundary! Try something else.\n"); 
        continue; 
       } 
       else{ 
        Posy = Posy - 1; 
        System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy); 
        continue; 
       } 
     } 
     if (usrAns == 2){ 
      if(check(Posx, Posy, maze, movx[2], movy[2])){ 
        System.out.println("Cannot move past cave boundary! Try something else.\n"); 
        continue; 
       } 
       else{ 
        Posy= Posy + 1; 
        System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy); 
        continue; 
       } 
     } 
     if (usrAns == 3){ 
      if(check(Posx, Posy, maze, movx[0], movy[0])){ 
        System.out.println("Cannot move past cave boundary! Try something else.\n"); 
        continue; 
       } 
       else{ 
        Posx = Posx - 1; 
        System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy); 
        continue; 
       } 
     } 
     if (usrAns == 4){ 
      if(check(Posx, Posy, maze, movx[3], movy[3])){ 
        System.out.println("Cannot move past cave boundary! Try something else.\n"); 
        continue; 
       } 
       else{ 
        Posx =Posx + 1; 
        System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy); 
        continue; 
       } 
     } 
     while (usrAns >= 5 || usrAns < 0){ 
      System.out.println("Please enter a number between 0 and 4:\n"); 
      usrAns = sc2.nextInt(); 
      if (usrAns == 0){ 
       System.out.println("Bye!\n"); 
       found = false; 
       break; 
      } 

     } 
    }while(found); 

任何意見,建議,或技巧將不勝感激。

回答

5

我認爲問題就出在這兩條線

//Array to hold the position. 
    int Posx = 0; 
    int Posy = 0; 

您重置POSX和波西每次迭代。嘗試在while循環之外設置它們。像這樣(你不想一遍又一遍地初始化它們)。同爲MOVX和movy:

//arrays to hold the direction. 
int[] movx ={ -1, 0, 0, 1}; 
int[] movy ={ 0, -1, 1, 0}; 

//Array to hold the position. 
int Posx = 0; 
int Posy = 0;  

do { 
    //Print out the array 
    for (int x = 0; x < maze.length; x++) { 
    ... 
    ... 

爲了提高代碼的可讀性,你可以檢查出switch statement,並嘗試鬆多continue語句:

switch(usrAns) { 
     case 1: 
      if(check(Posx, Posy, maze, movx[1], movy[1])){ 
       System.out.println("Cannot move past cave boundary! Try something else.\n"); 
      } 
      else{ 
       Posy = Posy - 1; 
       System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy); 
      } 
      break; 
     case 2: 
     ... 
     ... 
     default: 
      while (usrAns >= 5 || usrAns < 0){ 
       System.out.println("Please enter a number between 0 and 4:\n"); 
       usrAns = sc2.nextInt(); 
       if (usrAns == 0){ 
        System.out.println("Bye!\n"); 
        found = false; 
        break; 
       } 
      } 
      break; 
     } 
+1

不僅如此,我們不需要初始化Posx和Posy,我們不想這樣做,因爲它每次循環都會重置我們的位置。 – 3yakuya

+0

@Byakuya謝謝,編輯我的帖子;-) –

+0

謝謝!不能相信這件事很簡單... – Drieke

相關問題