2017-04-26 34 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

相关问题