0

编写一个完整的 Java 程序,构建一个虚构的成绩册并将其打印到命令行。成绩簿数据必须构造为 10 行的多维数组。每行有六个元素:第一个位置的学生姓名、四个作业分数和最后一个位置的作业平均值。作业分数由第二种方法产生(返回),该方法使用 Math.random() 和数字缩放来产生 0-100 之间的数字。平均值是根据四个分配值计算的。您的程序应至少包含三种方法,每种方法都完成部分工作。这是我的代码:

public static void main(String[] args) {
        // TODO code application logic here
        gradebook();
    }
    public static void gradebook(){


        findingNumbers();
        final int NAMES = 6;
        final int ASSIGNMENTS = 4;
        String [] names ={"Mike", "Jayson", "Ben","Luke", "Chris", "Joseph"};




        System.out.println("Name      Assign 1    Assign 2    Assign 3   Assign 4    Average");
        for (int i = 0; i < NAMES; i++){
            System.out.printf("%10s", names[i]);
            double total = 0;
            double average = 0;

            for (int j = 0; j < ASSIGNMENTS; j++){

                for(int k = 0; k < ASSIGNMENTS; k++){
                    int[] assingments = new int[4];
                    assingments[k] =(int) (Math.random()*100);
                }
                System.out.printf("%8d", assingments[i][j]);
                total = total + assingments[i][j];
                average = total/4;

            }
           System.out.printf("%2d", average);

        }

        }
    public static double findingNumbers(){
        double randomNumber = 0;
        randomNumber = Math.random();
        randomNumber = randomNumber *100;
               int randomInteger = 0;
               randomInteger = (int) randomNumber; 
        return randomInteger;
}



}
4

2 回答 2

0

多维数组是具有多于一维的数组。您正在创建一个只有一维的数组。

于 2013-11-14T03:04:21.743 回答
0

这是我解决问题的方法:

public class ReportCard {

String[][] report = new String[11][6]; //first row contains column names

// constructor that initializes the first row
public ReportCard(){
    report[0][0] = "Name";
    report[0][1] = "Sub1";
    report[0][2] = "Sub2";
    report[0][3] = "Sub3";
    report[0][4] = "Sub4";
    report[0][5] = "Average";
}

// function to display report card
public void displayReportCard(){        
    for(int i=0; i<11; i++){
        for(int j=0; j<6; j++){
            System.out.print(report[i][j] + "\t");
        }
        System.out.println();
    }       
}

// fill the marks with random numbers between 0 and 100
public void fillReportCard(){
    for(int i=1; i<11; i++){
        for(int j=1; j<6; j++){
            report[i][j] = String.valueOf(Math.floor(Math.random()*100)); 
        }
    }
}

//function to compute average of columns 1-4
public void getAverage(){
    double sum = 0;
    for(int i=1; i<11; i++){
        for(int j=1; j<5; j++){
            sum = sum + Double.parseDouble(report[i][j]);               
        }
        report[i][5] = String.valueOf(sum/4);
    }
}

public static void main(String[] args){
    ReportCard r1 = new ReportCard();
    r1.fillReportCard();
    r1.getAverage();
    r1.displayReportCard();
}

}

希望这可以帮助 :)

于 2013-11-14T06:22:35.370 回答