java - 一个关于程序编译的问题

标签 java compiler-construction compiler-errors

下面的代码是我的程序的一部分,它应该为用户提供按 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  ++  ;
          }

最佳答案

变量keyboard尚未定义。

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

user_selection  =  keyboard.nextLine() ;

定义之前

Scanner  keyboard  =  new Scanner( System.in ) ;

关于java - 一个关于程序编译的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402407/

相关文章:

c++ - 将第三方 C++ 编译器与 Microsoft Visual Studio 集成

c++ - 无法将模板类通过管道传递给 cout

Android:找出 Eclipse 中带有红色感叹号的错误

java - WS-Federation 主动请求者配置文件 SOAP 请求

java - 如何使用maven将webapp打包成jar

Java - 更改行为 Tab 键

java - Hibernate:使用条件按列表过滤

compiler-construction - 在 x86 中编码 - 编译器代码生成

compiler-construction - 使用哪个 D 编译器?

java - 我在编写测试驱动程序时遇到了麻烦