我的函数需要接受两个参数,分别称为 data、player。参数数据是一个二维字符串数组,其中包含 nba.txt 的数据。电子表格。参数player是一个字符串。 该函数查找数据中第 0 列(小写版本)包含作为子字符串存储在player(也是小写版本)中的字符串的所有行。对于所有此类行,它会打印相应玩家的统计数据。
我的代码:
public static void print_player_info(String[][] data, String player)
{
String name = player.toLowerCase();
for(int i = 0; i<data[0].length; i++)
{
String namecolumn = data[i][0];
String rownames = data[0][i];
for(int j = 0; j<data[0].length; j++)
{
String temp = Arrays.toString(data[j]);
if(temp.toLowerCase().contains(player))
{
System.out.println(rownames+": "+data[j][i]);
}
}
}
}
如果我的输入一次只检索一个人,它就会以所需的方式工作。
示例:输入:“lebro” 输出:
player: LeBron James
games played: 69
minutes per game: 36.1
etc...
如果我的输入碰巧检索到多个人,它会打印 it stacked:input "jam"
player: James Harden
player: LeBron James
games played: 81
games played: 69
etc...
当我输入“jor”时,它应该找到多个包含“jor”的名称,但它什么也不输出。我想知道如何解决这个问题以及之前的输入:
player: James Harden
games played: 81
etc...
player: LeBron James
games played: 69
etc...
最佳答案
public static void print_player_info(String[][] data, String player){
// lower case the search string
String searchString = player.toLowerCase();
// check every row in the data table except the first
// because it has the column names in it.
for(int row = 1; row < data.length; row++){
// get the player name in this row
String playerInThisRow = data[row][0];
// lower case it and check if it contains the search string
if(playerInThisRow.toLowerCase().contains(searchString)){
// go throu every column in this row
for(int column = 0; column < data[0].length; column++){
// and get the column name and the content
String columnContent = data[row][column];
String columnName = data[0][column];
// and print it
System.out.println(columnName +": " + columnContent);
}
}
}
}
关于java - 如何按特定顺序打印二维数组的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916647/