Java While(true)-循环未中断,扫描仪
我对一些代码有问题。当我试图用“退出”来打破我的循环时,它不会停止。如果我开始键入quit,它会按预期中断,但第二次循环运行时,我键入quit,它不会中断。有什么问题Java While(true)-循环未中断,扫描仪,java,Java,我对一些代码有问题。当我试图用“退出”来打破我的循环时,它不会停止。如果我开始键入quit,它会按预期中断,但第二次循环运行时,我键入quit,它不会中断。有什么问题 public static void interactionLoop() { input = new Scanner(System.in); String ssn = null; String message = null; int accountNr; double amount;
public static void interactionLoop() {
input = new Scanner(System.in);
String ssn = null;
String message = null;
int accountNr;
double amount;
while(true) {
for(Customers aCustomer : Customers.getCustomerList()) {
System.out.println(aCustomer.getName() + ", " + aCustomer.getSsn());
}
System.out.println("Choose a customer by using SSN.");
System.out.print(">> ");
ssn = input.nextLine();
if(ssn.equals("quit")) {
break;
}
Customers theChosenCustomer = Customers.getCustomerBasedOnSSN(ssn);
ArrayList<Accounts> accList = theChosenCustomer.getAccountList();
for(Accounts anAccount : accList) {
if(anAccount instanceof Savings) {
System.out.print("(Savings, " + anAccount.getAccountNr() + ")" + "\n");
}
if(anAccount instanceof Loans) {
System.out.print("(Loans, " + anAccount.getAccountNr() + ")" + "\n");
}
}
System.out.print("Enter the account that you want to work with using the account number:\n>> ");
accountNr = input.nextInt();
Accounts chosenAccount = theChosenCustomer.getSpecificAccount(accountNr);
System.out.println("Account balance: "+chosenAccount.getBalance());
for(Transaction t : chosenAccount.getTransaction()) {
System.out.println(t.getDateAndTime().getTime() +", " + t.getComment() +": " + t.getAmount());
}
System.out.println("\n");
System.out.print("Please enter the amount of money you wish you withdraw or deposit: ");
while(input.hasNext()) {
amount = input.nextDouble();
input.nextLine();
if(chosenAccount.isValid(amount)){
System.out.print("Please enter a comment: ");
message = input.nextLine();
Calendar transdatetime = Calendar.getInstance();
chosenAccount.makeTransaction(new Transaction(transdatetime,message,amount));
System.out.println("");
interactionLoop();
}
}
}
publicstaticvoidinteractionloop(){
输入=新扫描仪(System.in);
字符串ssn=null;
字符串消息=null;
国际会计;
双倍金额;
while(true){
对于(客户aCustomer:Customers.getCustomerList()){
System.out.println(aCustomer.getName()+”,“+aCustomer.getSsn());
}
System.out.println(“使用SSN选择客户”);
系统输出打印(“>>”);
ssn=input.nextLine();
如果(ssn.等于(“退出”)){
打破
}
Customers theChosenCustomer=Customers.getCustomerBasedOnSSN(ssn);
ArrayList accList=客户。getAccountList();
对于(帐户:Account:accList){
if(储蓄账户实例){
System.out.print(“(Savings,+anaAccount.getAccountNr()+”)”+“\n”);
}
if(贷款账户实例){
System.out.print(“(贷款,+anaAccount.getAccountNr()+”)”+“\n”);
}
}
System.out.print(“使用账号输入您想要使用的账号:\n>>”;
accountNr=input.nextInt();
Accounts chosenAccount=客户。获取特定账户(账户编号);
System.out.println(“账户余额:+chosenAccount.getBalance());
对于(事务t:chosenAccount.getTransaction()){
System.out.println(t.getDateAndTime().getTime()+,“+t.getComment()+”:“+t.getAmount());
}
System.out.println(“\n”);
System.out.print(“请输入您希望提取或存款的金额:”);
while(input.hasNext()){
amount=input.nextDouble();
input.nextLine();
如果(chosenAccount.isValid(金额)){
System.out.print(“请输入注释:”);
message=input.nextLine();
Calendar transdatetime=Calendar.getInstance();
makeTransaction(新事务(transdatetime、消息、金额));
System.out.println(“”);
交互循环();
}
}
}
从第二次开始,Scanner
从Std-InpuStream扫描Integer
,但仍保留由
ssn = input.nextLine();
由于此原因,您的程序不会退出。double
也是如此。更好地使用它
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
然后使用
reader.readLine()
并将其解析为所需的数据类型。例如Integer.parseInt(reader.readLine())
谢谢Aniket。我尝试了您的解决方案,但问题仍然存在。错误与以前完全相同。有什么建议吗?请将input.nextLine();
放在accountNr=input.nextLine()之后
也是。所以我根本不应该使用bufferedReader?我现在很困惑。我在你更新的代码中没有看到任何bufferedReader。
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));