将数字除以其他有效数字

时间:2013-10-05 15:33:20

标签: java currency

标题很混乱,因为我不知道该怎么称呼这个问题。无论如何,我有java编程功课,需要一个数字分成正确的变化量。我在使用程序时没有收到任何消息,我可以输入数字,然后什么也没发生。

由于

-Jordan

double input;   // The input
double l = 0;   // Toonies (2.00 dollars)
double t = 0;   // loonies (1.00 dollars)
double q = 0;   // quarters (0.25 dollars)
double d = 0;   // dimes (0.10 dollars)
double n = 0;   // nickels (0.05 dollars)
double p = 0;   // pennies (0.01 dollars)

System.out.println("Hello, this application will tell you how much"
        + "change you have, based on your input.");
System.out.println("Please enter a real integer");
input = TextIO.getDouble();     // Retrieves the next double entered

while (input > 2) {
    t++;
} // Closes of toonie statement

while (input > 1) {
    l++;
} // Closes of loonie statement

while (input > 0.25) {
    q++;
} // Closes of quarter statement

while (input > 0.1) {
    d++;
} // Closes of dime statement

while (input > 0.05) {
    n++;
} // Closes of nickel statement

while (input > 0.01) {
    p++;
} // Closes of penny statement

System.out.println("You have "  // Prints a message saying how many of each coin you have
        + t + "toonies, "
+ l + "loonie(s), "
+ q + "quarter(s), "
+ d + "toonie(s), "
+ n + "toonies(s), "
+ p + "pennies(s), ");

4 个答案:

答案 0 :(得分:2)

在每个while循环中,您还需要从输入中减去金额。既然你不是,它可能会在其中一个上进入无限循环。例如:

while (input > 2) {
    t++;
    input -= 2;
}

答案 1 :(得分:1)

你没有减少输入,所以你会陷入初始条件为真的第一个while的无限循环中:

您应该减少循环中的input金额:

while (input > 2) {
    t++;
    input -=2;
}

答案 2 :(得分:0)

变量'input'永远不会减少,因此一旦输入其中一个while循环,就无法退出它,它将一直持续到你通过其他方式停止程序。

答案 3 :(得分:0)

我建议你以美分计算。这样你可以使用很长时间。您可以使用整数除法,而不是使用循环。这将更快,更短,更准确。