【发布时间】: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