if(!"D".equalsIgnoreCase(answer) || !"W".equalsIgnoreCase(answer)){
System.out.println("The answer is invalid!Please input again!");
answer = input.nextLine();
}else if(answer.equalsIgnoreCase("D")){
System.out.println("Enter deposite ammount: ");
depositAmmount = input.nextDouble();
System.out.printf("adding %.2f to account1\n\n", depositAmmount);
acc1.Credit(depositAmmount);
//display balance
System.out.format("ballance of account1 is: %.2f\n", acc1.getBallance());
System.out.format("ballance of account2 is: %.2f\n\n", acc2.getBallance());
}
else if(answer.equalsIgnoreCase("W")){
System.out.println("Enter withdraw ammount: ");
withdrawAmmount = input.nextDouble();
System.out.printf("differing %.2f to account1\n\n", withdrawAmmount);
acc1.Withdraw(withdrawAmmount);
//display balance
System.out.format("ballance of account1 is: %.2f\n", acc1.getBallance());
System.out.format("ballance of account2 is: %.2f\n", acc2.getBallance());
}
我正在嘗試使用存款和繪製一個簡單的應用程序。但它有一個錯誤,當我用!equals
確保用戶必須輸入D
或W
和其他鍵將再次輸入,直到輸入正確的鍵,但在這裏它不工作和輸入D
或W
它不能識別通知輸入無效。比較字符串使用if(!「D」.equalsIgnoreCase(answer))不起作用
我需要一些幫助。
首先注意到錯誤,但是您可能希望在答案中使用格式(即'||'和'&&') –