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