1

私は教授から次のコードを与えられました。

public static void main(String[] args) {

    Customer customer;
    Transaction transaction;
    double withdrawalAmount = 0;

    boolean finished = false;

    while (finished == false)
    {
        // Menu Display and Get user input
        int inputInt = 0;
        while (inputInt == 0)
        {
            inputInt = displayMenuAndGetInput();

                    // if the input is out of range
                    if ((inputInt < 1) || (inputInt > 8))
                    {
                            System.out.println("\nThe input is out of range!");
                            System.out.println();
                            inputInt = 0;
                    }
            } //end while

            // switch to correspondence function
            switch (inputInt)

            {
                case 1:
                    customer = createPersonalCustomer();
                    System.out.println("\nThe Personal customer has been created: \n" + newPC.toString());
                    customers.add(customer);
                    break;
                case 2:
                    customer = createCommercialCustomer();
                    System.out.println("\nThe Commercial customer has been created: \n" + newCC.toString());
                    customers.add(customer);
                    break;
                case 3:
                    transaction = recordTransaction();
                    if(transaction != null)
                        System.out.println("\nThe Transaction has been created: \n" + trans.toString());
                    else
                        System.out.println("\nThe ID could not be found.");
                    break;
                case 4:
                    withdrawalAmount = makeWithdrawal();
                    if(withdrawalAmount > 0)
                        System.out.println("\nAmount withdrawn from this account: " + moneyFormat.format(acct.getMakeWithdrawal()) + "\n");
                    else
                        System.out.println("\nThe ID could not be found.");
                    break;
                case 5:
                    displayCustomer();
                    break;
                case 6:
                    displayCustomerSummary();
                    break;
                case 7:
                    displayGrandSummary();
                    break;
                case 8:
                    // exit
                    finished = true;
                    break;
                default:
                    System.out.println("Invalid Input!");

                    break;
            } // end switch
    } // end while

}

私は次のコードを取ることになっています

// Create a new Transaction
public static Transaction recordTransaction(){}

次のシナリオで機能するループを作成します。

顧客IDが入力され、顧客IDが配列内で一致しない場合、ケース3で読み出されたエラーが発生し、メインメニューが表示されます。顧客 ID が有効な場合、ユーザーは以下の入力情報を入力します。

以下は私のコードです

public static Transaction recordTransaction(){

System.out.println("Enter the customer ID to create the transaction > ");
    long customerID = scan.nextLong();

    for (Customer c : customers) {
        if (c.getCustomerID() == customerID) {
            if (trans != null) {

            System.out.println("\nEnter the weight of gold > ");
                Transaction.goldWt = scan.nextDouble();

                System.out.println("\nEnter the weight of platinum > ");
                Transaction.platinumWt = scan.nextDouble();

                System.out.println("\nEnter the weight of silver > ");
                Transaction.silverWt = scan.nextDouble();
                }
        }
            return null; 
}

とにかく、これをさまざまな方法で実行しましたが、コードが無効で有効な顧客 ID を受け入れるか、無効または有効な顧客 ID を受け入れません。私はおそらく何かを見落としていることを知っており、それが私が必死にフォーラムの助けを求めている理由です. 私はプログラミングに関して OCD の傾向があり、これは私の最初の入門 Java クラスであるため、言語に精通していません。私は過去 2 日間、この問題に悩まされていました。助けてください。

4

3 に答える 3

1

new Transaction()メソッド内でa をインスタンス化し、必要にrecordTransaction()応じてそれを返す必要があります。

public static Transaction recordTransaction(){

    System.out.println("Enter the customer ID to create the transaction > "); long customerID = scan.nextLong();

    for (Customer c : customers) {
        if (c.getCustomerID() == customerID) {
            Transaction trans = new Transaction();

            System.out.println("\nEnter the weight of gold > ");
            trans.goldWt = scan.nextDouble();

            System.out.println("\nEnter the weight of platinum > ");
            trans.platinumWt = scan.nextDouble();

            System.out.println("\nEnter the weight of silver > ");
            trans.silverWt = scan.nextDouble();

            return trans;
        }
    }
    return null; 
}
于 2013-03-07T15:43:44.123 に答える
0

この問題は、作成中の各アカウントのランダムな customerID の getter および setter メソッドによって生成されていました。

メルシーありがとう!

于 2013-03-08T03:28:58.250 に答える
0

あなたのコードに関するいくつかのコメント:

if (trans != null) {

「トランス」とは?Transaction インスタンスを参照する必要があると思います。

Transaction.goldWt = ...

「trans.goldWt」ではないでしょうか。(もちろん、プランチナムとシルバーについても同じです)。それが本当ならTransaction.goldWt、あなたは常にその単一の値を変更しています。

return null;

何も返さないため、呼び出しコードは常に「IDなし」になります。" return trans" じゃないの?

于 2013-03-07T15:44:08.887 に答える