[英]Need help in array output
I need some help on this array to show the output, but it seem like doesn't work what I expect. 我需要一些关于这个数组的帮助来显示输出,但它似乎不起作用我所期望的。
public static void main(String[] args){
String[][] record = {
{"abc","123","cbv"},
{"efg","456","cbb"},
{"hij","321","ggb"},
{"xyz","A4","ghy"}};
for (int i=0;i<4;i++){
for (int j=0;j<3;j++)
System.out.println(record[i][j]);
The Output show : 输出显示:
abc
123
cbv
efg
456
cbb
hij
321
ggb
xyz
A4
ghy
I need the output to show as : 我需要输出显示为:
abc 123 cbv
efg 456 ccb
hij 321 ggb
xyz A4 ghy
You should do: 你应该做:
for (int i=0;i<4;i++) {
for (int j=0;j<3;j++) {
System.out.print(record[i][j] + " "); // print instead of println
}
System.out.println(); // println (new row)
}
(The inner loop which prints elements in one row should not print line-breaks.) (在一行中打印元素的内部循环不应该打印换行符。)
Or, even better, use System.out.printf
to make sure all elements are equally wide (this example also uses for each loops): 或者,更好的是,使用
System.out.printf
确保所有元素都相同(此示例也用于每个循环):
for (String[] row : record) {
for (String element : row)
System.out.printf("%5s", element);
System.out.println();
}
Output: 输出:
abc 123 cbv
efg 456 cbb
hij 321 ggb
xyz A4 ghy
for (int i=0;i<4;i++){
for (int j=0;j<3;j++) {
System.out.print(record[i][j]);
}
System.out.println();
}
should do it. 应该这样做。
for (int i=0;i<4;i++) {
for (int j=0;j<3;j++) {
System.out.print(record[i][j] + " "); // print instead of println
}
System.out.println(); // println (new row)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.