如何打印2D数组中的特定行或列。例如,我想打印列2中的行它应该&;IJKL"或者打印第3行中的列,它应该"DHLP">
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] word = {{"A", "B", "C", "D", },
{"E", "F", "G", "H", },
{"I", "J", "K", "L", },
{"M", "N", "O", "P", }};
for (int row = 0; row < word.length; row++) {
for (int col = 0; col < word[row].length; col++) {
System.out.print(word [row][col] + " ");
}
System.out.println();
}
}
}
试试这个:
import java.util.Scanner;
公共类MyMainClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] word = { { "A", "B", "C", "D", }, { "E", "F", "G", "H", }, { "I", "J", "K", "L", },
{ "M", "N", "O", "P", } };
for (int row = 0; row < word.length; row++) {
for (int col = 0; col < word[row].length; col++) {
//To Print Second Row
if(row==2) {
System.out.print(word[row][col] + " ");
}
//To Print Third Column
if(col==3) {
System.out.print(word[row][col] + " ");
}
}
System.out.println();
}
}
}
你只需要在if条件中指定你想要显示的行/列…