Java中的引用返回变量
我正在尝试提出一个程序,它将采用双倍值,例如47.63,并将其分解为各自的纸币/硬币(4 10、1 5、2 1等) 我创建了一个名为Java中的引用返回变量,java,Java,我正在尝试提出一个程序,它将采用双倍值,例如47.63,并将其分解为各自的纸币/硬币(4 10、1 5、2 1等) 我创建了一个名为counter的函数,它接受一个输入,输入一个双倍金额(e.x.47.63)、一种货币,然后输入该货币的名称),并返回一个名为余数的双倍值。目前,我想使用返回值余数,并将其再次放入同一个函数中,以便在我的主函数中调用类似的内容 counter(total_money, tens, "ten dollar bills"); counter(remainder, fiv
counter
的函数,它接受一个输入,输入一个双倍金额(e.x.47.63)、一种货币,然后输入该货币的名称),并返回一个名为余数的双倍值。目前,我想使用返回值余数
,并将其再次放入同一个函数中,以便在我的主函数中调用类似的内容
counter(total_money, tens, "ten dollar bills");
counter(remainder, fives, "five dollar bills");
然后如何引用返回值余数
,以备将来使用
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)");
而不是像这样称呼它:counter(总金额,十美元,“十美元钞票”)代码>
将返回值存储在一个变量中,如下所示:double remains=counter(total_money,tens,“十美元钞票”)编码>将返回值赋给一个新的双变量,然后您可以返回该变量。首先在列表中定义您的面额。您可以为这些使用枚举
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(total_money, new LinkedList<>(denominations));
这样使用它就更简单了:
Map<Denomination, Integer> counts = counter(total_money);
counts.forEach((denomination, amount) -> System.out.println("%d %s", amount, denomination.getDescription());
Map counts=计数器(总金额);
counts.forEach((面额,金额)->System.out.println(“%d%s”,金额,面额.getDescription());
您可以对计数执行任何操作。您是否尝试使用余数
作为输入和输出?是的,只是不是第一次计数器
调用您使用的是primitivedouble
,java中的所有原语都是按值传递
。因此,只有值会传递到方法中,而不是re引用。即使您将使用wrapperDouble
类,这也是不可更改的。就好像您将更改值一样,它将创建一个ne对象。实现这一点的最佳方法是使用这些字段创建一个POJO类并使用它。谢谢。代码可以工作,但必须存储7个不同的余数_1,余数_2
有很多方法可以修改代码,其中之一是使其成为一个递归函数,在找到所有余数之前调用自己。Arrays.stream(demination.values()).sorted(comparingDouble(demination::getValue).reversed()).collect(toList())
is(可以论证)与手动创建列表相比,更能表达实际意图,更不容易出错。@AndyTurner是的,没错。观察和改进很好。
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());