0

メインメニューに戻る方法に問題があります。銀行に入金した後、メインメニューを再度ユーザーに表示したいのですが、何が問題なのかわかりません。正確な問題を特定するには..銀行に入金した後、「Y」を入力して「OK」ボタンをクリックすると、メインメニューが再び表示されるはずですが、表示されません。

これが私のプログラムです。

基本クラス:

import java.io.*;
import java.util.*;
public class BankAccount    
{

    public BankAccount(double b, String n) 
    {
       double balance = b; 
       String name = n;
    }

    public void deposit(double d)
    {
        balance += d;
    }

    public void withdraw(double w)
    {
        balance -= w;
    }

    public String nickname()
    {
        System.out.print("Enter a new name: ");
        Scanner kbIn = new Scanner(System.in);
        String n = kbIn.nextLine();
        return n;
    }

    double balance;
    String name;

}

テストクラス:

import java.awt.Component;
import java.io.*;
import javax.swing.JOptionPane;
import java.util.*;

public class Tester {
    private static String name;
    private static double bal;
    private static double withdraw;

    public static void main(String args[]) {
        Scanner kbInLine = new Scanner(System. in );
        Scanner kbIn = new Scanner(System. in );


        name = JOptionPane.showInputDialog(null, "Enter your name: ");

        String num;
        int pin;
        num = JOptionPane.showInputDialog("Enter your pin number: ");
        pin = Integer.parseInt(num);

        JOptionPane.showMessageDialog(null, "Login Success\n" + "Name : " + name + "\n" + "Pin Number : " + pin);



        BankAccount myAccount = new BankAccount(withdraw, name);


        String[] buttons = {
            "Deposit", "Withdraw", "Print Balance", "Exit"
        };
        int rc = JOptionPane.showOptionDialog(null,
            "What would you like to do?",
            "Confirmation",
        JOptionPane.INFORMATION_MESSAGE,
        0,
        null,
        buttons,
        buttons[2]);


        if (rc == 0) {
            int deposit;
            String dep = JOptionPane.showInputDialog("How much would you like to deposit?\n\t$ ");
            deposit = Integer.parseInt(num);
            JOptionPane.showMessageDialog(null, "You have deposited $" + dep + " into the account of " + name);

            String proceeds = "y";
            while (proceeds.equalsIgnoreCase("y"))
            do {
                proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
            } while (proceeds.equalsIgnoreCase("Y"));

            System.exit(0);
        }


        if (rc == 1) {
            double withdraw;
            String with = JOptionPane.showInputDialog("How much would you like to withdraw?\n\t$");
            withdraw = Integer.parseInt(num);
            if (bal - withdraw > 0) {
                myAccount.withdraw(withdraw);
                JOptionPane.showMessageDialog(null, "You have withdrawn $" + withdraw + " from the account of " + name + ". The new balance is: " + myAccount.balance);
            } else {
                JOptionPane.showMessageDialog(null, "Sorry, you have insufficient funds for this operation. Your existing balance is $" + myAccount.balance);
            }

            String proceeds = "y";
            while (proceeds.equalsIgnoreCase("y"))
            do {
                proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
            } while (proceeds.equalsIgnoreCase("Y"));

            System.exit(0);
        }

        if (rc == 2) {
            JOptionPane.showMessageDialog(null, "The balance in the account of " + name + " with the pin number " + num + " is $" + bal);



            String proceeds = "y";
            while (proceeds.equalsIgnoreCase("y"))
            do {
                proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
            } while (proceeds.equalsIgnoreCase("Y"));

            System.exit(0);
        }


        if (rc == -1) {
            System.exit(0);
        } else {

            JOptionPane.showMessageDialog(null, "\nThank you. Have a good day!");
            System.exit(0);
        }


    }
}
4

3 に答える 3

1

より明確になるように、クラスを再編成しようとしています。また、パフォーマンスを向上させるためにいくつかの小さな変更を加えます。しかし、私はすべての提案や批評家を受け入れる準備ができています。

BankAccountクラス

public class BankAccount {

    private double balance;

    private String name;

    public BankAccount(double b, String n) {
        this.balance = b;
        this.name = n;
    }

    public void deposit(double d) {
        balance += d;
    }

    public void withdraw(double w) {
        balance -= w;
    }

    public String nickname() {
        System.out.print("Enter a new name: ");
        Scanner kbIn = new Scanner(System.in);
        String n = kbIn.nextLine();
        return n;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

} 

テスタークラス

    import java.awt.HeadlessException;
    import java.util.Scanner;

    import javax.swing.JOptionPane;

    public class Tester {

        private static String name;

    private static double withdraw;

    private static int pin;

    public static void main(String args[]) {
        Scanner kbInLine = new Scanner(System.in);
        Scanner kbIn = new Scanner(System.in);

        name = JOptionPane.showInputDialog(null, "Enter your name: ");

        String num = JOptionPane.showInputDialog("Enter your pin number: ");
        pin = Integer.parseInt(num);

        JOptionPane.showMessageDialog(null, "Login Success\n" + "Name : " + name + "\n" + "Pin Number : " + pin);

        BankAccount myAccount = new BankAccount(withdraw, name);

        int rc = getRC();
        processor(myAccount, rc);
    }

    private static int getRC() throws HeadlessException {
        String[] buttons = { "Deposit", "Withdraw", "Print Balance", "Exit" };
        int rc = JOptionPane.showOptionDialog(
            null,
            "What would you like to do?",
            "Confirmation",
            JOptionPane.INFORMATION_MESSAGE,
            0,
            null,
            buttons,
            buttons[2]);
        return rc;
    }

    private static void processor(BankAccount myAccount, int rc) throws HeadlessException, NumberFormatException {
        switch(rc) {
            case 0:
                processDeposit(myAccount, rc);
                break;
            case 1:
                processWithdraw(myAccount, rc);
                break;
            case 2:
                processBalance(myAccount, rc);
            default:
                processExit(rc);
                break;
        }
    }

    private static void processExit(int rc) throws HeadlessException {
        if(rc == -1) {
            JOptionPane.showMessageDialog(null, "\nThank you. Have a good day!");
            System.exit(0);
        }
    }

    private static void processDeposit(BankAccount myAccount, int rc) throws HeadlessException, NumberFormatException {
        double deposit;
        String dep = JOptionPane.showInputDialog("How much would you like to deposit?\n\t$ ");
        deposit = Double.parseDouble(dep);
        myAccount.deposit(deposit);

        JOptionPane.showMessageDialog(null, "You have deposited $" + dep + " into the account of " + name);
        processManager(myAccount);
    }

    private static void processWithdraw(BankAccount myAccount, int rc) throws HeadlessException, NumberFormatException {
        double withdraw;
        String with = JOptionPane.showInputDialog("How much would you like to withdraw?\n\t$");
        withdraw = Double.parseDouble(with);
        if(myAccount.getBalance() - withdraw >= 0) {
            myAccount.withdraw(withdraw);
            JOptionPane.showMessageDialog(null, "You have withdrawn $" + withdraw + " from the account of " + name
                    + ". The new balance is: " + myAccount.getBalance());
        } else {
            JOptionPane.showMessageDialog(
                null,
                "Sorry, you have insufficient funds for this operation. Your existing balance is $"
                        + myAccount.getBalance());
        }
        processManager(myAccount);
    }

    private static void processBalance(BankAccount myAccount, int rc) throws HeadlessException {
        JOptionPane.showMessageDialog(null, "The balance in the account of " + name + " with the pin number " + pin
                + " is $" + myAccount.getBalance());
        processManager(myAccount);
    }

    private static void processManager(BankAccount myAccount) throws HeadlessException, NumberFormatException {
        int rc;
        String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
        if(proceeds.equalsIgnoreCase("y")) {
            rc = getRC();
            processor(myAccount, rc);
        } else {
            processExit(-1);
        }
    }
}

あなたの間違いは、あなたの構造が良くなかったということでした、そしてあなたは絶対に撤退方法にいる間はする必要はありません。
私の投稿では、メソッドでメインメニューを表すメソッドをprocessWithdraw()呼び出し、選択したボタンの値を返します。getRc()最後に、戻り値をprocessor()

于 2013-02-10T12:51:04.493 に答える
0

よく確認してご覧ください。進行チェックに2 つのループを使用するのはなぜですか?

私はこれが良いと思います:

if (rc == 0) {
        do {
            int deposit;
            String dep = JOptionPane.showInputDialog("How much would you like to deposit?\n\t$ ");
            deposit = Integer.parseInt(num);
            JOptionPane.showMessageDialog(null, "You have deposited $" + dep + " into the account of " + name);


             String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
            } while (proceeds.equalsIgnoreCase("Y"));

            System.exit(0);
}

コードが次の理由で2 つのループを使用していると言いました。

while (proceeds.equalsIgnoreCase("y"))
            do {
                proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
            } while (proceeds.equalsIgnoreCase("Y"));

ただの意味

while (proceeds.equalsIgnoreCase("y")) {
            do {
                proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
            } while (proceeds.equalsIgnoreCase("Y"));
}

これは2 つの異なるネストされたループです。

于 2013-02-10T11:31:06.663 に答える
0

この数行で何をしようとしていますか?

 while (proceeds.equalsIgnoreCase("y"))
        do {
            proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another  transaction? (Y/N)");
        } while (proceeds.equalsIgnoreCase("Y"));

System.exit(0)また、すべてのブロックが... is this all your code で終わるスタート メニューを表示する必要があると考える理由がよくわかりません。

于 2013-02-10T11:31:47.273 に答える