扫描程序与Switch语句的交互

时间:2018-02-10 18:45:55

标签: java

目前正在为Java编写一个类的交互式程序;它是一个菜单,通过switch语句有5种不同的选择(0-4)。启动时,扫描仪会接受任何选择,并执行五种不同方法中的一种。

case 2:
    System.out.println("Choose where you want to put the student");
    int id2 = scan.nextInt();
    AddStudent(id2);
    ViewStudents("all");
    break;

调用AddStudent:

private void AddStudent(int id) {
    System.out.println("Type in name, subject and mark: ");

    String name = scan.nextLine();
    String subject = scan.nextLine();
    int mark = scan.nextInt();

    /* check via loop studentNames array and if it's null at
     [i], set studentNames[i]  to name,
     studentSubjects[i] to subject and studentMarks[i] to mark. 
    */
    for (....)  {
       if (studentNames[i] == null) {
          studentNames[i] = name;
    }
}

但是当我运行这个方法时,我无法输入三个扫描实例。我得到一个InputMismatchException。我该如何解决这个问题?

0 个答案:

没有答案