首页 > 解决方案 > Java - 访问在不同的“if/else if”语句中创建的对象?

问题描述

我目前正在做一个项目(一个非常简单的临时书店),我有两个类(一个用于管理用户帐户,一个用作驱动程序类),其中一个菜单通过 main 方法呈现给用户驱动程序类。根据项目规范,此菜单由while循环表示,用户在其中键入与多个菜单选项之一相对应的单个字符。在这个while循环中是一系列if / else if语句,每个语句都包含一个菜单选项的代码。这些菜单选项如下:

Main Menu:

N - Create account
L - Load money into your account balance
O - Order a book
S - Print an account summary
X - Quit

我认为, while循环的目的是在用户完成各种子菜单后继续循环返回主菜单,仅当用户输入为“X”时才结束。

当用户输入为“N”并启动“创建帐户”菜单时,将执行以下代码:

if(menuInput == 'N') {
            // Scanner sc has already been declared at top of main method

            System.out.println("Enter your desired username:");
            in_userName = sc.next();
            // String in_userName has already been declared at top of main method

            System.out.println("Enter a starting balance:");
            in_initBal = sc.nextDouble();
            // double in_initBal has already been declared at top of main method

            UserAccount user = new UserAccount(in_userName,in_initBal);
            accountCheck = true;

         /* accountCheck is a boolean that makes sure an account has been
            created; it has already been declared/initialized to 'false' 
            prior to the while loop
         */

}

到目前为止,我编写的驱动程序类中唯一的其他菜单选项是'L - Load money into your account balance'。该菜单选项的代码如下:

else if(menuInput == 'L') {
            if(accountCheck == false) {
                System.out.println("You must create an account first. Enter N                                                                      
                                    to create one.");
            }
            else {
                System.out.println("Enter the amount to add:");
                in_amountToAdd = sc.nextDouble(); 
                // double in_amountToAdd has already been declared at top of main method   

                user.addToBalance(in_amountToAdd);     /* Method in the User Account class
                                                          that takes a double parameter and
                                                          adds it to the existing balance.
                                                       */
                System.out.println(user.getBalance());
            }

问题是user.addToBalance(in_amountToAdd)System.out.println(user.getBalance())行无法编译,因为"user cannot be resolved"我在与“创建帐户”选项相对应的if语句中创建了构造函数,但不知道如何在其他if / else if块中实现它,所以我想知道是否有办法:

  1. 获取“Load money into account”子菜单的if语句,以从“Create account”子菜单中识别“user”构造函数,这样我在这里包含的代码就可以编译,或者:
  2. 在 main 方法的开头初始化“用户”构造函数,然后稍后将其参数参数更改/更新为来自扫描仪的输入值。

我对上面列表中第二个的思考过程是,在 while 循环之前声明/初始化构造函数将允许每个if / else if块看到它,而目前似乎只有“创建帐户”块可以看到它(我猜这是因为构造函数是在这里创建的,并且对于这个特定的if语句来说是本地的)。

如果我使用的任何术语错误或不准确,并且我的任何代码令人困惑/不切实际,我提前道歉。我只是一个初学者,所以如果你们能指出我犯的任何错误作为我的学习经验,我将不胜感激。

谢谢!

标签: javaeclipseif-statementwhile-loopconstructor

解决方案


while您需要在循环之前声明变量和if-else

例如

UserAccount user = null;

while (...) {
    if (...) {
      user = new UserAccount(in_userName,in_initBal);
    }
    else {
       user.addToBalance(in_amountToAdd); 
    }
}

推荐阅读