私は、5人の学生のユーザー入力と学生ごとに4つのテストスコアを取り、それらを配列に入れるプログラムを書いています。次に、生徒のスコアを平均して、成績を出します。プログラムには、0未満および100を超えるスコアを受け入れない入力検証も必要です。
これまでのところ、私のプログラムはユーザーを検証し、無効な番号を入力したことをユーザーに通知しますが、無効な番号を入力した後にユーザーに最初からやり直すように促すことも望んでいます。以下に示すように(メソッド内で)いくつかの異なるオプションを試しましたenterData
が、いずれもユーザーに最初からやり直す必要はありません。助言がありますか?
import java.util.Scanner;
public class GradeBook {
private String[] names = new String[5];
private char[] grades = new char[5];
private double[] scores1 = new double[5];
private double[] scores2 = new double[5];
private double[] scores3 = new double[5];
private double[] scores4 = new double[5];
private double[] scores5 = new double[5];
int studentData = 0;
int studentCount = 0;
Scanner keyboard = new Scanner(System.in);
public void enterData() {
do {
System.out.println("Enter the student's name:");
String student = keyboard.nextLine();
System.out.println("Enter the student's first test score");
double score1 = Double.parseDouble(keyboard.nextLine());
if (score1 > 0 || score1 < 100) {
} else {
System.out.println("Invalid number");
continue;
}
System.out.println("Enter the student's second test score");
double score2 = Double.parseDouble(keyboard.nextLine());
if (score2 > 0 || score2 < 100) {
} else {
System.out.println("Invalid number");
System.out.println("Please start over");
continue;
}
System.out.println("Enter the student's third test score");
double score3 = Double.parseDouble(keyboard.nextLine());
if (score3 < 0 || score3 > 100)
System.out.println("Invalid number");
System.out.println("Enter the student's fourth test score");
double score4 = Double.parseDouble(keyboard.nextLine());
if (score4 < 0 || score4 > 100)
System.out.println("Invalid number");
addStudent(student, score1, score2, score3, score4);
studentData++;
} while (studentData < 5);
}
public String toString() {
StringBuilder printout = new StringBuilder();
for (int i = 0; i < names.length; i++) {
if (names[i] == null)
continue;
printout.append("Student name: " + names[i] + "\t" + "Average test score: " +
getAverage(i) + "\t" + "Grade: " + grades[i] + "\n");
}
return printout.toString();
}
private void addStudent(String inName, double scr1, double scr2, double scr3, double scr4) {
names[studentCount] = inName;
scores1[studentCount] = scr1;
scores2[studentCount] = scr2;
scores3[studentCount] = scr3;
scores4[studentCount] = scr4;
grades[studentCount] = getLetterGrade(studentCount);
studentCount++;
}
private double getAverage(int index) {
double score1 = scores1[index];
double score2 = scores2[index];
double score3 = scores3[index];
double score4 = scores4[index];
double average = (score1 + score2 + score3 + score4) / 4.0;
return average;
}
private char getLetterGrade(int index) {
double average = getAverage(index);
char grade;
if (average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >= 60)
grade = 'D';
else
grade = 'F';
return grade;
}
}
私はこれを必要以上に難しくしました。これは問題なく機能します。
System.out.println("Enter the student's third test score");
double score3 = Double.parseDouble(keyboard.nextLine());
while (score3 < 0 || score3 > 100)
{
System.out.println("Invalid score, plese reenter");
System.out.println("Enter the student's third test score");
score3 = Double.parseDouble(keyboard.nextLine());
}