我不明白为什么我不能调用我的方法。我检查了几次并尝试运行它,但它总是在我输入物有所值后立即结束。它应该返回给定金额的找零金额,使用百元、五十元、二十元、十元、五元、一元、二十五分、一角、五分和便士。因此,如果是 42.55 美元,则输出将为“0 0 2 0 0 2 2 0 1 0”(两个二十,两个单打,两个 25 美分,一个镍币)。预先感谢您!
import java.util.Scanner;
public class MakeChange {
public static void main(String arg[]) {
Scanner readmoney = new Scanner(System.in);
System.out.println("Money amount? >");
double money = readmoney.nextDouble();
System.out.println();
String thing = makeChange(money);
}
public static String makeChange(double money) {
int hundreds = (int) money/100;
int fifties = (int)(money/50) - (2*hundreds);
int twenties = (int) (money/20) - (5*hundreds) - (int)(2.5*fifties);
int tens = (int)(money/10) - (10*hundreds) - (5*fifties) - (2*twenties);
int fives = (int)(money/5) - (20*hundreds) - (10*fifties) - (4*twenties) - (2*tens);
int singles = (int)(money) - (100*hundreds) - (50*fifties) - (20*twenties) - (10*tens) - (5*fives);
int quarters = (int)(money/0.25) - (400*hundreds) - (200*fifties) - (80*twenties) - (40*tens) - (20*fives) - (4*singles);
int dimes = (int)(money/0.1) - (1000*hundreds) - (500*fifties) - (200*twenties) - (100*tens) - (50*fives) - (10*singles) - (int)(2.5*quarters);
int nickels = (int)(money/0.05) - (2000*hundreds) - (1000*fifties) - (400*twenties) - (200*tens) - (100*fives) - (20*singles) - (5*quarters) - (2*dimes);
int pennies = (int)(money/0.01) - (10000*hundreds) - (5000*fifties) - (2000*twenties) - (1000*tens) - (500*fives) - (100*singles) - (25*quarters) - (10*dimes) - (5*nickels);
String change = (hundreds + " " + fifties + " " + twenties + " " + tens + " " + fives + " " + singles + " " + quarters + " " + dimes + " " + nickels + " " + pennies);
return change;
} }
最佳答案
String thing = makeChange(money);
}
// end of program
您没有打印结果。
该方法被调用(并且不会因异常而崩溃)。
关于java - 有人可以帮我弄清楚为什么我无法调用我的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26644076/