英文:
How do I run a line through an array using a for loop
问题
import java.util.Scanner;
public class ChangeMaker {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Enter the total cost");
int cost = console.nextInt();
System.out.println("Enter the total amount paid");
int paid = console.nextInt();
makeChange(paid, cost);
}
public static void makeChange(int paid, int cost) {
int[] billDenomination = { 2000, 1000, 500, 100 };
int[] coinDenomination = { 25, 10, 5, 1 };
int remainder = (paid - cost);
for (int i = 0; i < billDenomination.length; i++) {
int numBills = remainder / billDenomination[i];
remainder = remainder % billDenomination[i];
System.out.println("Number of " + billDenomination[i] + " bills: " + numBills);
}
for (int i = 0; i < coinDenomination.length; i++) {
int numCoins = remainder / coinDenomination[i];
remainder = remainder % coinDenomination[i];
System.out.println("Number of " + coinDenomination[i] + " coins: " + numCoins);
}
}
}
英文:
import java.util.Scanner;
public class ChangeMaker {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Enter the total cost");
int cost = console.nextInt();
System.out.println("Enter the total amount paid");
int paid = console.nextInt();
}
public static void makeChange(int paid, int cost) {
int[] billDenomination = { 2000, 1000, 500, 100 };
int[] coinDenomination = { 25, 10, 5, 1 };
int remainder = (paid - cost);
for (int remainder = 0 // TODO
}
}
How do I run int remainder through the for loop so that it returns the values of the denominations?
专注分享java语言的经验与见解,让所有开发者获益!
评论