如何根据我想要的输出打印数组?逐行

时间:2016-07-11 05:48:25

标签: java arrays println

我需要一些帮助来逐行打印数组,但它有两个不同的数组......

目前,我的代码允许我打印所有姓名,按照分数,按照平均分数和分数进行打印。但是我需要根据学生的姓名和分数输出它们。例如,看看图片......

  

现状:Current

     

预期输出..:Expected

public static void main(String[] args) {

        // 1D Array for Student Names
        String[] names = new String[10];

        //2D Array for test score
        double[][] scores = new double[10][5];

        //1D Array for grades
        char[] grades = new char[5];

        getNames(names);
        getScores(scores);

        print1Array(names);
        print2Array(scores);

        calculateAvg(scores);

        classAvg(scores, names);

    } // PSVMain

    // Using FOR loop to read and store each name 
    public static void getNames(String[] data) {
        Scanner kb = new Scanner(System.in);

        for (int r = 0; r < data.length; r++) {
            System.out.println("Enter Student names : ");

            data[r] = kb.next();
        } // For R
    } // getInput

    // Using FOR loop to read and store each scores
    public static void getScores(double[][] data) {
        Scanner kb = new Scanner(System.in);

        for (int r = 0; r < data.length; r++) {
            System.out.println("Enter number for row " + (r + 1));

            for (int c = 0; c < data[r].length; c++) {
                System.out.println("Enter number for column " + (c + 1));
                data[r][c] = kb.nextInt();

            } // For C
        } // For R
    } // getInput

    public static void classAvg(double[][] data, String[] name) {

        double[] allAverage = new double[data.length];

        double totalAvg = 0;
        double classAvg = 0;
        double average = 0;

        for (int r = 0; r < data.length; r++) {
            double total = 0;

            for (int c = 0; c < data[r].length; c++) {
                total = total + data[r][c];
            } // For C

            average = total / data[r].length;

            // This is the Average for each ROW
            allAverage[r] = average;

            totalAvg = totalAvg + allAverage[r];

        } // For R

        classAvg = (totalAvg / name.length);

        System.out.println("The class Average score is : " + classAvg);

    } // classAvg

    // This method will calculate the Average and assigning Grades
    public static void calculateAvg(double[][] data) {
        double[] allAverage = new double[data.length];
        char[] allGrades = new char[data.length];

        double average = 0;

        for (int r = 0; r < data.length; r++) {
            double total = 0;

            for (int c = 0; c < data[r].length; c++) {
                total = total + data[r][c];
            } // For C

            average = total / data[r].length;

            // This is the Average for each ROW
            allAverage[r] = average;

            if (average >= 85 && average <= 100) {
                allGrades[r] = 'A';
            } else if (average >= 75 && average <= 84) {
                allGrades[r] = 'B';
            } else if (average >= 65 && average <= 74) {
                allGrades[r] = 'C';
            } else if (average >= 50 && average <= 64) {
                allGrades[r] = 'D';
            } else if (average < 50) {
                allGrades[r] = 'F';
            } // If

            // Print each ROWS average
            System.out.println("The average for this row is : " + average);

            // Print each ROWS grades
            System.out.println("The Grades for this row is : " + allGrades[r]);

            System.out.println();

        } // For R

    } // Grades

    // This method allows it to print 1D Array
    public static void print1Array(String[] data) {

        for (int r = 0; r < data.length; r++) {

            System.out.print("The student names are " + data[r] + "   ");

            System.out.println();

        } // For R

    } //print 1D Array

    // This method allows it to print 2D Array
    public static void print2Array(double[][] data) {

        System.out.println();
        System.out.println("This are the respective student's score");

        for (int r = 0; r < data.length; r++) {

            for (int c = 0; c < data[r].length; c++) {

                System.out.print(data[r][c] + "   ");
            } // For C

            System.out.println();
        } // For R

    } // Print 2D Array

1 个答案:

答案 0 :(得分:-1)

此伪代码可以帮助您实现所需的结果

For i=0 till names.length
   print names[i]
   total=0
   For j=0 till scores[i].length
      print scores[i][j]
      total=total+scores[i][j]
   End For
   Print Average = total/scores[i].length;
   decide Grade on basis of average and print
End For