为了账号安全,请及时绑定邮箱和手机立即绑定

Java getter setter 无法存储值

Java getter setter 无法存储值

慕森卡 2023-06-14 14:56:12
我试图回答这些问题。新帐户选项应实现以下内容:输入客户详细信息:姓名、地址、生日和联系电话输入不少于 PhP5,000 的初始存款随机生成一个四位数的账号余额查询选项应执行以下操作:输入帐号并验证如果账号有效,显示客户名称和当前余额我已经尝试在其中使用 setter 方法对“新帐户”选项进行编码,并且还随机生成一个四位数,我可以使用它在具有 getter 方法但显示为空的“余额查询”选项中输入它。我尝试调试它,退出 if 语句后变量返回空。使用主要方法、用于选项的 displayMainMenu()、newAccount() 和 fourRandomNumber() 进行分类。public class ClientUgang {     public static void main(String[] args) {        displayMainMenu();    }    public static void displayMainMenu() {        SavingsAccountUgang savingsAccount = new SavingsAccountUgang();        int option = 0;        while (option != 7) {            Scanner scan = new Scanner(System.in);            System.out.println("JBank Main Menu");            System.out.println("[1] New Account");            System.out.println("[2] Balance Inquiry");            System.out.println("[3] Deposit");            System.out.println("[4] Withdraw");            System.out.println("[5] Client Profile");            System.out.println("[6] Close Account");            System.out.println("[7] Exit");            option = scan.nextInt();            if (option == 1) {                newAccount();            }            if (option == 2) {                savingsAccount.balanceInquiry();            }        }    }    public static void newAccount() {        Scanner scan = new Scanner(System.in);        SavingsAccountUgang savingsAccount = new SavingsAccountUgang();        System.out.print("Name: ");        String name = scan.nextLine();        System.out.print("Address: ");        String address = scan.nextLine();        System.out.print("Birthday: ");        String birthday = scan.nextLine();        System.out.print("Contact number: ");        String contactNumber = scan.nextLine();        savingsAccount.setAccountName(name);        savingsAccount.setAddress(address);        savingsAccount.setBirthday(birthday);        savingsAccount.setContactNumber(contactNumber);        }    }我的 balanceInquiry() 方法所在的类。我的 accountName setter 和 getter 方法在 BankAccountUgang 类中。
查看完整描述

3 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

您的代码有很多问题。


要修复该 1 方法如下:


从该方法返回一个新的储蓄账户newAccount,因此将返回类型更改为:

public static SavingsAccountUgang newAccount() {

    // Your existing code

    return savingsAccount;

}

然后在您的displayMainMenu()方法中保存此帐户,如果用户输入 1 作为输入,然后使用该实例显示余额:

public static void displayMainMenu() {

    SavingsAccountUgang savingsAccount = null // don't create object here as you are doing

    // Your code

    if (option == 1) {

        savingsAccount = newAccount();

    }

    if (option == 2) {

        if(savingsAccount  == null) {

            // throw exception or whatever you want to do.

        }

        savingsAccount.balanceInquiry();

    }

}


查看完整回答
反对 回复 2023-06-14
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

好吧,该setBalance()方法需要一个参数 type double,但是您发送了一个 type int,但这不是错误的原因。this此外,您应该在这样的声明中使用:

while (accountNumber != this.getAccountNo());


查看完整回答
反对 回复 2023-06-14
?
MYYA

TA贡献1868条经验 获得超4个赞

SavingsAccountUgang您方法中的实例是newAccount()局部变量,因此仅对此方法可见。如果你想在你的方法之外使用它,你必须在你的方法之外返回或声明它。



查看完整回答
反对 回复 2023-06-14
  • 3 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信