基于JAVA控制台的基本菜单问题

时间:2011-04-25 00:22:37

标签: java

我正在尝试创建一个基于java控制台的基本菜单系统。我设法使第一层工作正常:

public static int menu(){

    //Displays the main menu and handles passing off to next menu. 

      Scanner scanner = new Scanner (System.in);
      int selection=0;
      String CDid;
      int i=0;
      int j=0;

      while(i==0) {   //changed while(1) to a value that didn't complain in netbeans
          System.out.println("Please choose an option from the following:"); 
          System.out.println("[1] Search by CD ID"); 
          System.out.println("[2] View all CD's in store"); 
          System.out.println("[3] Quit"); 
          System.out.println("Choice: "); 
          selection=scanner.nextInt();     

     switch (selection){

         case 1:System.out.println("Please enter the CD ID:");
              i=1;
              break;       

         case 2:System.out.println("List all CD's");
              i=1;
              break;

         case 3:System.out.println("Quiting...");
              System.exit(5);

         default:System.out.println("Your choice was not valid!");

          };

      }  
      return selection;
  }

我现在正试图让它当你选择选项[1]时它会输入您输入的下一个字符串并运行showCD()方法。我已经尝试了之前使用的方法,但因为它是一个字符串,我遇到了一些错误。

  public static int menu(){
        //Displays the main menu and handles passing off to next menu. 

          Scanner scanner = new Scanner (System.in);
          int selection=0;
          String CDid;
          int i=0;
          int j=0;

          while(i==0) {   //changed while(1) to a value that didn't complain in netbeans
              System.out.println("Please choose an option from the following:"); 
              System.out.println("[1] Search by CD ID"); 
              System.out.println("[2] View all CD's in store"); 
              System.out.println("[3] Quit"); 
              System.out.println("Choice: "); 
              selection=scanner.nextInt();     

         switch (selection){

             case 1:System.out.println("Please enter the CD ID:");
                    CDid=scanner.toString(); 
                      while(j==0) { 
                         switch (CDid){
                         case 1:showCD(CDid);
                         j=1;
                         break;

                         default:System.out.println("Your choice was not valid!");

                         }
                      }

                  i=1;
                  break;       

             case 2:System.out.println("List all CD's");
                  i=1;
                  break;

             case 3:System.out.println("Quiting...");
                  System.exit(5);

             default:System.out.println("Your choice was not valid!");

              };

          }  
          return selection;
      }

2 个答案:

答案 0 :(得分:2)

听起来像家庭作业(你应该这样标记)。您正在尝试获取下一个用户输入字符串,但您的代码正在调用scanner.toString()。这只会打印出Scanner对象的String表示形式。您可能希望使用scanner.nextLine()代替。请查看this以供参考。

编辑:此外,只需仔细阅读您的代码,您就会尝试启用CDid,这是一个字符串。您无法在Java中打开String(但如果您<= 1.6)。你需要在这里使用if-else块。

答案 1 :(得分:0)

更改:CDid = scanner.toString();

to:CDid = scanner.next();

我不确定您是否可以使用String进行切换...如果您无法使用Integer.parseInt()方法将CDid解析为Integer