正在练习模拟银行应用程序(在Java / intelliJ中工作),在其中我希望能够创建新的银行分支机构,然后在这些分支机构中创建新客户(分支机构和客户的单独类)。我创建了变量branchChoice,该变量应从扫描仪获取用户输入,但是当我尝试将其进一步打印时,它将打印为空。例如:
System.out.println(“按3将客户添加到” + branchChoice +“分支。);
打印为:
按3将客户添加到分支机构。
但是显然,它应该打印用户输入的分支的名称。 这是整个Main类:
import java.util.Scanner;
public class main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Bank bank = new Bank();
boolean flag = true;
while (flag) {
boolean flag2 = true;
System.out.println("Press 0 to see a list of branches");
System.out.println("Press 1 to search for a branch");
System.out.println("Press 2 to add a new branch");
Branch foundBranch = new Branch("Placeholder");
while (flag2) {
int choice1 = scanner.nextInt();
switch (choice1) {
case 0:
bank.printBranchesList();
System.out.println("Select a branch:");
String branchChoice = scanner.nextLine();
scanner.nextLine();
foundBranch = bank.convertStringToBranch(branchChoice);
System.out.println("Press 3 to add a customer to the " + branchChoice + " branch.");
System.out.println("Press 4 to see a list of customers registered to the " + branchChoice + " branch");
break;
case 1:
case 2:
scanner.nextLine();
bank.addBranch(Branch.createNewBranch(scanner.nextLine()));
flag2 = false;
break;
case 3:
foundBranch.addCustomer(Customer.createNewCustomer("Doyle"));
System.out.println("customer added");
}
}
}
}
}
我不知道为什么它不打印输入的字符串。任何帮助将不胜感激,谢谢。