請爲這個聰明或stupid..i試圖在一個double類型的方法返回一個字符串..我試圖解析String intto Double,可以嗎?
public double withdrawCash()
{
if (amounts == 20)
{
totalWithdrawal = BALANCE - 20;
return totalWithdrawal;
}else if (amounts == 40){
totalWithdrawal = BALANCE - 40;
return totalWithdrawal;
}else if (amounts == 60){
totalWithdrawal = BALANCE - 60;
return totalWithdrawal;
}else if (amounts == 100){
totalWithdrawal = BALANCE - 100;
return totalWithdrawal;
}else if (amounts == 200){
totalWithdrawal = BALANCE - 200;
return totalWithdrawal;
}
else{ //this where i tried to be smart or stupid...just learning..help!!!
String complainWithdrawal = "Please, you need to select appropriate amount";
double showWithdrawal = Double.parseDouble(complainWithdrawal);
return showWithdrawal;
}
}
請諮詢我的理想解決方案
您不能從表示返回double的方法返回字符串。這將使編程成爲一場噩夢。你可以拋出異常。那說'金額'從哪裏來? –
您不能將'String'作爲'double'返回。在我看來,你需要拋出一個異常,以防''量'不是你的期望值之一。 – Mureinik
是的,你是對的。感謝您的建議。 – Progzilla