0

我正在阅读 2 个数组它们就像:

{xxxxx, yyy, aaaaaa, rrrrrrrr}

{222, 44, 55, 77}

我想输出为

xxxxx    === 222
yyy      ===  44
aaaaaa   ===  55
rrrrrrrr ===  77

我该如何进行格式化?

我正在努力实现我的目标

System.out.println(array[1] + "===" + array1[1]);

我怎样才能做到这一点?

4

6 回答 6

2
String[] array = new String[]{"xxxxx", "yyy", "aaaaaa", "rrrrrrrr"};
String[] array1 = new String[]{"222", "44", "55", "77"};
for (int i = 0; i < array.length; i++) {
    System.out.println(String.format("%-9s===%4s", array[i], array1[i]));
}

这是格式化程序文档

于 2012-10-03T07:32:58.017 回答
0
int[] array1 = {222,44,55,77};
String[] array ={"xxxxx", "yyy", "aaaaaa", "rrrrrrrr"};

    for(int i=0; i<array.length; i++)
    System.out.println(array[i] + " ===== " + array1[i]);
于 2012-10-03T07:23:28.073 回答
0

迭代一个循环。

String[] array = {"xxxxx","yyy","aaaaaa","rrrrrrrr"};
        String[] array1 = {" 222","44","55","77"};
        for (int i = 0; i < array1.length && i<array.length; i++) {
            System.out.println(array[i]+"==="+array1[i]);
        }
于 2012-10-03T07:24:21.053 回答
0
    String array1[] = {"xxxxx","yyy","aaaaaa"};
    int[] array2[] = {222,44,55,77};
    if(array1.length==array2.length) { 
     for(int i=0; i<array1.length; i++) {
    system.out.println(array1[i]+ "==="+array2[i]);
   }
于 2012-10-03T07:24:57.907 回答
0

假设您的 println 调用输入错误(缺少一个“+”),如果您想在输出中添加标签,请使用\t

此外,您需要使用循环来打印所有数组:

for(int i=0;i<array.length;i++){
    System.out.println(array[i] + "\t===\t" + array1[i]);
}
于 2012-10-03T07:29:04.520 回答
0

您需要使用printf方法格式化输出..

     String[] array1 = {"xxxxx","yyy","aaaaaa", "zzz"};
     int[] array2 = {222 ,44 ,55 ,77};

     if(array1.length==array2.length) { 

         for(int i=0; i<array1.length; i++) {
             System.out.printf("%-7s===%4d", array1[i], array2[i]);
             System.out.println();
         }
    }

使用%-7s左对齐您的first string.. 宽度为 7
使用%4d右对齐您的最后一个值.. 宽度为 4

于 2012-10-03T07:49:23.927 回答