龙空技术网

如何在Java中将两个类模块连接在一起?

解影视 128

前言:

如今大家对“java两个类”大体比较关切,我们都想要剖析一些“java两个类”的相关资讯。那么小编同时在网摘上汇集了一些对于“java两个类””的相关知识,希望朋友们能喜欢,兄弟们一起来学习一下吧!

我有一个包含五个的程序。txt文件。这些文件被读入并放入不同的数组中。一个是一系列的名字。另外四个是带有测试分数的数组。

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

问题是:我在另一页上有主程序课和另一个成绩册课(做计算)。如何将这两页连接在一起?

例如:如果studentName是'Andrew',并且在studentNameArray中找到了它,我选择了我想要查看的测试分数的1(scoreOneArray),说出数字88。我的程序找到了“Andrew”和“88”。它没有做的是将“Andrew”和“88”发送到成绩册,让数据计算测试百分比(88/100),并找到相应的字母成绩(在本例中为“B”)。最后,打印学生姓名、考试成绩(88%)和字母成绩。

Program (main):

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();    }}

Calculations(GradeBook):

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");        }    }} 

标签: #java两个类