使用printf对齐二维数组的输出?

时间:2014-03-05 13:18:59

标签: java printf

我有三个独立的数组,一个是标题行的字符串,另外两个是二维数组。行标题的左侧只有一个值。另一个是双倍的并且具有随机生成的值。到目前为止,示例输出如下所示:

Test 1     Test 2     Test 3     Test Average   
Student1 65.0 61.0 64.0 63.333333333333336 
Student2 63.0 64.0 80.0 69.0 
Student3 60.0 100.0 89.0 83.0 
Student4 80.0 62.0 77.0 73.0 
Student5 68.0 75.0 68.0 70.33333333333333 
Averages 67.2 72.4 75.6 71.73333333333333 

以下是代码:

import java.util.Random;
public class averages { //This part randomly generates scores to use for the students
public static int RNG1(int a) { 
    int min=60;//The minimum score a student can get is 60, for realism purposes
    int max=100;//Te max score is 100
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;//Formula for a random number
        return randomNum;//Return result
    }   
public static void main(String[] args)
{
    String [] Header= { "    ", "Test 1", "Test 2", "Test 3", "Test Average" };//Header/column titles
    String [][] RowTitles= {//The Row titles
              {"Student1", "", "", "", ""},
              {"Student2", "", "", "", ""},
              {"Student3", "", "", "", ""},
              {"Student4", "", "", "", ""},
              {"Student5", "", "", "", ""},
              {"Averages", "", "", "", ""}
            };
    int a=0;        
    //The next bunch of lines are variables defined by random numbers
    //That will be inserted into the table as test scores
    //
        double oneone=RNG1(a);
        double onetwo=RNG1(a);
        double onethree=RNG1(a);
        double onefour=RNG1(a);
        double onefive=RNG1(a);
        double twoone=RNG1(a);
        double twotwo=RNG1(a);
        double twothree=RNG1(a);
        double twofour=RNG1(a);
        double twofive=RNG1(a);
        double threeone=RNG1(a);
        double threetwo=RNG1(a);
        double threethree=RNG1(a);
        double threefour=RNG1(a);
        double threefive=RNG1(a);

        //The following lines are averages of the individuals and of the tests
        //
        double stuone= (oneone+twoone+threeone)/3;
        double stutwo= (onetwo+twotwo+threetwo)/3;
        double stuthree= (onethree+twothree+threethree)/3;
        double stufour= (onefour+twofour+threefour)/3;
        double stufive= (onefive+twofive+threefive)/3;

        double testone= (oneone+onetwo+onethree+onefour+onefive)/5;
        double testtwo= (twoone+twotwo+twothree+twofour+twofive)/5;
        double testthree= (threeone+threetwo+threethree+threefour+threefive)/5;
        double testfour= (stuone+stutwo+stuthree+stufour+stufive)/5;        
        //array for test scores and averages
        double [][] TestScores=
            {   
                {0, oneone, twoone, threeone, stuone},
                {0, onetwo, twotwo, threetwo, stutwo},
                {0, onethree, twothree, threethree, stuthree},
                {0, onefour, twofour, threefour, stufour},
                {0, onefive, twofive, threefive, stufive},
                {0, testone, testtwo, testthree, testfour}
            };         
        //Prints out header:
        for (int  r = 0;  r < Header.length;  r++)
        {
            if(r < Header.length-1)
            {
                System.out.print("  "+Header[r]+"   ");
        } else {
                System.out.println("  "+Header[r]+"   ");
            }                   
        }       
        //Prints out student names and test scores
        for (int  r = 0;  r <= 5;  r++)
        {   
            for (int  c = 0;  c < 4;  c++)
            {              
                if(c==0){System.out.print(RowTitles[r][c]+" ");
            }                  
            if(c>0){
                System.out.print(TestScores[r][c]+" ");
            }                  
            if(c==3){
                System.out.println(TestScores[r][4]+" ");
            }
        }
    }
}

对于我的生活,我无法弄清楚如何使用printf。我假设它将在最后一个非括号行中使用,而不是System.out.println(TestScores[r][4]+" ");,但我无法使其工作。感谢。

1 个答案:

答案 0 :(得分:0)

互联网上有很多例子。以下是一个非常相似的链接。

http://www.dreamincode.net/forums/topic/158604-printf-and-alignment/

以下是如何获得按行排列的成绩。只需删除标题for循环,在定义TestScores数组后,将以下代码添加到循环的第一个打印测试分数中。然后注释掉内部for循环。

    //To make the starting point the same on all lines I will put 8 + 5 spaces
    //which is the number of chracters in your row titles (ie.. Student1) plus
    //Five spaces for the first row padding. Your header 0 string has 4 spaces
    System.out.printf("         %s%s     %s     %s     %s\n",Header[0],Header[1],Header[2],Header[3],Header[4]);
    for (int  r = 0;  r <= 5;  r++)
    {     
        //Now do the simlar format but include padding for numbers so the line up
        // on the right. So Title + pad + row head + pad + row head + pad....etc pad + row head = 11
        //So use 11.2 to define the grades which are floats... 11.2f
        System.out.printf("%s%11.2f%11.2f%11.2f%11.2f\n",RowTitles[r][0],TestScores[r][1],TestScores[r][2],TestScores[r][3],TestScores[r][4]);
    }