在switch java中的case之间传递用户输入

时间:2017-11-27 03:25:38

标签: java

我的代码有一个小问题。我在案例1 中添加了讲师,并且我试图在案例3 中为这位讲师添加书籍。问题是,似乎案例3 无法识别创建的讲师。

有没有办法传递这个值。

解决方案可能非常简单但是在这个时候我根本无法得到它......

public class Menu {
static Scanner in = new Scanner (System.in);
LectureList lec = new LectureList(100);

BookList bl = new BookList(0);
public Menu(){

}

public int mainMenu(){
    int option = 0;

    System.out.println("---------------------------------------------------------");
    System.out.println("                    Lecturer Menue                       ");
    System.out.println("*********************************************************");
    System.out.println("1) Add Lecturer");
    System.out.println("2) Find Lecturer by ID");
    System.out.println("3) Add book to Lecturer BookList");
    System.out.println("4) Remove book from Lecturer BookList ");
    System.out.println("5) Search for a book using the ISBN number");
    System.out.println("6) Calculate the yearly book payment");
    System.out.println("7) Output all of the book details in the system to a file");
    System.out.println("8) Exit");
    boolean selected = false;

    while (selected == false)
    {


        try 
        {
            option = in.nextInt();
            in.nextLine();
            if 
                ((option == 8)){

                System.out.println("Goodbye!");
                System.exit(0);}


            else if 
            ((option <= 0) || (option > 8))
                System.out.println("Sorry but you have to choose an option between 1 and 8");
            else
                selected = true;



        }
        catch (InputMismatchException e) 
        {
            System.out.println("Sorry you did not enter a valid option");

            in.next();
        }       

    }

    return option;

}
public void menuSwitch(){
    boolean finish = false;
    if (finish == false){
        int option = mainMenu();

        switch (option){

        case 1: 
            String LecName = " ";

            System.out.println("Please enter Lecturer's  name");
            LecName = in.nextLine();
            Lecturer l = new Lecturer(LecName);


            lec.add(l);

            break;
        case 2:

            break;
        case 3:

            String name = "";
            Double price = 00.00 ;
            String isbn ="";
            String author = "";

            System.out.println("Please enter Book title ");
            name = in.nextLine();
            System.out.println("Please enter Book's price ");
            price = in.nextDouble();
            System.out.println("Please enter Book's isbn number ");     
            isbn = in.next();       
            System.out.println("Please enter book author's name");
            author = in.next();
            Book b = new Book( name,  price,  isbn,  author);

            l.addBook(b);



            break;``
            default: 
            finish = true;
            break;
        }
        menuSwitch();
    }
}
}

1 个答案:

答案 0 :(得分:0)

case中的每个switch语句都以break;结束,而default案例也是如此。在这种情况下,对于switch语句的任何传递,只会执行一个case标签。

如果第一遍执行第一个case,第二遍执行第二个case,第一个案例中定义的变量将不再存在 - 它们将超出范围当switch语句第一次完成时。

您是否可以从Lecturer收藏中获得对LectureList的引用?

case 1:中,您可以创建它并将其放入集合中;在case 3:中,您可以从集合中获取并更新它。