如何打印数组表? [英] How to print a table of arrays?

查看:60
本文介绍了如何打印数组表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看看是否有人可以解决这个问题.

I'm here to see if anyone would be able to help out with the problem.

我正在尝试打印一张看起来像这样的表

I'm trying to print a table out which would look something like this

             Month #1      Month #2
   Person 1  $1293         $128
   Person 2  $122          $1233

我已经完成了获取数字的所有其他步骤,等等,我只是停留在获取正确的表格输出的最后一步中.

I've already done all other steps on getting the number, etc i'm just stuck in my last step of getting the correct output as a table.

int[][] table = new int[people][month];

     // Load the table with values
    for (int i=0; i < table.length; i++){     
          for (int j=0; j < table[i].length; j++){
            table[i][j] = r.nextInt(20000-1000) + 1000;
           }
      }

      // Print the table
      System.out.println("\n\nSummer Internship Salary Information:");
      for (int i=0; i < table.length; i++) {
          for (int j=0; j < table[i].length; j++)                    
             System.out.print ("Person #" + (i+1) + "$" + table[i][j] + "\t");
         System.out.println();     

      } 

数组的初始大小由用户确定.以及在表格中加载值的第一部分.这可以忽略.

the initial size of the array is determined by the user. and the 1st part of loading the table with values. That can be ignore.

我真正遇到麻烦的部分是从表中打印出来.从我现在拥有的代码中,它给出了

The part im having real trouble with is printing out into a table from. From the code i'm having now, it gives an output of

     person#1 $12312   person #1 $12312
     person#2 $12312   person #2 $12312

(请注意,数字不是正确的数字,仅是示例)

(note that the numbers are not the correct number,its just an example)

如何使它具有如下输出:

how can i enable it to have an output that looks like :

              Month#1   Month#2
    Person#1  $12312    $12312
    Person#2  $12312    $12312

在本练习中,我不允许使用call方法或JCF.

And I'm not allowed to use the call method or JCF in this exercise.

推荐答案

假定所有人的月数相同:

Assuming all people have the same number of months:

System.out.println("\n\nSummer Internship Salary Information:");
for (int j=0; j < table[0].length; j++) {
    System.out.print("\tMonth #" + (j+1));
}
for (int i=0; i < table.length; i++) {
    System.out.print("\nPerson #" + (i+1));
    for (int j=0; j < table[i].length; j++) {
        System.out.print("\t$" + table[i][j]);
    }
}
System.out.println();

请注意,Person#已从内部循环中删除,并且列标题是最先打印的.

Notice that the Person# is taken out of the inner loop, and the column headings are printed first.

还请注意,如果任何数字太宽(比制表符大),则会破坏布局.您将不得不更聪明地解决此问题(首先查找每列的最大宽度或截断值)

Also beware that if any number is too wide (wider than a tabstop) it will break the layout. You would have to be cleverer to fix that (find maximum width for each column first or truncate the values)

(已编辑,可以将制表符和换行符放在更好的位置;更少的字符串且没有尾随的制表符)

(Edited to put tabs and newlines in better places; fewer strings and no trailing tabs)

这篇关于如何打印数组表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆