【发布时间】:2021-08-27 19:56:24
【问题描述】:
所以我正在为一个学校项目开发这个程序,我必须创建一个 ATM。出于某种原因,在下面的程序中输入 W 不会做任何事情,但 R 按预期工作。我尝试使用 Else If 并将“R”代码放在“W”代码之上,但似乎没有任何效果。抱歉,代码太长了,谢谢。
import java.util.ArrayList;
public class ConsutingProblem {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int totalCash;
String choice;
String rrr = "R";
String www = "W";
String iii = "I";
String qqq = "Q";
boolean stopAtm = false;
int hundred = 10;
int fifty = 10;
int twenty = 10;
int ten = 10;
int five = 10;
int one = 10;
totalCash = (hundred*100) + (fifty*50) + (twenty*20) + (ten*10) + (five*5) + one;
do {
System.out.println("Please enter the letter corresponding to your chosen action.");
System.out.println("R for a restock.");
System.out.println("W for withdrawl followed by amount. Example: W $145");
System.out.println("I for number of bills present with that denomination. Example: I $20");
System.out.println("Q for quitting this aplication.");
choice = input.nextLine();
if ((choice.substring(0, 0)).equals(www)) {
int withdrawlAmount = Integer.parseInt(choice.substring(3));
totalCash -= withdrawlAmount;
int displayWithdrawl = withdrawlAmount;
int hundredBack = (int)(displayWithdrawl/100);
displayWithdrawl -= (hundredBack*100);
int fiftyBack = (int)(displayWithdrawl/50);
displayWithdrawl -= (fiftyBack*50);
int twentyBack = (int)(displayWithdrawl/20);
displayWithdrawl -= (twentyBack*20);
int tenBack = (int)(displayWithdrawl/10);
displayWithdrawl -= (tenBack*10);
int fiveBack = (int)(displayWithdrawl/5);
displayWithdrawl -= (fiveBack*5);
int oneBack = (int)(displayWithdrawl/1);
displayWithdrawl -= (oneBack*1);
if (withdrawlAmount <= totalCash && hundredBack <= hundred && fiftyBack <= fifty && twentyBack <= twenty && tenBack <= ten && fiveBack <= five && oneBack <= one) {
System.out.println("Success: Dispensed $" + withdrawlAmount);
hundred -= hundredBack;
fifty -= fiftyBack;
twenty -= twentyBack;
ten -= tenBack;
five -= fiveBack;
one -= oneBack;
System.out.println("Machine Balance:");
System.out.println("$100 - " + hundred);
System.out.println("$50 - " + fifty);
System.out.println("$20 - " + twenty);
System.out.println("$10 - " + ten);
System.out.println("$5 - " + five);
System.out.println("$1 - " + one);
} else {
System.out.println("Faliure: insufficient funds");
}
}
if (choice.equals(rrr)) {
hundred = 10;
fifty = 10;
twenty = 10;
ten = 10;
five = 10;
one = 10;
totalCash = (hundred*100) + (fifty*50) + (twenty*20) + (ten*10) + (five*5) + one;
System.out.println("Machine Balance:");
System.out.println("$100 - " + hundred);
System.out.println("$50 - " + fifty);
System.out.println("$20 - " + twenty);
System.out.println("$10 - " + ten);
System.out.println("$5 - " + five);
System.out.println("$1 - " + one);
}
} while (stopAtm == false);
}
}
【问题讨论】:
标签: java string if-statement