java新手。阅读其他一些问题,但尚未找到解决方案。我希望将 acname , dob 和 balance1 (用户的所有输入)传递给另一个类。目前它导致nullnullnull。请帮忙:
头等舱:
public static String acname;
public static String dob;
public static String balance1;
public static void main(String[] args) {
switch (menu) {
case 1: Scanner sc=new Scanner(System.in);
System.out.println("Please enter account holders name:");
String acname = sc.nextLine();
Scanner sc2=new Scanner(System.in);
System.out.println("Please enter account holders date of birth:");
String dob = sc2.nextLine();
BankAccount balance1 = new BankAccount(0, 0.10);
balance1.getBalance();
System.out.println("Account Created. Overview and Balance: " + "\n" + acname + "\n" + dob + "\n" + "£" + balance1.getBalance() + "\n");
第二课:
public class MainMenu {
public static void main(String[] args) {
System.out.println("--WELCOME TO BEAN BANKING LTD--");
System.out.println("");
System.out.println("1. Create new account");
System.out.println("2. View account details and balance");
System.out.println("3. Deactivate account");
System.out.println("4. Exit System");
Scanner scanner = new Scanner(System.in);
int menu = scanner.nextInt();
switch (menu) {
case 1: AccountCreator.main(null);
case 2: System.out.println(AccountCreator.acname + AccountCreator.dob + AccountCreator.balance1);
答案 0 :(得分:1)
您声明的局部变量与您的类变量具有相同的名称。除非您符合对它们的引用,否则将假定局部变量。
而不是:
String acname = sc.nextLine();
只是做:
acname = sc.nextLine();