0

我正在编写一个程序,该程序接受五个学生的用户输入和每个学生的四个测试分数并将它们放入数组中。然后它平均学生的分数并给出一个字母等级。该程序还需要一个不接受低于 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());
      }
4

3 回答 3

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 类中是什么等价物。

于 2013-01-28T00:22:01.167 回答
0

您要做的是流量控制。最简单的方法是:

1)让你的方法返回一个布尔值。如果方法正确退出(所有数据输入) return true,如果方法必须错误退出(如本例),则返回false(在本例中,从if控制条件的内部。

2) 如果方法发现错误,则抛出异常。

在这两种情况下,调用您的方法的逻辑都必须检查抛出的返回值/异常,并在需要时再次调用该方法。

于 2013-01-28T00:29:30.227 回答
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());
  }
于 2013-01-29T20:53:24.693 回答