繁体   English   中英

如何将AVERAGE和LETTERGRADE合并到具有2D数组的Java程序中

[英]How do you incorperate an AVERAGE and LETTERGRADE in a java program which has 2D Array

我编写的程序错误最少,但是不知道应该在哪里放置“平均”或“字母”等级函数:

package org.education.tutorial;
import java.util.Scanner;
public class GradingScale
{
    public static void main (String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter the number of students attending your current session :");
        int numberOfStudents = keyboard.nextInt();
        System.out.println();
        System.out.print("Also, please enter the amount of exams taken during the duration of the course : ");
        int examScores = keyboard.nextInt();
        AssignValueToArray(numberOfStudents, examScores);
        keyboard.close();
    }
    public static void AssignValueToArray(int amountOfStudents, int amountOfExams)
    {
        int[][] overallScore = new int[amountOfStudents][amountOfExams];
        Scanner keyboardArray = new Scanner(System.in);
        int numberValue = 1;
        for (int index = 0; index < amountOfStudents; index++)
        {
            System.out.println ("\n" + "Please submit Student #" + numberValue + " 's score :" );
            for(int indexOfHomeWork=0; indexOfHomeWork < amountOfExams; indexOfHomeWork++)
            {
                overallScore[index][indexOfHomeWork] = keyboardArray.nextInt();
            }
            numberValue++;
        }
        DisplayvalueInArray(overallScore);
        keyboardArray.close();
    }
    public static void DisplayvalueInArray(int[][] overallScoreArray)
    {
        System.out.println ("The students' scores are posted below : " + "\n");
        int studentCount = 1;
        for (int index = 0; index < overallScoreArray.length; index++)
        {
            System.out.print("Grades for student " + studentCount +": ");
            for (int indexOfHomeWork = 0; indexOfHomeWork < overallScoreArray[index].length;
            indexOfHomeWork++)
            {
                System.out.print(overallScoreArray[index][indexOfHomeWork]+"\t");
            }
            System.out.println();
            studentCount++;
        }
    }

我强烈建议您在进行诸如此类的操作之前,先学习如何使用可变范围和对象。

您永远不会在类的范围内声明任何变量。

您的大多数函数不会影响其自身范围之外的任何事物,因此不应将其视为函数。 而是使用注释进行封装。

您的许多变量命名不当,而不是不是全不是数组的keyboardArray ,请执行kbkeyboardScanner

为了计算平均值和字母等级,请考虑“我是什么平均值”,并将其用作您的参数,将平均值作为您的返回类型,所以类似static int average(int[] scores)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM