我有一个包含五个.txt文件的程序.这些文件被读入并放入不同的数组中.一个是名称array.其他四个是带有测试分数的array.

目前,该程序确实正确创建了array.接下来,程序将计算并显示每个测试的平均值(这很好).然后程序会提示用户输入名称.如果找到一个名称,一个新的菜单将提示用户 Select 他们想要数据的测试.(这很好用).

问题:我在另一个页面上有主程序类和另一个成绩册类(进行计算).我怎么把这两页连在一起呢?

例如:如果StudentName是‘Andrew’,并且在StudentNameArray中找到它,并且我 Select 1作为我想要查看的测试分数(ScoreOneArray),那么就说数字88.我的程序找到‘Andrew’和‘88’.它不做的是将‘Andrew’和‘88’发送到评分簿,让数据计算测试百分比(88/100),并找到相应的字母等级(在本例中为‘B’).最后,打印学生姓名、考试成绩(88%)和字母分数.

程序(主):

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        double test1Calculation;
        double test2Calculation;
        double test3Calculation;
        double test4Calculation;
        int i, j, k, l;
        int testOneSum = 0;
        int testTwoSum = 0;
        int testThreeSum = 0;
        int testFourSum = 0;
        int checker = 0;
        int index=0;
        String choice;

        Scanner students = new Scanner(new File("names.txt"));
        Scanner TestOne = new Scanner(new File("testOne.txt"));
        Scanner TestTwo = new Scanner(new File("testTwo.txt"));
        Scanner TestThree = new Scanner(new File("testThree.txt"));
        Scanner TestFour = new Scanner(new File("testFour.txt"));

        String token1 = "";
        List<String> studentName = new ArrayList<>();
        while (students.hasNext()) {
            // find next line
            token1 = students.next();
            studentName.add(token1);
        }

        String[] studentNameArray = studentName.toArray(new String[0]);

        List<Integer> scoreOne = new ArrayList<>();

        // while loop
        while (TestOne.hasNext()) {
            // find next line
            Integer token2 = TestOne.nextInt();
            scoreOne.add(token2);
        }

        Integer[] scoreOneArray = scoreOne.toArray(new Integer[0]);

        List<Integer> scoreTwo = new ArrayList<>();

        // while loop
        while (TestTwo.hasNext()) {
            // find next line
            Integer token3 = TestTwo.nextInt();
            scoreTwo.add(token3);
        }

        Integer[] scoreTwoArray = scoreTwo.toArray(new Integer[0]);

        List<Integer> scoreThree = new ArrayList<>();

        // while loop
        while (TestThree.hasNext()) {
            // find next line
            Integer token4 = TestThree.nextInt();
            scoreThree.add(token4);
        }

        Integer[] scoreThreeArray = scoreThree.toArray(new Integer[0]);

        List<Integer> scoreFour = new ArrayList<>();

        // while loop
        while (TestFour.hasNext()) {
            // find next line
            Integer token5 = TestFour.nextInt();
            scoreFour.add(token5);
        }

        Integer[] scoreFourArray = scoreFour.toArray(new Integer[0]);

        for (i = 0; i < scoreOneArray.length; i++)
            testOneSum += scoreOneArray[i];
        test1Calculation = (double) testOneSum / 5;

        for (j = 0; j < scoreTwoArray.length; j++)
            testTwoSum += scoreTwoArray[j];
        test2Calculation = (double) testTwoSum / 5;

        for (k = 0; k < scoreThreeArray.length; k++)
            testThreeSum += scoreThreeArray[k];
        test3Calculation = (double) testThreeSum / 5;

        for (l = 0; l < scoreFourArray.length; l++)
            testFourSum += scoreFourArray[l];
        test4Calculation = (double) testFourSum / 5;

        System.out.println("The average for test one is: " + test1Calculation);
        System.out.println("The average for test two is: " + test2Calculation);
        System.out.println("The average for test three is: " + test3Calculation);
        System.out.println("The average for test four is: " + test4Calculation);

        Scanner studentSearch = new Scanner(System.in);
        System.out.println("Enter student name : ");
        String foundStudent = studentSearch.nextLine();
        boolean found = Arrays.stream(studentNameArray).anyMatch(t -> t.equals(foundStudent));
        if (found) {
            index++;
            System.out.println(foundStudent + " is found.");

            //menu loop
            do {
                //displayed user options
                System.out.println("1. To find score for first test");
                System.out.println("2. To find score for second test");
                System.out.println("3. To find score for third test");
                System.out.println("4. To find score for fourth test");

                //menu choices
                Scanner keyboard = new Scanner(System.in);
                System.out.print("\nEnter your choice: ");
                choice = keyboard.next();

                if (choice.equals("1")) {
                    int  score= scoreOneArray[index];

                    System.out.println(score);
                    checker = -1;
                } else if (choice.equals("2")) {
                    int  score= scoreTwoArray[index];
                    System.out.println(score);
                    checker = -1;
                } else if (choice.equals("3")) {
                    int  score= scoreThreeArray[index];
                    System.out.println(score);
                    checker = -1;
                } else if (choice.equals("4")) {
                    int  score= scoreFourArray[index];
                    System.out.println(score);
                    checker = -1;
                } else {
                    //Error message
                    System.out.println("invalid choice");
                }
            }
            while (checker != -1);
        } // End of Menu Method
        else {
            System.out.println(foundStudent + " is not found.");}
        

        students.close();
        TestOne.close();
        TestTwo.close();
        TestThree.close();
        TestFour.close();

    }
}

计算(成绩册):

import java.util.ArrayList;

public class GradeBook {

    private char[] letterGrade = {'A', 'B', 'C', 'D', 'F'};
    private ArrayList<String> names = new ArrayList<>();
    private double [][] scores = new double[5][4];

    public GradeBook(ArrayList<String> studentNames, double[][] studentScores){
        this.names = studentNames;

        for (int i = 0; i < 5; i++){
            for (int j = 0; j < 4; j++){
                scores [i][j] = studentScores[i][j];
            }
        }
    }
    public  String getName(int studentIndex){
        return names.get(studentIndex);
    }
    public double getAverage(int studentIndex){
        double total = 0;

        for (int i = 0; i < 4; i++){
            total += scores[studentIndex][i];
        }
        return (total / 4);
    }
    public char getLetterGrade(double avgScore){

        if (avgScore >= 90 && avgScore <= 100){
            return letterGrade[0];
        }
        else if (avgScore >= 80 && avgScore <= 89){
            return letterGrade[1];
        }
        else if (avgScore >= 70 && avgScore <= 79){
            return letterGrade[2];
        }
        else if (avgScore >= 60 && avgScore <= 69){
            return letterGrade[3];
        }
        else if (avgScore >= 0 && avgScore <= 59){
            return letterGrade[4];
        }
        return ' ';
    }
    public void getStudent(){

        for (int i = 0; i < names.size(); i++){

            System.out.println("\nStudent #" + (i+1)
            +"\n\t\tName: " + names.get(i)
            +"\n\t\tAverage: " + getAverage(i) + "%"
            +"\n\t\tLetter Grade: " + getLetterGrade(getAverage(i))
            +"\n\n");
        }

    }
} 

推荐答案

我不明白在成绩册上发生的事情有多少与你在你的main函数中所做的事情有关.我知道在你的main人中,你是如何根据用户 Select 的学生和考试号码得出一个单一的分数的.但是一旦您有了这个用户和分数,我就看不出它如何与GradeBook中包含的double[][] studentScores表中的数据相匹配.那是什么数据?它是从哪里来的?

您在main中的代码似乎有一个很大的问题.index在它被使用的时候总是1岁.它的值不受作为学生姓名输入的内容的影响.我想你的意思是让它成为现实.另外,我不明白你在main中得出的单个整数score是如何与评分册接受的avgScore相匹配的.但是忽略了所有这些...

看起来你只有一本成绩册,对吧?所以我认为你只需要实例化它的一个实例,然后你就可以用它来查找学生的名字,并根据学生的分数来计算分数.假设indexGradeBook中的名字列表相匹配,而您不知何故计算出averageScore,结果将如下所示…

public class Main {

    public static void main(String[] args) throws IOException {
        ...
         
        GradeBook gradeBook = new GradeBook(...);
        
        ...

        while (...) { 
            index = ...
            ...
            averageScore = ...
            ...
            studentName = gradeBook.getName(index);
            grade = gradeBook. getLetterGrade(averageScore);
        
            System.out.println(String.format("Student: %s. Grade: %s", studentName, grade));
    }

我能看到的另一个用例是,你可以从main中读取的数据计算出studentScores个表格,然后你可以用它创建一个成绩册,让它显示所有这些信息.看起来像这样...

studentScores = ...
...
GradeBook gradeBook = new GradeBook(studentScores);
gradeBook.getStudent()

Java相关问答推荐

如何在Spring Boot中创建500错误的响应正文?

Saxon 9:如何从Java扩展函数中的net.sf.saxon.expr. XPathContent中获取声明的变量

Java JAR环境(JRE)是否支持模块?

如何转换Tue Feb 27 2024 16:35:30 GMT +0800 String至ZonedDateTime类型""

在Java中如何从Executors.newFixedThreadPool(MAX_THREAD_COUNT())迁移到虚拟线程

如何使用SpringBoot中的可分页对整数作为字符串存储在数据库中时进行排序

解释左移在Java中的工作原理

将响应转换为带值的键

对字符串长度进行排序,但颠倒了顺序(最长字符串在前)

S,要对Java复制构造函数深度克隆所有属性进行单元测试,最可靠的方法是什么?

带有Health Check的Spring Boot BuildPack打破了本机映像构建过程

%This内置函数示例

没有使用Lombok生成的参数

为什么Collectors.toList()不能保证易变性

buildDir:File!&#的getter解决方案是什么?39.被抛弃

二进制数据的未知编码/序列化

如何使JOOQ渐变脚本不重新创建表未更改的类?

获取月份';s在java中非UTC时区的开始时间和结束时间

Java中计算大n和k值模10^9+7的二项式系数的乘法公式输出错误值

在外部类和内部类之间,当调用外部类内部或外部的主方法时,它们的静态初始化程序的运行顺序不同