二维数组作为输入;存储在自己的二维数组中

时间:2016-05-20 10:43:22

标签: java multidimensional-array input

对于作业,我必须阅读如下输入:

. . . . . . . . . . . . . * .
. . . . . . . . . . . . * . .
. . . . . . . . . . . . * * *
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
* * * * * * * * . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .

并将其存储在包含字符串的数组中。 现在我已经想出了这个,但是,我不知道如何安排输入,使其可以存储为数组。

void readInputField(){
    String inputField; //string in which input is stored
    inputField = sc.nextLine(); //scans the input
    String[][] fieldParts; //array in which I want to store Strings of inputField
    fieldParts = new String[height][width]; //width and height are determined 
    //by earlier scanner input and correspond to the dimensions of the input array

    fieldParts = inputField.split(" "); //error on this line, how to split the input
    //as parts of the array?
}

2 个答案:

答案 0 :(得分:1)

在发布您获得的确切错误之前,无法确定问题所在。与上面的注释一样,如果您只是阅读简单字符,字符数组将是理想的。但是对于字符串,如果你逐行读取输入,那么代码就会像这样在for循环中拆分一行。 (假设用户在提供每个输入行后单击“输入”)

 $("#start").click(function(){
  function SortByalltime(a, b) {
    var aNum = a.alltime.sort();
    var bNum = b.alltime.sort();
    return ((aNum < bNum) ? -1 : ((aNum > bNum) ? 1 : 0));
  }
  $.get("https://fcctop100.herokuapp.com/api/fccusers/top/recent", function(data) {

    //here we run the function to sort the array of data before transforming it to table
    data.sort(SortByalltime);
    var table = '<table>'
    for (var i = 0; i < data.length; i++) {
      table += '<tr><td>' + data[i].alltime + '</td><td><img width=20 height=20 src="' + data[i].img + '"></td><td>' + data[i].lastUpdate + '</td><td>' + data[i].recent + '</td><td>' + data[i].username + '</td></tr>';

    }
    $('body').append(table);
  }); 
 });

答案 1 :(得分:0)

您在fieldParts [] []中存储值,这是2d数组 所以你需要拆分输入2次。

void readInputField(){
            String inputField; //string in which input is stored
            inputField = ". . . . . . . . . . . . . * .\n" +
                    ". . . . . . . . . . . . * . .\n" +
                    ". . . . . . . . . . . . * * *\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    "* * * * * * * * . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .\n" +
                    ". . . . . . . . . . . . . . .";
            int height = 10;
            int width = 10;
            String[][] fieldParts; //array in which I want to store Strings of inputField
            fieldParts = new String[height][width]; //width and height are determined
            //by earlier scanner input and correspond to the dimensions of the input array
            String val[] = inputField.split("\n");
            for(int i=0;i<height;i++){
                String wd[] = val[i].split(" ");
                for(int j=0;j<width;j++){
                    fieldParts[i][j] = wd[j];
                }
            }
        }
相关问题