如何将单个扫描仪连接到两个阵列?

How do you connect a single scanner to two arrays?

基本上,我试图询问用户的输入,并且输入应该使用单个扫描仪存储在两个数组中。使用两个会询问用户两次,这是不切实际的。代码看起来像这样

int record = 0;
Scanner midOrFinal = new Scanner(System.in);
Scanner scansubjects = new Scanner(System.in);
Scanner scangrades = new Scanner(System.in);
    
System.out.println("Press 1 to Record for Midterm");
System.out.println("Press 2 to Record for Final Term");
record = midOrFinal.nextInt();
    
int midterm[] = new int[8];
int grades[] = new int[8];
    
{ 
  if ( record == 1 )
    System.out.println("Enter 8 subjects and their corresponding grades:");

  System.out.println();
    
  int i = 0;
    
  for( i = 0; i < 8; i++ )
  {   
    System.out.println(subjects[i]);                               
    System.out.print("Enter Grade: ");               
    grades[i] = scangrades.nextInt();            
    if( i == ( subjects.length) )
      System.out.println();
  }
  System.out.println("Enter Grade Successful");       
}

如果用户选择选项 1,系统将向用户提供数组中的一些主题(我没有包括)并要求输入成绩。然后输入将进入期中或期末数组,但我似乎无法使用一台扫描仪来完成。

如果有比我提出的想法更好的想法,请分享。我对 Java 还是很陌生,也是我第一次使用 Whosebug。谢谢!

    class Main {

        public static final Scanner in = new Scanner(System.in);

        public static void main(String[] args) {
            in.useDelimiter("\r?\n");
            Student student = new Student();

            System.out.println("Press 1 to Record for Midterm");
            System.out.println("Press 2 to Record for Final Term");
            int record = in.nextInt();

            if (record == 1) {
                student.setTerm(TermType.MID);
                System.out.println("Enter 8 subjects and their corresponding grades:");
                System.out.println("Enter Subject and grades space separated.  Example -  \nMaths 79");
                System.out.println();


                for (int i = 0; i < 8; i++) {
                    System.out.println("Enter Subject " + (i + 1) + " details");
                    String subjectAndGrade = in.next();

                    int index = subjectAndGrade.lastIndexOf(" ");
                    String subject = subjectAndGrade.substring(0, index);
                    int grade = Integer.parseInt(subjectAndGrade.substring(index + 1));
                    student.getSubjects().add(new Subject(grade, subject));
                }
                System.out.println("Enter Grade Successful");

                System.out.println("========================================================");
                System.out.println("Details: ");

                System.out.println("Term Type " + student.getTerm());
                for(int i = 0; i< student.getSubjects().size(); i++) {
                    System.out.println("Subject: " + student.getSubjects().get(i).getSubjectName() + ", Grade: " + student.getSubjects().get(i).getGradeScore());
                }
            }
        }
    }

    class Student {
        private List<Subject> subjects = new ArrayList<>();
        private TermType term;

        public List<Subject> getSubjects() {
            return subjects;
        }

        public void setSubjects(List<Subject> subjects) {
            this.subjects = subjects;
        }

        public TermType getTerm() {
            return term;
        }

        public void setTerm(TermType term) {
            this.term = term;
        }
    }

    class Subject {
        private int gradeScore;
        private String subjectName;

        public Subject(int gradeScore, String subjectName) {
            this.gradeScore = gradeScore;
            this.subjectName = subjectName;
        }

        public double getGradeScore() {
            return gradeScore;
        }

        public void setGradeScore(int gradeScore) {
            this.gradeScore = gradeScore;
        }

        public String getSubjectName() {
            return subjectName;
        }

        public void setSubjectName(String subjectName) {
            this.subjectName = subjectName;
        }
    }

扫描仪通过将输入分成 'tokens' 和 'delimiters' 序列来工作。开箱即用,'one or more whitespace characters' 是定界符。

将成绩集合分解为一个新函数,并将要将成绩收集到的数组传递给它。

    public static void main(String[] args) throws IOException {

        int gradeType = 0;

        // Use a single scanner for all input
        Scanner aScanner = new Scanner(System.in);

        System.out.println("Press 1 to Record for Midterm");
        System.out.println("Press 2 to Record for Final Term");
        gradeType = aScanner.nextInt();

        String[] subjects = { "Subject A", "Subject B" };
        int[] midtermGrades = new int[subjects.length];
        int[] finalGrades = new int[subjects.length];

        int[] gradesToCollect;

        // Use gradesToCollect to reference the array you want to
        // collect into.
        //
        // Alternatively, we could call collectGrades() in both the if/else
        // condition
        if (gradeType == 1) {
            gradesToCollect = midtermGrades;
        } else {
            gradesToCollect = finalGrades;
        }

        collectGrades(subjects, gradesToCollect, aScanner);

        System.out.println("\n\nThese are the collected grades");
        System.out.println("Mid  Final");
        for (int i = 0; i < subjects.length; i++) {
            System.out.format("%3d  %3d\n", midtermGrades[i], finalGrades[i]);
        }
    }

    // Collect a grade for each subject into the given grades array.
    public static void collectGrades(final String[] subjects, final int[] grades, Scanner scn) {

        System.out.format("Enter %s subjects and their corresponding grades:",
                subjects.length);
        System.out.println();

        for (int i = 0; i < subjects.length; i++) {
            System.out.format("Enter Grade for %s : ", subjects[i]);
            grades[i] = scn.nextInt();
            if (i == (subjects.length))
                System.out.println();
        }
        System.out.println("Enter Grade Successful");
    }