在我的代码中,给定输入 475 我希望输出为: 申请金额 = 475 美元 ATM 中还剩 1 100 百 = 4,剩余金额 = 75 美元 ATM 还剩 4 个 50 五十岁 = 1,剩余金额 = $25
而是: 申请金额 = 475 美元 ATM 中还剩 0 100 百 = 0,剩余金额 = 75 美元 ATM 中还剩 0 50 秒 五十岁 = 0,剩余金额 = $25
为什么会出现这个问题?我的 calcNumBills 方法有问题吗?
import javax.swing.JOptionPane;
/* 1-26-2023
ISYS-216-001
This is the code for a special ATM that tells you how much money you are getting and which bills
The inputs will be the amount that the end-user would like in cash, with the outputs being which bills they will receive this amount in.
The exposed interfaces will be the input amount window and the print of what they will receive in monetary value.
Variables will be input, amount, fifties, twenties, tens, fives, and remainingamount. */
class Main {
//Method for calculating the number of bills needed
static int calcNumBills(int remainingAmount, int denomination) {
// Divide remaining amount by denomination using integer division
return remainingAmount / denomination;
}
//Method for calculating the remainder
static int calcRemainder(int remainingAmount, int denomination) {
//Divides remainingAmount by denomination and returns a remainder
return remainingAmount % denomination;
}
//Method for printing the values as described in step 4
static void printValues(int numBillsAvailable, int remainingAmount, int denomination) {
/* Begin switch statement for denomination
Each case is a check for each type of bill offered by the atm
break is used to end that check for each type of bill offered
default is the code executed if conditions are not met */
switch (denomination) {
case 100:
System.out.println("Hundreds = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
case 50:
System.out.println("Fifties = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
case 20:
System.out.println("Twenties = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
case 10:
System.out.println("Tens = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
case 5:
System.out.println("Fives = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
case 2:
System.out.println("Twos = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
case 1:
System.out.println("Ones = " + calcNumBills(remainingAmount, denomination) + ", Remaining Amount = $" + calcRemainder(remainingAmount, denomination));
break;
default:
System.out.println("Invalid denomination");
}
}
/*Method for checking to match the amount of bills available
Checks if the number of bills available is less than the number needed
If there are enough bills available, it returns how many remain */
static boolean checkAvailableBills(int remainingAmount, int denomination, int numBillsAvailable) {
int numBillsNeeded = calcNumBills(remainingAmount, denomination);
if (numBillsNeeded > numBillsAvailable) {
System.out.println("Error: insufficient bills available for denomination " + denomination);
return false;
} else {
numBillsAvailable =- numBillsNeeded;
System.out.println("There are " + numBillsAvailable + " " + denomination + "s remaining in the ATM");
return true;
}
}
public static void main(String[] args) {
String input = JOptionPane.showInputDialog ("Enter the amount:");
// Declares input and displays the input dialog with the text "Enter the Amount"
int amount = Integer.parseInt (input);
// Declares the amount variable as a conversion of the input string to an integer
System.out.println ("Requested Amount = $"+ amount);
//Declare amount of each bill available in the ATM
int numHundredsAvailable = 5;
int numFiftiesAvailable = 5;
int numTwentiesAvailable = 5;
int numTensAvailable = 5;
int numFivesAvailable = 5;
int numTwosAvailable = 5;
int numOnesAvailable = 5;
//Sets the value of remaining amount to amount
int remainingAmount = amount;
int numHundreds = calcNumBills(remainingAmount, 100);
remainingAmount = calcRemainder(remainingAmount, 100);
if (!checkAvailableBills(remainingAmount, 100, numHundredsAvailable)) {
return;
}
numHundredsAvailable -= numHundreds;
printValues(numHundredsAvailable, remainingAmount, 100);
int numFifties = calcNumBills(remainingAmount, 50);
remainingAmount = calcRemainder(remainingAmount, 50);
if (!checkAvailableBills(remainingAmount, 50, numFiftiesAvailable)) {
return;
}
numFiftiesAvailable -= numFifties;
printValues(numFiftiesAvailable, remainingAmount, 50);
}
}
我一直在尝试调整公式。 我试过输入也返回零的打印语句。 在这一点上我迷路了。