0
我目前正在開發一個程序,它使用BufferedReader
來讀取輸入,而System.out.println()
用於輸出。System.out.println()和BufferedReader在控制檯中混合輸出
這裏是我的代碼:
public void chooseMethod() throws IOException{
int in = 0;
while(true){
System.out.println("What do you want to do? (0 to exit, 1 to read Bank Account, 2 to write Bank Account, 3 to read Bill, 4 to write Bill): ");
in = Integer.parseInt(cin.readLine());
if(in == 0){
break;
}else if((in < 0) || (in > 4)){
System.out.println("Invalid choice.");
}else if(in == 1){
showBankAccount();
}else if(in == 2){
insertBankAccount();
}else if(in == 3){
showBill();
}else if(in == 4){
insertBill();
}
}
dbm.close();
}
public void insertBankAccount() throws IOException{
int bankNr = 0;
int sortCode = 0;
int accountNumber = 0;
int balance = 0;
int interest = 0;
String details;
String name;
while(true){
System.out.println("Enter bankNr (0 to exit): ");
bankNr = Integer.parseInt(cin.readLine());
if(bankNr == 0){
break;
}else if(bankNr <= maxBankNr){
System.out.println("Invalid Number: already taken");
break;
}
System.out.println("Enter Account Name: ");
name = cin.readLine();
System.out.println("Enter Sort Code: ");
sortCode = Integer.parseInt(cin.readLine());
System.out.println("Enter Account Number: ");
accountNumber = Integer.parseInt(cin.readLine());
System.out.println("Enter balance: ");
balance = Integer.parseInt(cin.readLine());
System.out.println("Enter Interest: ");
interest = Integer.parseInt(cin.readLine());
System.out.println("Enter Details: ");
details = cin.readLine();
bankAccountDAO.insertBankAccount(bankNr, sortCode, accountNumber, balance, interest, details, name);
this.maxBankNr = bankAccountDAO.getMaxBankNr();
}
}
現在,說我輸入 「2」 到 「寫的銀行賬戶,」 輸出如下:
運行:
What do you want to do? (0 to exit, 1 to read Bank Account, 2 to write Bank Account, 3 to read Bill, 4 to write Bill):
Enter bankNr (0 to exit):
2
輸出中的這個「2」應該在「輸入bankNr(0退出)之前:」
我已經有過幾次這個問題,兩個w ith Scanner
和BufferedReader
,並花了很長時間google搜索,但似乎無法找到任何答案。
有沒有其他人有這個問題?如果是這樣,你是如何解決它的?
謝謝:)這個工作。我相信這會在未來節省我的時間:) – kyul 2013-05-02 12:32:45
在寫入提示之前接受輸入沒有任何意義。 – EJP 2013-05-02 12:39:17
我不認爲它也沒有任何意義,但它的工作:S 我不知道爲什麼它會倒退... – kyul 2013-05-31 09:58:16