有关程序编译的问题

时间:2013-12-05 14:19:41

标签: java compiler-construction compiler-errors

波纹管代码是我程序的一部分,假设通过按下按钮给用户选择继续按p并退出。反正我一直得到这个单一的错误,我不知道该怎么做才能解决这里是错误(我尝试移动线arround但它没有任何影响,如果我删除它程序编译但它然后它是无休止的循环):

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  ++  ;
          }

3 个答案:

答案 0 :(得分:2)

尚未定义变量keyboard

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

user_selection  =  keyboard.nextLine() ;
在定义之前

Scanner  keyboard  =  new Scanner( System.in ) ;

答案 1 :(得分:0)

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

Scanner  keyboard  =  new Scanner( System.in ) ;

答案 2 :(得分:0)

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

Scanner keyboard=new Scanner(System.in);

必须之前才能尝试使用keyboard