0
public class ElectronicCounter {
public ElectronicCounter(User user)throws IOException{
Scanner scanner = new Scanner(System.in);
boolean done = false;
loginName = user.getName();
System.out.println("--------------------------------------------------------");
System.out.println("Welcome to the Electronic-Sales Counter! ");
while(!done){
try{
System.out.print("Please enter '1' to record sales or '2' to exit:");
input = scanner.nextLine();
System.out.println("bug");
if(input=="1"){
System.out.println("Please enter a list of purchasing-product ID and number");
done = true;
}
else if(input=="2"){
System.out.println("<LOG> User " +loginName+ " has successfully logged off! ");
done = true;
}
else{
System.out.println("<LOG> Invalid command");
}
}
catch (NoSuchElementException e){
System.out.println("<LOG> No Such Element");
}
catch (IllegalStateException e){
System.out.println("<LOG> IllegalState");
}
}
scanner.close();
}
static String loginName;
static String input;
}
當計算scanner.nextLine()
時,掃描器不停止搜索令牌。我想問問怎樣才能停止掃描器等待輸入??謝謝scanner.next()不停止
,最好使用'「2」 .equals(輸入)',因爲在這種情況下,你不能得到NPE – nikis
@nikis我敢肯定,'nextLine()'沒有返回null它會拋出一個'NoSuchElementException'或'IllegalStateException'。 –
是的,但我說的是一般情況 – nikis