Java 关于程序编译的一个问题

Java 关于程序编译的一个问题,java,compiler-construction,compiler-errors,Java,Compiler Construction,Compiler Errors,下面的代码是我的程序的一部分,它假定为用户提供按p继续和按e退出的选项。不管怎么说,我一直收到这个错误,我不知道该怎么做来修复它,这里是错误(我试着将这条线向四周移动,但没有效果,如果我删除它,程序会编译,但它会进入一个无休止的循环): 代码如下: String user_selection = "????" ; System.out.print("\n This program prints inventory. Please, select from"

下面的代码是我的程序的一部分,它假定为用户提供按p继续和按e退出的选项。不管怎么说,我一直收到这个错误,我不知道该怎么做来修复它,这里是错误(我试着将这条线向四周移动,但没有效果,如果我删除它,程序会编译,但它会进入一个无休止的循环):

代码如下:

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

尚未定义变量
键盘

也就是说,你正在使用它

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

Scanner  keyboard  =  new Scanner( System.in ) ;

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

Scanner  keyboard  =  new Scanner( System.in ) ;

您从未定义过
键盘
。看起来你在用它做扫描器。在这种情况下,它很可能被定义为

Scanner keyboard=new Scanner(System.in);

在尝试使用
键盘之前,哪个必须您是否在任何地方声明了变量键盘?我在最上面有两条语句:import java.util.Scanner;导入java.util.*;这些只是import语句,告诉编译器在哪里可以找到东西,而不是实际创建变量
Scanner keyboard=new Scanner(System.in);