Java中的参考返回变量

时间:2019-02-01 18:04:53

标签: java

我正试图提出一个程序,该程序将采用双值,例如47.63,并将其分解为各自的钞票/硬币(4个10s,1个5个,2个等)。

我制作了一个名为counter的函数,该函数以两倍的金额(例如47.63),一种货币和该货币的名称进行输入,并返回一个名为remainder的双精度数。目前,我正在考虑使用返回值remainder并将其再次放入同一函数,以便可以在主函数中调用类似的内容。

counter(total_money, tens, "ten dollar bills");
counter(remainder, fives, "five dollar bills");

然后如何引用返回值remainder以备将来使用?

import java.util.Scanner;

public class MoneyCalculator {

    public static double counter(double total_money, double currency, String currency_name) {
        int i;
        // for loop to subtract 10's out
        for (i = 0; total_money - currency > 0; i++) {
            total_money = total_money - currency;
        }

        double remainder = total_money;
        System.out.printf("%d " + currency_name + "\n", i);
        System.out.printf("%f remaining\n", remainder);
        return remainder;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a double\n");
        double total_money = scan.nextDouble();  //47.63

        double tens = 10.00,
                fives = 5.00,
                ones = 1.00,
                quarters = 0.25,
                dimes = 0.10,
                nickles = 0.05,
                pennies = 0.01;

        int tenbills, fivebills, onebills, quartercoins, dimecoins, nicklecoins, pennycoins;

        counter(total_money, tens, "ten dollar bills");
        // counter(remainder, fives, "five dollar bills");
    }
}

从下面得到答案,以下解决方案有效,但看起来很杂乱。有没有更优雅的方法可以做到这一点?

double remainder = counter(total_money, tens, "ten dollar bill(s)");
double remainder_1 = counter(remainder, fives, "five dollar bill(s)");
double remainder_2 = counter(remainder_1, ones, "one dollar bill(s)");
double remainder_3 = counter(remainder_2, quarters, "quarter(s)");
double remainder_4 = counter(remainder_3, dimes, "dime(s)");
double remainder_5 = counter(remainder_4, nickles, "nickle(s)");
counter(remainder_5, pennies, "penny(s)");

3 个答案:

答案 0 :(得分:2)

与其像这样counter(total_money, tens, "ten dollar bills");来称呼它 在一个变量中存储返回值,如下所示:double remainder = counter(total_money, tens, "ten dollar bills");

答案 1 :(得分:0)

首先在列表中定义面额。您可以为这些使用枚举。

public enum Denomination {

  TENS(10.0, "ten dollar bill(s)"), 
  FIVES(5.0, "five dollar bill(s)"), 
  //... etc.

  private final double amount;
  private final String description;

  Denomination(double amount, String description) {
    this.amount = amount;
    this.description = description;
  }

  double getAmount() {
    return this.amount;
  }

  double getDescription() {
    return this.description;
  }
}

然后按以下正确顺序创建它们的列表:

private static final List<Denominations> denominations = 
    Collections.unmodifiableList(Arrays.asList(TENS, FIVES, ONES, QUARTERS, DIMES , NICKELS, PENNIES));

我们将其设为不可修改,以免您误将其弄乱。将其放在顶部的某个位置。

然后将您的counter()函数更改为递归:

public static void counter(double money, Queue<Denominations> remainingDenominations) {
  if (remainingDenominations.isEmpty()) {
    return;
  }

  //get the next denomination, and remove it from the list
  Denomination denomination = remainingDenominations.remove();

  //calculate how many times the denomination amount fits in the money (rounded down to the nearest whole `int`)
  int times = money / denomination.getAmount();

  //calculate the remainder 
  double remainder = money - (denomination.getAmount() * times);

  System.out.printf("%d %s", times, denomination.getDescription());
  System.out.printf("%f remaining\n", remainder);

  counter(remainder, remainingDenominations);
}

然后使用它,只需将面额列表复制到LinkedList中(这样原始面额将保持不变),然后调用counter()一次。

counter(total_money, new LinkedList<>(denominations));

您甚至可以使它以Map的形式返回面额,而不仅仅是输出并丢失它:

 private static Map<Denomination, Integer> counter(double money, Queue<Denomination> remainingDenominations, Map<Denomination, Integer> accumulated) {

  if (remainingDenominations.isEmpty()) {
    return accumulated;
  }

  //... same logic as before ...

  accumulated.put(denomination, times);
  counter(remainder, remainingDenominations, denomination);
}

/**
  * A simpler overloaded version for the initial entry point.
  */
public static Map<Denomination, Integer> counter(double money) {
  return counter(money,  new LinkedList<>(denominations), new LinkedHashMap<>());
}

这种使用方式变得更加简单:

Map<Denomination, Integer> counts = counter(total_money);

counts.forEach((denomination, amount) -> System.out.println("%d %s", amount, denomination.getDescription());

您可以根据需要执行任何操作。

答案 2 :(得分:-1)

将值分配给新的double变量,然后您可以返回该变量。