簡體   English   中英

從不將輸入作為其簽名的一部分的另一種方法調用一個方法(Java)

[英]Calling one method from another method that does not take input as part of its signature (Java)

我有這個方法可以返回存款金額

public double take_deposit_amount_into_checking_account() throws InputMismatchException {
    System.out.println("Enter the amount to be depsosited in the format XX:YY");
    Scanner sc = new Scanner(System.in);
    double deposit_amount = sc.nextDouble();
    System.out.println("You have deposited " + deposit_amount + " into your checkings account ");
    user_option(); // call user option
    return deposit_amount;

}

我有一個第二個方法(見下文),它不會將輸入作為其簽名的一部分,但會利用上面 take_deposit_amount_into_checking_account() 的返回值。

完成下面的初始化后,

double deposit_amount = take_deposit_amount_into_checking_account();

public double withdrawl_from_checking() throws InputMismatchException {
    System.out.println("Enter the amount you wish to deposit");
    Scanner sc = new Scanner(System.in);
    double withdrawl_amount = sc.nextDouble();
    double depositAmount = deposit_amount;
    double balance_after_withdrawal = 0;
    if (withdrawl_amount > 0.0 && withdrawl_amount >= depositAmount) {
        balance_after_withdrawal = depositAmount - withdrawl_amount;
        System.out.println(" You have made a withdrawal in the amount of " + withdrawl_amount
                + " You balance is now " + balance_after_withdrawal);

    } else if (withdrawl_amount > depositAmount) {
        System.out.println(" Insifficient funds: withdrwal amount is greater than current balance");

    }
    return balance_after_withdrawal;

}

我仍然無法成功地將 deposit_amount 的實際值傳遞到 doublewithdrawl_from_checking() 中

我不知道如何在第二個方法中使用我的第一個方法的返回值。

我不明白你到底想做什么,但以下是我的初步發現

take_deposit_amount_into_checking_account 應該從withdrawl_from_checking 方法的某個地方調用。 所以你應該改變

double depositAmount = deposit_amount;

double depositAmount = take_deposit_amount_into_checking_account();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM