Как передать введенное пользователем целое число через методы? - PullRequest
0 голосов
/ 01 мая 2019

По сути, я выделил проблему, int numpersons начинается с 0. Я беру пользовательский ввод, чтобы сделать его конкретным числом, равным размеру массива, когда начинается второй метод, он снова принимает 0, а затем массив имеет исключение вне границ. Я хочу передать его от одного метода к другому или сделать их более последовательными. ИДК, как это сделать

заранее спасибо

import java.util.Scanner;

public class BankApp {
Scanner input = new Scanner(System.in);
int numpersons = 0;
private SavingsAccount[] clients = new SavingsAccount[numpersons];

public BankApp() {
    while (numpersons < 1) {
        System.out.println("How many people are there?");
        numpersons = input.nextInt();
        if (numpersons < 1 || 2147483647 < numpersons) {
            System.out.println("invalid number, please enter again");
        }
    }
    input.nextLine();
}

public void addClients() {
    int i = 0;
    while (i < numpersons) {
        System.out.println("enter account id " + (i + 1));
        String AccountID = input.nextLine();
        System.out.println("enter account name " + (i + 1));
        String AccountName = input.nextLine();
        System.out.println("enter account balance " + (i + 1));
        Double AccountBalance = input.nextDouble();
        clients[i] = new SavingsAccount(AccountID, AccountName, AccountBalance);
        input.nextLine();
        i++;
    }
}

public void displayClients() {
    int i = 0;
    while (i < numpersons) {
        System.out.println("======================================");
        System.out.println("Account ID " + (i + 1) + ": " + clients[i].getID());
        System.out.println("Account Name " + (i + 1) + ": " + clients[i].getName());
        System.out.println("Account Balance " + (i + 1) + ": " + clients[i].getBalance());
        System.out.println("======================================");
        i++;
    }
}

public static void main(String args[]) {
    BankApp ba = new BankApp();
    ba.addClients();
    ba.displayClients();

}

}

...