简体   繁体   English

如何根据所需的输出打印数组? 逐行

[英]How do I print my array based on the output that I want? Row by Row

I need some help to print the arrays row by row but it's 2 different array... 我需要一些帮助来逐行打印数组,但这是2种不同的数组...

Currently My codes allows me to print all the names, follow by scores, follow by average scores and grade. 目前,“我的代码”允许我打印所有名称,分数,平均分数和等级。 However I would need to output them based on the student's name and score.. Take a look at the picture for example... 但是,我需要根据学生的姓名和分数来输出它们。例如看图片...

Current situation : Current 现状: 当前

Expected output.. : 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

This pseudo code might help you in achieving desired result 此伪代码可以帮助您获得所需的结果

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我希望我的按钮与我的 gui 中的选择按钮不在同一行? 我将如何做到这一点? - I want my button not in the same row as my choose buttons in my gui? How will I do that? 如何分别打印出二维数组每一行的平均值? - How do I to print out the average of each row of a 2D array separately? MySQL-输出中只有1行? - MySQL - I get only 1 row in my output? 如何从CSV文件输出特定行的内容? - How do I output the contents of a specific row from a CSV file? ListView中每行的颜色不同,我也想拖动该行,并且每行的背景颜色也要交换。 怎么做 - Different color for each row in a ListView, I want to drag the row and each row's background color exchange too. how to do it 我想让我的 toString 方法打印数组列表中对象的所有信息,但我得到的输出带有 [] 括号 - I want to get my toString method to print all the information of the objects in my array list but I am getting the output with [] brackets 我想从Java中的一系列数字中连续输出7个偶数 - I want to output 7 even numbers in a row from a range of numbers in java JPA Inheritance - 我想删除带有子行的父行我该怎么做 - JPA Inheritance - I want to delete parent row with child rows how do I do it 如何在 excel 中逐行打印传递的字符串 - How can I print the passing string row by row in excel 如何将JLabel与其下方的JLabel行对齐 - How do I line up my JLabel with the row of JLabels below it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM