Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用printf对齐二维数组的输出?_Java_Printf - Fatal编程技术网

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

Java 使用printf对齐二维数组的输出?,java,printf,Java,Printf,我有三个独立的数组,一个字符串作为标题行,两个二维数组。其中一个仅在行标题的左侧具有值。另一个是double,具有随机生成的值。到目前为止,示例输出如下所示: 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

我有三个独立的数组,一个字符串作为标题行,两个二维数组。其中一个仅在行标题的左侧具有值。另一个是double,具有随机生成的值。到目前为止,示例输出如下所示:

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]+" ");
            }
        }
    }
}
import java.util.Random;
公共课平均值{//这部分随机生成分数供学生使用
公共静态int RNG1(int a){
int min=60;//为了现实起见,学生能得到的最低分数是60分
int max=100;//最大分数为100
Random rand=新的Random();
int randomNum=rand.nextInt((max-min)+1)+min;//随机数的公式
return randomNum;//返回结果
}   
公共静态void main(字符串[]args)
{
字符串[]头={“”,“测试1”,“测试2”,“测试3”,“测试平均值”};//头/列标题
字符串[][]行标题={//行标题
{“Student1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生1”,“学生,
{“Student2”、、、、、、},
{“Student3”、、、、、、},
{“Student4”、“”、“”、“”、“”、“”、“”},
{“Student5”、“”、“”、“”、“”、“”、“”},
{“平均值”、“”、“”、“”、“”、“”}
};
int a=0;
//下一行是由随机数定义的变量
//将作为测试分数插入表中
//
双一=RNG1(a);
双一双=RNG1(a);
双三分之一=RNG1(a);
双一四=RNG1(a);
双一五=RNG1(a);
双二一=RNG1(a);
双倍=RNG1(a);
双二三=RNG1(a);
双二四=RNG1(a);
双二五=RNG1(a);
双三1=RNG1(a);
双三双=RNG1(a);
双三=RNG1(a);
双三四=RNG1(a);
双三五=RNG1(a);
//以下几行是个人和测试的平均值
//
双单=(一单+二单+三单)/3;
双stutwo=(一二+二二+三二)/3;
二三=(一三+二三+三三)/3;
双stufour=(一四+二四+三四)/3;
双五=(一五+二五+三五)/3;
双测试一=(一个+一个两个+一个三个+一个四个+一个五个)/5;
双测试二=(二一+二二+二三+二四+二五)/5;
双重测试三=(三一+三二+三三+三四+三五)/5;
双测试四=(stuone+stutwo+stuthree+stufour+stufive)/5;
//测试分数和平均值的数组
双[][]测试分数=
{   
{0,一,二,三,一},
{0,onetwo,twotwo,threetwo,stutwo},
{0,一三,二三,三三,三},
{0,one-four,two-four,three-four,stufour},
{0,一五,二五,三五,斯图五},
{0,testone,testtwo,testthree,testfour}
};         
//打印出标题:
对于(int r=0;r

就我个人而言,我不知道如何使用
printf
。我假设它将用于最后一个非括号行,而不是
System.out.println(TestScores[r][4]+”)但我无法让它工作。谢谢。

互联网上有很多这样的例子。下面是一个非常类似的链接

下面是你如何获得成行排列的分数。只需删除循环的标题,在定义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]);
    }   
//为了使所有行的起点相同,我将放置8+5个空格
//您的行标题(即学生1)中的字符数加上
//第一行填充为五个空格。标题0字符串有4个空格
System.out.printf(“%s%s%s%s%s\n”,头[0],头[1],头[2],头[3],头[4]);

对于(int r=0;r),您实际想用printf实现什么?打印特定数量的小数或其他东西?