2

銀行取引をシミュレートするプログラムの作成に取り組んでいます。入金、出金、または送金を希望するかどうかをユーザーに尋ねなければなりません。現在、私は口座の入出金オプションに取り組んでいます。

ユーザーがトランザクション (たとえば預金) を選択して番号を入力すると、プログラムが「このトランザクションを続行しますか? 明らかに、はいの場合、プログラムはトランザクションを続行し、いいえの場合は続行しません」と尋ねるようにしました。ユーザーが入力した番号を入金します。

私の問題は、 no オプションに何を入れる必要があるのか​​ 見当がつかないことです。トランザクションを拒否するということは、ループを終了する必要があることを意味するのか、それとも何を意味するのかわかりませんが、現時点では、いいえを押してもトランザクションは続行されます。以下は、トランザクションに入ったが続行したくない場合に何が起こるかを視覚的に表したものです。

ここに画像の説明を入力

以下は私のコード全体です。何を入力すればよいかわからないコードの部分に ** が付いています おそらく組織の役に立たないでしょう 申し訳ありません!

import java.util.Scanner;

public class BankTransactions {


public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    int num;
    double balance = 0;
    double checkingBalance= 0, savingsBalance =0;

    do {

        double amount;

        System.out.println("------------------------");
        System.out.println("Select a Transaction by typing number");
        System.out.println("1. Deposit");
        System.out.println("2. Withdrawal");
        System.out.println("3. Balance");
        System.out.println("4. Exit");
        System.out.println("------------------------");
        num = scan.nextInt();


        if (num == 1) { //if DEPOSIT is selected

            //ask to deposit from checking or savings
            System.out.println("------------------------");
            System.out.println("Would you like to deposit in checking or savings?");
                System.out.println("1. Checking");
                System.out.println("2. Savings");
            System.out.println("------------------------");
                num = scan.nextInt();

                if (num == 1) { //if CHECKING is selected
                    //enter amount to be deposited
                    System.out.println("------------------------");
                    System.out.println("Enter amount to deposit in checking account: ");
                    System.out.println("------------------------");
                    amount = scan.nextDouble();

                    //ask if they want to continue with transaction
                    System.out.println("------------------------");
                    System.out.println("Would you like to continue this transaction?");
                        System.out.println("1. Yes");
                        System.out.println("2. No");
                    System.out.println("------------------------");
                        num = scan.nextInt();

                    // Add the amount to the checking balance
                    checkingBalance += amount;
                    System.out.println("------------------------");
                    System.out.println("Your checking account's balance is " + checkingBalance);
                    System.out.println("------------------------");

                } else if (num == 2) { //if SAVINGS is selected
                    //enter amount to be deposited
                    System.out.println("------------------------");
                    System.out.println("Enter amount to deposit in savings account: ");
                    System.out.println("------------------------");
                    amount = scan.nextDouble();

                    //ask if they want to continue with transaction
                    System.out.println("------------------------");
                    System.out.println("Would you like to continue this transaction?");
                        System.out.println("1. Yes");
                        System.out.println("2. No");
                    System.out.println("------------------------");
                        num = scan.nextInt();

                        if (num == 1) {
                            // Add the amount entered to the savings balance
                            savingsBalance += amount;
                            System.out.println("------------------------");
                            System.out.println("Your savings account's balance is " + savingsBalance);
                            System.out.println("------------------------");
                        **} else if (num == 2) {
                            //EMPTY NEEDS CODE
                        }**
                }


        } else if (num == 2) { //if withdrawal is selected

            //ask to withdrawal from checking or savings
            System.out.println("------------------------");
            System.out.println("Would you like to withdrawal from checking or savings?");
                System.out.println("1. Checking");
                System.out.println("2. Savings");
            System.out.println("------------------------");
                num = scan.nextInt();

                if (num == 1) { //if checking is selected
                    //enter amount to be withdrawn
                    System.out.println("------------------------");
                    System.out.println("Enter amount to withdrawal: ");
                    System.out.println("------------------------");
                    amount = scan.nextDouble();

                    //ask if they want to continue with transaction
                    System.out.println("------------------------");
                    System.out.println("Would you like to continue this transaction?");
                        System.out.println("1. Yes");
                        System.out.println("2. No");
                    System.out.println("------------------------");
                        num = scan.nextInt();

                        if (num == 1) { //if you say yes to continuing
                            // Remove the amount from the balance
                            checkingBalance -= amount;
                            System.out.println("------------------------");
                            System.out.println("Your checking account's balance is " + checkingBalance);
                            System.out.println("------------------------");
                        } else if (num == 2) { //if you say no to continuing
                            //Do not remove amount from savings balance
                            //EMPTY NEEDS CODE
                        }                               

                } else if (num == 2) { //if savings is selected
                    //enter amount to be withdrawn
                    System.out.println("------------------------");
                    System.out.println("Enter amount to withdrawal: ");
                    System.out.println("------------------------");
                    amount = scan.nextDouble();

                    //ask if they want to continue with transaction
                    System.out.println("------------------------");
                    System.out.println("Would you like to continue this transaction?");
                        System.out.println("1. Yes");
                        System.out.println("2. No");
                    System.out.println("------------------------");
                        num = scan.nextInt();

                        if (num == 1) { //if you say yes to continuing
                            // Remove the amount from the savings balance
                            savingsBalance -= amount;
                            System.out.println("------------------------");
                            System.out.println("Your savings account's balance is " + savingsBalance);
                            System.out.println("------------------------");
                        } else if (num == 2) { //if you say no to continuing
                            //Do not remove amount from savings balance
                            //EMPTY NEEDS CODE
                        }
                }


        } else if (num == 3) { //if balance is selected

            //ask to see balance of checking or savings
            System.out.println("------------------------");
            System.out.println("Your Checking balance is " + checkingBalance);
            System.out.println("Your Savings balance is " + savingsBalance);
            System.out.println("------------------------");
                num = scan.nextInt();

                //needs to return to transaction options
        }



    } while (num != 4);

    System.out.println("------------------------");
    System.out.println("Good Bye!");
    System.out.println("------------------------");

}

}

私は立ち往生していて、それを理解したいと思っています。修正したコード全体を投稿しないでください。自分で直して学びたい!

4

3 に答える 3

3

ただし、トランザクションは、貯蓄のためではなく、チェックのために行われます。

理由は次のとおりです。

if (num == 1) { //if CHECKING is selected
                    //enter amount to be deposited
                    System.out.println("------------------------");
                    System.out.println("Enter amount to deposit in checking account: ");
                    System.out.println("------------------------");
                    amount = scan.nextDouble();

                    //ask if they want to continue with transaction
                    System.out.println("------------------------");
                    System.out.println("Would you like to continue this transaction?");
                        System.out.println("1. Yes");
                        System.out.println("2. No");
                    System.out.println("------------------------");
                        num = scan.nextInt();

                    // Add the amount to the checking balance
                    checkingBalance += amount;
                    System.out.println("------------------------");
                    System.out.println("Your checking account's balance is " + checkingBalance);
                    System.out.println("------------------------");

                } else if (num == 2) { //if SAVINGS is selected
                    //enter amount to be deposited
                    System.out.println("------------------------");
                    System.out.println("Enter amount to deposit in savings account: ");
                    System.out.println("------------------------");
                    amount = scan.nextDouble();

                    //ask if they want to continue with transaction
                    System.out.println("------------------------");
                    System.out.println("Would you like to continue this transaction?");
                        System.out.println("1. Yes");
                        System.out.println("2. No");
                    System.out.println("------------------------");
                        num = scan.nextInt();

                        if (num == 1) {
                            // Add the amount entered to the savings balance
                            savingsBalance += amount;
                            System.out.println("------------------------");
                            System.out.println("Your savings account's balance is " + savingsBalance);
                            System.out.println("------------------------");
                        **} else if (num == 2) {
                            //EMPTY NEEDS CODE
                        }**
                }

「num = scan.nextInt();」の後の違いに注意してください。どちらの場合も行?最初のものでは、入力に関係なくとにかく追加するように指示しています。2番目のケースでは、ユーザーが 1 を入力した場合にのみ追加する if/else ステートメントがあり、ユーザーが 2 を入力した場合は、何もしない。

1または2以外のオプションの場合にどうするかについてのあなたの質問について.elseステートメントを使用せずにifステートメントを使用してnumが1であるかどうかを確認するので、他のオプションが入力された場合は最初に移動しますまた。(ただし、2 が「いいえ」であると主張する場合は、else if(num != 2){System.out.println("Invalid Option. Going to the begin";} を使用できます)

于 2013-10-10T14:35:51.250 に答える