如何使用for循环遍历数组中的每个元素。

huangapple 未分类评论45阅读模式
英文:

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(&quot;Enter the total cost&quot;);
        int cost = console.nextInt();
        
        System.out.println(&quot;Enter the total amount paid&quot;);
        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?

huangapple
  • 本文由 发表于 2020年4月6日 10:32:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61052150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定