我试图弄清楚为什么while循环不起作用所以我添加了depIn1
和value
的打印语句。为什么跳过depValue1 = Double.parseDouble(depIn1)
行?如果是这样,由于检查值不是真的,while循环是否仍然有效?
String depIn1 = "";
double originBalance1 = 0.00;
double newBalance1 = 0.00;
double depValue1 = 0.00;
String newNewBalance1 = "";
String originStringBalance1 = "";
boolean value;
BufferedReader depositInput1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Amount to withdraw: ");
depIn1 = depositInput1.readLine();
value = isInt(depIn1);
System.out.println("depIn1: " + depIn1);
System.out.println("Value: " + value);
depValue1 = Double.parseDouble(depIn1);
System.out.println("depValue1: " + depValue1);
double doubleBalanceValue = Double.parseDouble(users.get(i).getBalance());
while((depValue1 > bankBalance) || (depValue1 <= 0) || (depValue1 > doubleBalanceValue) || (value != true)){
System.out.print("No! Invalid input! Try again! Amount to withdraw: ");
depIn1 = depositInput1.readLine();
depValue1 = Double.parseDouble(depIn1);
}
System.out.println("Thanks!");
当前输出:
Amount to withdraw: asfd
depIn1: asfd
Value: false
答案 0 :(得分:0)
您可以尝试:
public class test {
public static void main(String[] args) throws IOException {
String depIn1 = "";
double originBalance1=0.00;
double newBalance1=0.00;
double depValue1=0.00;
String newNewBalance1 = "";
String originStringBalance1 = "";
boolean value=false;
double bankBalance = 5000.00;
double doubleBalanceValue =5000.00;
BufferedReader depositInput1 = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.print("Amount to withdraw: ");
depIn1 = depositInput1.readLine();
System.out.println("depIn1: " + depIn1);
System.out.println("Value: " + value);
try{
depValue1 = Double.parseDouble(depIn1);
value=true;
}
catch(NumberFormatException e){
value=false;
}
System.out.println("depValue1: " + depValue1);
if(!value){
System.out.print("No! Invalid input! Try again!");
}
else{
System.out.println("Thanks!");
}
}
while((depValue1 > bankBalance) || (depValue1 <= 0) || (depValue1 > doubleBalanceValue) || (value != true));
}
}