Java代码,算术和if / else语句问题

时间:2013-01-25 18:24:07

标签: java math

希望有人可以帮我解决这个问题。代码的开头你会看到我的psudo,它说明了我正在尝试做什么。只有在用C ++编写之前,转换才会让我失去一些。

package correctFare;


/* create static variables for figures that will remain constant throughout program (fare,dollar value etc.)
 * 
 * create variable that will be used to store the amount of money the user states is entered (entered amount must be 0=<x<=20
 * Prompt user to enter the amount of money they are inserting which will be stored in the input var.
 *      note* remind user that amount must be divisable by .25 and no greater than 20.0
 * 
 * subtract 2.25 from input, if the input-fare is not >= zero report error insufficient funds
 * divide input by 0.25, if the number is not a whole number, int, report error and state that all values bust be multiples of 25 cents
 * 
 * create open double variable as 'change'
 * take input, subtract 2.25 and store new amount as change
 * 
 * change is then divided by static variable ten, if zero continue to next step, else store amount as tenChange, change-(tenChange*10) continue to next step
 * follow with dividing change by 5, same steps as prior
 * repeat again with one
 * lastly finish with quarters
 * 
 * Prompt user with total cost of fare, amount entered and then tendered cash broken into each denomination
 */

import java.util.Scanner;

public class CTAFare {

    /**
     * @param args
     */

            static double fare=2.25;            //fare amount, remains easily accessible
            static double quarter=0.25;         //denomination amounts stored as static variables
            static int oneDollar=1;
            static int fiveDollar=5;
            static int tenDollar=10;
            static int twentyDollar=20;


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner in = new Scanner( System.in );

        int max = 20;
        int min = 0;
        double inputAmount;

        System.out.println("Please enter the amount of money you wish to insert:");
        System.out.println("\n *Please note, this machine will accept bills no larger than $20 and must be divisible by $0.25.");
            inputAmount = in.nextDouble();

            if (min <= inputAmount <= max) 
        {
            System.out.println("You entered: $"+ inputAmount);
        }
        else if (max < inputAmount < min)
        {
            System.out.println("The amount you entered is not acceptable.");
        }

            double change;
            int tenChange;
            int fiveChange;
            int oneChange;
            int quarterChange;

        inputAmount-fare = change;

        if (change/tenDollar > 0) {
            change/tenDollar = tenChange;
            change - (tenChange*10) = change;

        }
        else if (change/fiveDollar > 0) {
            change/five = fiveChange;
            change - (fiveChange * 5) = change;

        }
        else if (change/oneDollar > 0) {
            change/onDollar = oneChange;
            change - (oneChange * 1) = change;
        }
        else if (change/quarter > 0) {
            change/quarter = quarterChange;
            change - (quarter*0.25) = change;
        }

        System.out.println("You have purchased one fare at $"+ fare".\n");
        System.out.println("Amount insterted: $"+ inputAmount "\n");
        System.out.println("Change tendered: \n");
        System.out.println("Ten Dollar Bills: "+ tenChange " \n");
        System.out.println("Five Dollar Bills: "+ fiveChange " \n");
        System.out.println("One Dollar Bills: "+ oneChange " \n");
        System.out.println("Quarters: "+ quarterChange " \n");



    }

}

3 个答案:

答案 0 :(得分:1)

这可能是我注意到的问题

if (min <= inputAmount <= max) 

尝试使用

if (min <= inputAmount && inputAmount <= max)

这是另一个错误

 change/five = fiveChange;

你不能这样做。试试以下

 double newFiveCHange =  change/five ;

或像他们一样互相支持

 fiveChange = change/five;

以下是您可以使用的有用链接。

1. Summary of Operators

2. The if-then and if-then-else Statements

3. Expressions, Statements, and Blocks

答案 1 :(得分:1)

C ++中的赋值是从右到左完成的,我假设它与Java类似。尝试更改作业......

change/five = fiveChange;

应该是

fiveChange = change/five;

在这个例子中,你还没有定义五个是什么。与Python不同,您需要在使用它们之前或之后定义变量。

此外,您可能想要考虑每种类型。在某些情况下,我不确定int是最佳选择。尝试修复作业和声明,然后查看输出是否正确。

答案 2 :(得分:0)

只需替换:

if (min <= inputAmount <= max)

通过

if (min <= inputAmount && inputAmount <= max)

并替换:

else if (max < inputAmount < min)

通过

else