0

在下面的代码中,当从 main 方法调用时,方法 deposit 或withdraww 或 deposit 或 transfer 没有被完全执行。我是一个基本的 Java 学习者,这是我的第一个程序之一。你能帮我做同样的事情吗?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Bank {

    static Scanner input = new Scanner(System.in);
    static String name, no;
    static int accNo, accNo1, bal, amt, a, tra, acct, bal1, bala, acct2, ac1;

    Bank(String name, String no, int accNo, int accNo1, int bal, int bal1, int ac1) {
        this.name = name;
        this.no = no;
        this.accNo = accNo;
        this.bal = bal;
    }

    private Bank(String name, String no, int accNo, int accNo1, int bal, int amt, int a, int tra, int acct, int bal1, int bala, int acct2, int ac1) {
    }

    static void deposit() {
        System.out.println("Enter the account to which amount is to be deposited");
        Scanner ac = new Scanner(System.in);
        int ac1 = ac.nextInt();

        System.out.println("Enter amount to deposit: (Enter in Multiples of Rs.500)");
        amt = input.nextInt();

        if (ac1 == accNo) {
            bal = bal + amt;
            System.out.println("Acct no:" + accNo);
            System.out.println("Balance:Rs. " + bal);
        } else if (ac1 == accNo1) {
            bal1 = bal1 + amt;
            System.out.println("Acct no:" + accNo1);
            System.out.println("Balance:Rs. " + bal1);
        } else if ((acct2 != accNo) || (acct2 != accNo1)) {
            System.out.println("Account no not registered");
        }
    }

    static void withdraw() {
        System.out.println("Enter the account from which amount is to be withdrawn");
        Scanner ac3 = new Scanner(System.in);
        int ac2 = ac3.nextInt();
        if (ac2 == accNo) {
            System.out.println("Enter amount to withdraw");
            amt = input.nextInt();
            bal = bal - amt;
            System.out.println("Acct no:" + accNo);
            System.out.println("Balance:Rs. " + bal);
        } else if (ac2 == accNo1) {
            System.out.println("Enter amount to withdraw");
            amt = input.nextInt();
            bal1 = bal1 - amt;
            System.out.println("Acct no:" + accNo1);
            System.out.println("Balance:Rs. " + bal1);
        } else {
            if ((acct2 != accNo) || (acct2 != accNo1)) {
                System.out.println("Account no not registered");
            }
        }
    }

    static void display() {

        System.out.println("Enter the account number for balance:");
        bala = input.nextInt();
        if (bala == accNo) {
            System.out.println("Account No:" + accNo);
            System.out.println("The balance in the account is Rs." + bal);

        } else if (bala == accNo1) {
            System.out.println("Account No:" + accNo1);
            System.out.println("The balance in the account is Rs." + bal1);

        }
        if ((acct2 != accNo) || (acct2 != accNo1)) {
            System.out.println("Account number not registered!!");
        }
    }

    static void transfer() {

        System.out.println("Enter the account number to which amount is to be transferred:");
        acct2 = input.nextInt();
        if (acct2 == accNo) {
            System.out.println("Enter the amount to be transferred:");
            tra = input.nextInt();
            if (bal < tra - 500) {
                System.out.println("Insufficient funds. Minimum balance to be maintained in your account is Rs.500");
            } else {
                bal = bal - tra;
                bal1 = bal1 + tra;
            }
        } else if (acct2 == accNo1) {
            if (bal1 < tra - 500) {
                System.out.println("Insufficient funds. Minimum balance to be maintained in your account is Rs.500");
            } else {
                bal1 = bal1 - tra;
                bal = bal + tra;
            }

        } else {
            if ((acct2 != accNo) || (acct2 != accNo1)) {
                System.out.println("Account No not registered !!");
            }
        }
    }

    public static void main(String args[]) {

        System.out.println("Welcome to personal account netbanking!");
        System.out.println("To become a registered user of netbanking, kindly enter the following details");
        System.out.println("Please enter your Full Name: ");
        String name = input.nextLine();
        System.out.println("Please enter your Contact Number:");
        Scanner no1 = new Scanner(System.in);
        String no = no1.nextLine();
        System.out.println("Please enter the first  Account Number ");
        Scanner acc1 = new Scanner(System.in);
        int accNo = acc1.nextInt();
        System.out.println("Please enter the second  Account Number ");
        Scanner acc2 = new Scanner(System.in);
        int accNo1 = acc1.nextInt();
        System.out.println("Please enter the Amount to be deposited in the fist account");
        Scanner amt1 = new Scanner(System.in);
        int bal = amt1.nextInt();
        System.out.println("Please enter the Amount to be deposited in the second account");
        Scanner amt2 = new Scanner(System.in);
        int bal1 = amt2.nextInt();
        System.out.println("Thank you for registering");
        System.out.println("Kindly make a note of your username: 'admin' and password: 'admin'");
        System.out.println("Please enter the user name: ");
        String u = input.nextLine();
        System.out.println("Please enter the password: ");
        BufferedReader p = new BufferedReader(new InputStreamReader(System.in));
        String y = input.nextLine();
        Bank b1 = new Bank(name, no, accNo, accNo1, bal, amt, a, tra, acct, bal1, bala, acct2, ac1);
        if ("admin".equals(u) && "admin".equals(y)) {


            int menu;
            System.out.println(" Welcome " + name);
            boolean quit = false;
            do {

                System.out.println("Please enter your choice: ");
                System.out.println("1. Balance Enquiry");
                System.out.println("2. Deposit Amount");
                System.out.println("3. Withdraw Amount ");
                System.out.println("4. Transfer Amount");
                System.out.println("5. Exit");
                menu = input.nextInt();

                switch (menu) {
                    case 1:
                        b1.display();
                        break;

                    case 2:
                        b1.deposit();
                        break;

                    case 3:
                        b1.withdraw();
                        break;
                    case 4:
                        b1.transfer();
                        break;

                    case 5:
                        quit = true;
                        break;
                }
            } while (!quit);
        } else {
            System.out.println("Invalid username or password");
        }
    }
}
4

2 回答 2

2

问题是您有两个构造函数,当您调用“new Bank”时,会调用空构造函数。将第一个构造函数的赋值复制到第二个构造函数中,或者只是从空的构造函数中调用好的构造函数。


编辑:

你有这个:

Bank(String name, String no, int accNo, int accNo1, int bal, int bal1, int ac1) {
    this.name = name;
    this.no = no;
    this.accNo = accNo;
    this.bal = bal;
}

private Bank(String name, String no, int accNo, int accNo1, int bal, int amt, int a, int tra, int acct, int bal1, int bala, int acct2, int ac1) {
}

你应该(至少)有这个:

Bank(String name, String no, int accNo, int accNo1, int bal, int bal1, int ac1) {
    this.name = name;
    this.no = no;
    this.accNo = accNo;
    this.bal = bal;
}

private Bank(String name, String no, int accNo, int accNo1, int bal, int amt, int a, int tra, int acct, int bal1, int bala, int acct2, int ac1) {
    this.name = name;
    this.no = no;
    this.accNo = accNo;
    this.bal = bal;
}
于 2013-05-06T16:46:46.520 回答
1

我猜你的问题是代码在某处抛出异常,这只是部分运行。尝试(没有双关语)在您的代码周围放置一个 try catch 块:

public static main(String args[]) {

    try {

         // you existing code goes here

    } catch (Throwable t) {

        System.out.println(t);

    }

}

这至少会告诉你问题是什么。

于 2013-05-06T16:40:40.690 回答