如何获取调用数组的随机数?

时间:2013-11-14 02:57:40

标签: java

编写一个完整的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;
}



}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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();
}

}

希望这会有所帮助:)