-3

誰かがこのコードを手に入れてくれることを願っています。コードの最初に、私がやろうとしていることを示す私の疑似が表示されます。これまで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;

以下は、使用できる便利なリンクです。

1.オペレーターの概要

2.if-thenおよびif-then-elseステートメント

3.式、ステートメント、およびブロック

于 2013-01-25T18:27:56.647 に答える
1

C ++での割り当ては右から左に行われ、Javaでも同様であると思います。割り当てを変更してみてください...

change/five = fiveChange;

する必要があります

fiveChange = change/five;

この例でも、5つがどこにあるかを定義していません。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 に答える