2017-04-26 72 views
0

我在嘗試在二維數組中輸入用戶輸入的單詞時遇到了困難。 我的程序需要做的是創建一個單詞搜索難題,提示用戶他或她想找到多少單詞,以及給定的單詞將會是什麼。我遇到的麻煩是我似乎無法將用戶的輸入放入2d數組中。下面是我當前的代碼:將用戶輸入的單詞放入java中的二維數組中

public static void generate(){ 
     int rows = 5; 
     int columns = 5; 
     char[][] table = new char [rows][columns]; 
     int numberwanted; 

     System.out.println("Type in the number of words you want to generate: "); 
     numberwanted = userinput.nextInt(); 
     System.out.println("Type in the words you want to generate: "); 
     for (int i = 0; i < numberwanted; i++){ 
     String words = userinput.next(); 
     char te = words.charAt(i); 
     for(int r = 0; r < rows; r++){ 
      for(int c = 0; c < columns; c++){ 
       table[r][c] = te; 
       System.out.print(table[r][c] + " "); 
      } 
      System.out.println(); 
     } 

     }// forloop 

輸出:

Type in the number of words you want to generate: 
2 
Type in the words you want to generate: 
test 
t t t t t 
t t t t t 
t t t t t 
t t t t t 
t t t t t 

目標輸出:

Type in the number of words you want to generate: 
2 
Type in the words you want to generate: 
test 
hi 
t e s t x   x x t x x 
x x x x x or x x e x x and so on... with x's being empty 
x h x x x   x x s x x 
x i x x x   x x t x x 
x x x x x   x x h i x 
+0

'char te = words.charAt(i);',你只是在第一次迭代中設置每個單詞的第一個字母,你要輸入單詞。你如何認爲這會得到每一個可能的角色,當你只有每次輸入時只做一次這樣的事情? – SomeJavaGuy

+0

歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然遇到問題,請隨時返回一個[最小,完整且可驗證的示例](http://stackoverflow.com/help/mcve),以說明您的問題。 –

回答

0

我調整了整個程序有點使其更加堅固。它如何工作:

  • 處理table的每一行。
  • 如果用戶想提供一個單詞,請求單詞。
  • 在當前的row處添加單詞(可能被截斷)。
  • 如果需要的話,可以在x的右邊填一行。

,最後使整個table

private static int ROWS = 5; 
private static int COLUMNS = 5; 

public static void main(String[] args) { 
    char[][] table = new char[ROWS][COLUMNS]; 
    int numberwanted; 
    try (Scanner in = new Scanner(System.in)) { 
     System.out.println("Type in the number of words you want to generate: "); 
     numberwanted = Math.min(in.nextInt(), ROWS); 

     for (int row = 0; row < ROWS; row++) { 
      String word = null; 
      if (row < numberwanted) { 
       System.out.println(String.format("Type in the %d. word: ", row)); 
       word = in.next(); 
      } 
      for (int col = 0; col < COLUMNS; col++) { 
       table[row][col] = word != null && word.length() > col ? word.charAt(col) : 'x'; 
      } 
     } 
    } 
    renderGrid(table); 
} 

private static void renderGrid(char[][] grid) { 
    for (int row = 0; row < ROWS; row++) { 
     for (int col = 0; col < COLUMNS; col++) { 
      System.out.print(grid[row][col] + " "); 
     } 
     System.out.println(); 
    } 
} 
+0

你好,對於這個部分:word!= null && word.length()> col? word.charAt(col):'x';你能向我解釋什麼?是指還是代表?從未見過它。 – calebeja9

+0

這意味着:如果該單詞是從用戶檢索的(值不是「null」),並且該單詞爲當前列提供了一個字符('word.length> col'),則('?')從在字符索引col('word.charAt(col)')。否則(':')使用'x'。它的[[ternary operator]](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html)。 – Harmlezz

0

您需要更新的字符串中的所有字符。在你的情況下,你只有字符串的第一個字符。

public static void generate(){ 
      int rows = 5; 
      int columns = 5; 
      char[][] table = new char [rows][columns]; 
      int numberwanted; 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Type in the number of words you want to generate: "); 
      numberwanted = sc.nextInt(); 
      System.out.println("Type in the words you want to generate: "); 
      int j=0; 
      for (int i = 0; i < numberwanted; i++){ 
      String words = sc.next(); 
      char te = ' '; 
      for(int r = 0; r < rows; r++){ 
       for(int c = 0; c < columns; c++){ 
        if(j<words.length()) te = words.charAt(j++); 
        else te = '*'; 
        table[r][c] = te; 
        System.out.print(table[r][c] + " "); 

       } 
       System.out.println(); 
      } 

      } 
} 

更新。 另外,如果你只想要對角元素。下面是它

public static void generate(){ 
      int rows = 5; 
      int columns = 5; 
      char[][] table = new char [rows][columns]; 
      int numberwanted; 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Type in the number of words you want to generate: "); 
      numberwanted = sc.nextInt(); 
      System.out.println("Type in the words you want to generate: "); 
      int j=0; 
      for (int i = 0; i < numberwanted; i++){ 
      String words = sc.next(); 
      char te = ' '; 
      for(int r = 0; r < rows; r++){ 
       for(int c = 0; c < columns; c++){ 
        if(j<words.length() && r==c) te = words.charAt(j++); 
        else te = '*'; 
        table[r][c] = te; 
        System.out.print(table[r][c] + " "); 

       } 
       System.out.println(); 
      } 

      } 
} 

希望這有助於

+0

提供你解決方案的解釋。 –

+0

在你的代碼中看到這個 - char te = words.charAt(i);但是你每次都在第i個位置改變字符......所以我加了if(j Kangkan