2016-04-21 20 views
-1

我在我的程序課上有問題,我被困在一個點我不知道在哪裏尋找修復。在2d陣列上得到一個空輸出不知道我錯了什麼地方

這裏的問題是:

世界級將包含三個私有數據域。第一個私人數據字段是一個代表世界的二維char數組。世界的大小取決於傳遞給構造函數的 。下一個私人數據字段是characterRow,最後一個私人數據字段是characterColumn。 構造函數將接受兩個值,寬度和高度。使用寬度和高度,設置2維數組的大小 。然後用短劃線填充數組, - 。 characterRow和 characterColumn數據字段都設置爲0,然後將字符'P'放置在位置 characterRow和characterColumn。 所以,如果你的世界將被打印寬度爲9或9列,高度4或4行,它會 像創建後是這樣的:

P-------- 
--------- 
--------- 
--------- 

世界級最初將包含5個公共方法。這些方法是moveUp, moveDown,moveLeft,moveRight和displayWorld。

我得到的輸出繼電器:

Enter number of rows: 
9 
Enter number of columns: 
9 

P - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 
null - null - null - null - null - null - null - null - null - 

- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
- - - - - - - - - 
Commands can be UP, DOWN, LEFT, RIGHT, or EXIT. EXIT closes the program. 
Enter command: 

我能去的地方,用戶再次然後打印正確輸入數組的零在正確的位置上的字符的大小,但隨後的陣列打印點沒有人物。我不確定我在這裏偏離了什麼地方。你能以任何方式看看並幫助我嗎?我的兩個類代碼都低於

Driver類:

import java.util.Scanner; 

public class Driver { 

public static void main(String[] args) { 
    World world = new World(); 
    System.out.println("Commands can be UP, DOWN, LEFT, RIGHT, or EXIT. EXIT closes the program."); 
    System.out.println("Enter command: "); 
    Scanner input = new Scanner(System.in); 
    while (true) 
    { 
     String s = input.nextLine(); 

      switch (s) 
      { 
       case "up": 
        world.moveUp(); 
        world.displayWorld(); 
        break; 
       case "down": 
        world.moveDown(); 
        world.displayWorld(); 
        break; 
       case "left": 
        world.moveLeft(); 
        world.displayWorld(); 
        break; 
       case "right": 
        world.moveRight(); 
        world.displayWorld(); 
        break; 
       case "exit": 
        break; 
      } 
     } 
    } 
} 

世界級:

import java.util.*; 

public class World 
{ 
private static final String P = "P"; 
private String[][] array; 
public World() 
{   
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter number of rows: "); 
    int crow = input.nextInt(); 
    System.out.println("Enter number of columns: "); 
    int ccol = input.nextInt(); 
    array = new String[crow][ccol]; 
    array[0][0]=P; 
    displayWorld(); 
     for(int i = 0; i < array.length; i++) 
     { 
      for (int j = 0; j < array[i].length; j++) 
      { 
       array[i][j] = new String();      
      } 
     } 
    displayWorld(); 
} 

public void displayWorld() 
{ 
    System.out.println(); 
     for(int i = 0; i < array.length; i++) 
     { 
      for (int j = 0; j < array[i].length; j++) 
      { 
       System.out.print(array[i][j] + " - "); 
      } 
      System.out.println(); 
     } 
} 

public void moveUp() 
{ 
    for(int i= 0; i < array.length; i++) 
    { 
     for (int j = 0; j < array[i].length; j++) 
     { 
      if ((array[i][j]) == " - ") 
      { 
       if (i < array.length - 1) 
       { 
        array[i][j] = " - "; 
        array[i - 1][j] = P; 
       }      
       return; 
      } 
     } 
    } 
} 

public void moveDown() 
{ 
    for(int i= 0; i < array.length; i++) 
    { 
     for (int j = 0; j < array[i].length; j++) 
     { 
      if ((array[i][j]) == " - ") 
      { 
       if (i < array.length - 1) 
       { 
        array[i][j] = " - "; 
        array[i + 1][j] = P; 
       }      
       return; 
      } 
     } 
    } 
} 

public void moveLeft() 
{ 
    for(int i= 0; i < array.length; i++) 
    { 
     for (int j = 0; j < array[i].length; j++) 
     { 
      if ((array[i][j]) == " - ") 
      { 
       if (i < array.length - 1) 
       { 
        array[i][j] = " - "; 
        array[i][j - 1] = P; 
       }      
       return; 
      } 
     } 
    } 

} 

public void moveRight() 
{ 
    for(int i= 0; i < array.length; i++) 
    { 
     for (int j = 0; j < array[i].length; j++) 
     { 
      if ((array[i][j]) == " - ") 
      { 
       if (i < array.length - 1) 
       { 
        array[i][j] = " - "; 
        array[i][j + 1] = P; 
       }      
       return; 
      } 
     } 
    } 
    } 
} 
+0

使用調試器併爲自己回答你的問題 – redFIVE

+0

好吧,所以一旦你第一次顯示世界,你的'array'在除[[0] [0]之外的每個位置都爲空,因爲你賦值'P'到它。但是,接下來你需要通過包含'P'的for循環來擦除數組,因爲你從0到0都是這樣做的,這就是爲什麼你得到了你所得到的結果。 *這一切發生在你的世界建築師* – 3kings

回答

1
public World(){   
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter number of rows: "); 
    int crow = input.nextInt(); 
    System.out.println("Enter number of columns: "); 
    int ccol = input.nextInt(); 
    array = new String[crow][ccol]; 
    //array[0][0]=P; 
    //displayWorld(); 
     for(int i = 0; i < array.length; i++) 
     { 
      for (int j = 0; j < array[i].length; j++) 
      { 
       if(i == 0 && j == 0){ 
       array[i][j] = P; 
       }else{ 
       array[i][j] = new String(); 
       }      
      } 
     } 
    displayWorld(); 
    } 

在構造函數中,你有你的填充陣列之前一個displayWorld方法調用。然後,當你填充它時,你重新填充你的位置[0] [0],P消失。通過使用上面的代碼,你會得到你想要的。

我希望我幫助你!

相關問題