亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java getter setter 無法存儲值

Java getter setter 無法存儲值

叮當貓咪 2023-06-08 20:41:57
我試圖回答這些問題。新帳戶選項應實現以下內容:輸入客戶詳細信息:姓名、地址、生日和聯系電話輸入不少于 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);        }    }}
查看完整描述

3 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

您的代碼有很多問題。


要修復該 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-08
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

SavingsAccountUgang您方法中的實例是newAccount()局部變量,因此僅對此方法可見。如果你想在你的方法之外使用它,你必須在你的方法之外返回或聲明它。



查看完整回答
反對 回復 2023-06-08
?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

好吧,該setBalance()方法需要一個參數 type double,但是您發送了一個 type int,但這不是錯誤的原因。this此外,您應該在這樣的聲明中使用:

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


查看完整回答
反對 回復 2023-06-08
  • 3 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號