-3

希望有人可以帮我处理这段代码。代码的开头你会看到我的 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");



    }

}
4

3 回答 3

1

这可能是我注意到的一个问题

if (min <= inputAmount <= max) 

尝试使用

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

这是另一个错误

 change/five = fiveChange;

你不能这样做。尝试关注

 double newFiveCHange =  change/five ;

或交换他们喜欢

 fiveChange = change/five;

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

一、算子总结

2. if-then 和 if-then-else 语句

3. 表达式、语句和块

于 2013-01-25T18:27:56.647 回答
1

C++ 中的赋值是从右到左完成的,我假设这与 Java 类似。尝试改变你的任务...

change/five = fiveChange;

应该

fiveChange = change/five;

在这个例子中,你也没有在任何地方定义什么是五。与 Python 不同,您需要在使用变量之前或使用时定义变量。

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

于 2013-01-25T18:36:00.983 回答
0

只需更换:

if (min <= inputAmount <= max)

经过

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

并替换:

else if (max < inputAmount < min)

经过

else

于 2013-01-25T18:33:05.450 回答