【问题标题】:an issue concerning program compiling关于程序编译的问题
【发布时间】:2013-12-05 14:19:41
【问题描述】:

下面的代码是我的程序的一部分,它假设让用户可以选择按 p 继续,按 e 退出。无论如何,我一直收到这个错误,我不知道该怎么做才能修复它是错误(我尝试将线移动,但它没有效果,如果我删除它,程序会编译但它会无休止循环):

C:\Users\Asus\Desktop\javaproject\products.java:112: error: cannot find symbol
           user_selection  =  keyboard.nextLine() ;
                              ^
  symbol:   variable keyboard
  location: class productsDataFinder
1 error

Process completed.

这里是代码:

String  user_selection  =  "????"  ;

      System.out.print("\n This program prints inventory. Please, select from"
                    +  "\n the following menu by typing in a letter. ") ;

      while ( user_selection.charAt( 0 )  !=  'e' )
      {
         System.out.print("\n\n   p   Print inventory."
                         +  "\n   e   Exit the program.\n\n   " ) ;

        user_selection  =  keyboard.nextLine() ;   //error concerning this line

     if ( user_selection.charAt( 0 )  ==  'p' )
     { 

       System.out.print("\n Please insert your serial number:  ");              

        Scanner  keyboard  =  new Scanner( System.in ) ;
        int  given_id  =  keyboard.nextInt() ;
        int  products_index  =  0 ;

            boolean table_search_ready  =  false ;

         while ( table_search_ready  ==  false )
         {

         if ( products_index >= products_table.length )
         {

            table_search_ready  =  true ;

                      System.out.print( "\n    Sorry, no such product id "
                           +  given_id  + ".\n" ) ;
         }

         else if ( products_table[ products_index ].get_id()  ==  given_id )
         {

            products_table[ products_index ].print_products_data() ;

            table_search_ready  =  true ;
         }



         else
         {
            products_index  ++  ;
          }

【问题讨论】:

  • 你是否在任何地方声明了可变键盘?
  • 我在最上面有这两个语句: import java.util.Scanner;导入 java.util.* ;
  • 那些只是导入语句,告诉编译器它可以在哪里找到东西而不是实际创建变量

标签: java compiler-construction compiler-errors


【解决方案1】:

变量keyboard尚未定义。

也就是说,你正在使用它……

user_selection  =  keyboard.nextLine() ;

在定义之前

Scanner  keyboard  =  new Scanner( System.in ) ;

【讨论】:

    【解决方案2】:

    您需要在使用前声明keyboard。您可以在第一个 while 循环之前移动此行:

    Scanner  keyboard  =  new Scanner( System.in ) ;
    

    【讨论】:

      【解决方案3】:

      您从未定义过keyboard。看起来您正在将其用作扫描仪。在这种情况下,它很可能被定义为

      Scanner keyboard=new Scanner(System.in);
      

      这必须是在您尝试使用keyboard之前

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-06-15
        • 2016-02-28
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        相关资源
        最近更新 更多