我正在编写一个程序,每个学生需要用户输入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());
}
答案 0 :(得分:0)
我知道使用JOptionPanes的方法:
int n = JOptionPane.showConfirmDialog(null,
"Would you like to start over?",
"Incorrect Input",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
//start over
}
else
{
System.exit(0);
}
我猜这与System类中的等价物无关。
答案 1 :(得分:0)
您想要做的是流量控制。最简单的方法是:
1)让你的方法返回一个布尔值。如果方法正确退出(输入的所有数据)返回true
,如果方法必须退出错误(如本例所示),则返回false
(在这种情况下,从{{1}内部返回控制条件。
2)如果方法发现错误,则抛出异常。
在这两种情况下,调用方法的逻辑都必须检查抛出的返回值/异常,如果需要,再次调用该方法。
答案 2 :(得分:0)
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());
}