方向是将数据从文件传递到studentName和studentGrade的数组中。文本文件看起来像这样:
Sandy 94 91 93 91 99 93 97 93
Randy 90 89 100 100 92 89 92 99
Randi 88 98 89 100 100 81 91 91
Mandy 82 84 99 88 98 85 95 85
Mandie 84 96 88 91 82 75 83 92
Mandi 80 84 95 83 73 82 97 89
Andy 77 98 84 70 80 70 72 94
Brandy 90 86 82 70 95 85 91 80
Brandi 78 67 79 95 92 68 98 81
Candie 89 97 72 85 60 72 94 95
有两个方法我不知道如何传递:
getStudentNameByIndex -一个以整数作为参数的方法,该参数用作names数组的索引,并返回该索引处的名称。
getGradeByIndex -一个以整数作为参数的方法,该参数用作studentGrades数组的索引,并返回该索引处的分数。
public class Main {
public static final int MAX_STUDENT = 50;
public static final String FILE_PATH = "grade1.txt";
public static final String MENU_QUIT = "Quit";
public static final String HIGHEST_GRADE = "Highest Grade";
public static final String LOWEST_GRADE = "Lowest Grade";
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String Option = " ";
char menuSelection = ' ';
double grade = 0;
ClassRoom sg = new ClassRoom();
sg.loadGrades(FILE_PATH);
System.out.println(sg.findMaxGradeIndex());
System.out.println(sg.findMinGradeIndex());
/*
* displayWelcome(); displayMenu();
*
*
* menuSelection = getValidatedSelection(input);
*
* while (menuSelection != 'Q') {
*
* while (menuSelection == 'A') { Option = HIGHEST_GRADE;
*
* System.out.printf("%.2f", grade);
*
* }{ Option = LOWEST_GRADE; grade = sg.findMinGradeIndex();
*
* }//end of else displaySelecetion(grade); }//end of loop
*
* displayFarewell();
*/
}// end of main
private static void displaySelecetion(double grade) {
System.out.printf("%.2f", grade);
}
private static void displayFarewell() {
System.out.println("End of Grading Program");
}
private static char getValidatedSelection(Scanner input) {
char selection;
selection = input.next().toUpperCase().charAt(0);
// validate selection
while (selection != 'A' && selection != 'B' && selection != 'Q') {
System.out.println("Invailid Selection.\n");
displayMenu();
selection = input.next().toUpperCase().charAt(0);
}
return selection;
}
private static void displayMenu() {
System.out.println("Display Menu:");
System.out.printf("%-4s%-10s\n", "(A)", HIGHEST_GRADE);
System.out.printf("%-4s%-10s\n", "(B)", LOWEST_GRADE);
System.out.printf("%-4s%-10s\n", "(Q)", MENU_QUIT);
System.out.println("Please Make A Selection");
}
private static void displayWelcome() {
System.out.println("Welcome to the Student Grading Program:");
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ClassRoom {
private String courseName;
private String[] studentNames = new String[50];
private double[] studentGrades = new double[50];
private int numStudents = 10;
public void loadGrades(String file) {
try {
Scanner readFile = new Scanner(new File(file));
double sum = 0;
int count = 0;
courseName = readFile.next();
while (readFile.hasNextLine()) {
studentNames[count] = readFile.next();
double grade1 = readFile.nextDouble();
double grade2 = readFile.nextDouble();
double grade3 = readFile.nextDouble();
double grade4 = readFile.nextDouble();
double grade5 = readFile.nextDouble();
double grade6 = readFile.nextDouble();
double grade7 = readFile.nextDouble();
double finalGrade = readFile.nextDouble();
sum = grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + 3 * finalGrade;
sum = sum / 10;
studentGrades[count] = sum;
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public int findMaxGradeIndex() {
double highest = 0;
for (int index = 1; index < studentGrades.length; index++) {
if (studentGrades[index] > highest)
highest = studentGrades[index];
}
return (int) highest;
}
public int findMinGradeIndex() {
double lowest = studentGrades[0];
for (int index = 1; index < 8; index++) {
if (studentGrades[index] < lowest)
lowest = studentGrades[index];
}
return (int) lowest;
}
public String getStudentNameByIndex(int name) {
int integer = 0;
return studentNames[name];
}
public double getGradeByIndex(int grade) {
return studentGrades[grade];
}
public int getnumStudents() {
return numStudents;
}
}
1条答案
按热度按时间bksxznpy1#
首先,您需要更改 findMaxGradeIndex 和 findMinGradeIndex 的返回值,因为它们返回的是成绩,而不是索引。
我引入了一个新变量 indexOf,并在访问 studentGrades 时对其赋值。
并且,随后将返回值更改为 indexOf。
从这里,您可以使用这些方法的输出作为 getStudentNameByIndex 和 getGradeByIndex 方法的输入。
输出量
至于 getGradeByIndex 方法,我不确定期望它做什么。
如果你能提供一些额外的信息,我很乐意提供一个额外的建议。
最后,代码中有几个错误,我不妨提一下。
在 loadGrades 方法中,我必须注解掉以下行。
它抛出了 readFile**Scanner 读取的输入。
对于 sum,您除以 10,并且只有 9 个值。
并且,*findMaxGradeIndex**for循环 * 跳过索引 0,因为 index 从 1 开始。
总的来说,这是一个很好的方法,它的功能正确。
一些额外的改进是使用 collection,而不是 array。
该大小是动态的,将随着您添加值而调整。