【问题标题】:Return to method & passing variables between methods?返回方法并在方法之间传递变量?
【发布时间】:2014-07-13 19:51:44
【问题描述】:

我目前正在执行一项关于我必须编程的 ATM 机的任务。总共有 4 种方法 - 主要、提款、存款和支票余额。在 MAIN 方法中,我有一个菜单,因此将从那里将变量发送到其余方法。

我想知道是否有一种方法,如果在我完成 WITHDRAWAL 中的总数计算后,我可以将该值传递给 CHECKBALANCE?因为我试过了:

total = deposit - withdrawal;
checkbalance(withdrawal);

为了让用户检查他们的余额,他们必须将在他们之前的选择中计算的值发送到 CHECKBALANCE 方法,对吗?但弹出的错误是“参数列表长度不同”。另外,我想知道在完成其他方法中执行的每项任务后如何返回到 MAIN 中的菜单。我试过了:

choice = Integer.parseInt(JOptionPane.showInputDialog("Would you like to return to the    menu? (1 = Yes, 2 = No));

if (choice == 2)
{
System.exit(0);
}

main();

如果你们中的任何人能向我伸出援助之手,我将不胜感激。我还是新手,这个学期刚开始学习java。请并感谢大家:)

更新:

package lab2;

import javax.swing.JOptionPane;
public class LAB2 
{

    public static void main(String[] args) 
    {
        double initialAmount, withdraw = 0, deposit = 0, checkBalance = 0;
        int choice;

        initialAmount = Double.parseDouble(JOptionPane.showInputDialog("Please enter current savings amount: "));

        while(initialAmount < 0)
        {
            initialAmount = Double.parseDouble(JOptionPane.showInputDialog("Invalid amount. Amount cannot be negative. Please re-enter" ));   
        }

        choice = Integer.parseInt(JOptionPane.showInputDialog("MENU: "
                + "What would you like to do? Please choose from 1 - 4\n"
                + "1) Withdraw\n"
                + "2) Deposit\n"
                + "3) Check balance\n"
                + "4) Exit"));
        while ((choice <= 0) || (choice > 4))
        {
            choice = Integer.parseInt(JOptionPane.showInputDialog("Invalid option. Please choose between option 1 - 4: "));
        }

        if (choice == 1)
        {
            ChoiceOne(withdraw, initialAmount);
        }

        else if (choice == 2)
        {
            ChoiceTwo(deposit, initialAmount);
        }
        else if (choice == 3)
        {
            ChoiceThree(checkBalance, initialAmount);
        }

        else if (choice == 4)
        {
            System.exit(0);
        }
    }

    /////////////////////////////////////////////////////////////////////////////
    public static void ChoiceOne(double withdraw, double initialAmount)
    {
        double afterDeduction;
        int input, choice1;

        withdraw = Double.parseDouble(JOptionPane.showInputDialog("Your current balance is: RM"+initialAmount+" . How much would you like to withdraw? "));

        while (withdraw > initialAmount)
        {
            input = Integer.parseInt(JOptionPane.showInputDialog("Insufficient balance to perform withdrawal. Continue? (1 = Yes, 2 = Exit ")); 

            if (input == 2)
            {
                JOptionPane.showMessageDialog(null,"Thank you!");
                System.exit(0);
            }  
            else if (input == 1)
            {
                withdraw = Double.parseDouble(JOptionPane.showInputDialog("Your current balance is: RM"+initialAmount+" . How much would you like to withdraw? "));

                afterDeduction = initialAmount - withdraw;
                JOptionPane.showMessageDialog(null, "You have withdrawn RM"+withdraw+". Your total balanace now is: RM"+afterDeduction+".");

                choice1 = Integer.parseInt(JOptionPane.showInputDialog("Would you like to continue to menu or exit? (1 = Yes /2 = No"));

                if (choice1 == 2)
                {
                    JOptionPane.showMessageDialog(null,"Thank you!");
                    System.exit(0);
                }

                if (choice1 == 1)
                {

                }
            }
        }

        while  (withdraw < 0)
        {
            input = Integer.parseInt(JOptionPane.showInputDialog("Invalid withdrawal. Please re-enter: "));
        }

        afterDeduction = initialAmount - withdraw;
        JOptionPane.showMessageDialog(null,"You have withdrawn RM"+withdraw+". Your total balanace now is: RM"+afterDeduction+"."
                + "/nWould you like to return to the menu? ");
    }

    /////////////////////////////////////////////////////////////////////////////
    public static void ChoiceTwo (double deposit, double initialAmount)
    {
        double afterAddition; 
        int input;

        deposit = Double.parseDouble(JOptionPane.showInputDialog("How much would you like to deposit? "));

        while (deposit < 0)
        {
            input = Integer.parseInt(JOptionPane.showInputDialog("Invalid deposit. Please re-enter: "));
        }

        if (deposit > 0)
        {
        afterAddition = deposit + initialAmount;
        JOptionPane.showMessageDialog(null,"You have deposit RM"+deposit+". Your total balanace now is: RM"+afterAddition+"."
                + "/nWould you like to return to the menu? ");
        }
    }

    /////////////////////////////////////////////////////////////////////////////
    public static void ChoiceThree (double CheckBalance, double initialAmount)
    {
    }

}

【问题讨论】:

  • 在此处发布您的支票余额和提款方式
  • 您可以使用是/否视觉提示代替输入数字:stackoverflow.com/questions/8396870/…
  • @Niemand 我已经用完整的编码更新了它~
  • @kajacx 感谢您的建议!我会用那个:)

标签: java methods return


【解决方案1】:

您可以创建新对象,我们称之为 SessionData 并将所有操作数据存储在其中。它可以显着简化参数传递问题。根据您提供的信息,我只能提供这些建议。

【讨论】:

    【解决方案2】:

    我不完全理解您仅查看一小部分代码所做的工作,但是,您可以尝试使用布尔值来检查用户是否希望在布尔值等于时返回主菜单true 然后显示主菜单,如果它等于 false 然后程序开始/结束。

    【讨论】:

    • 我已经用完整的代码更新了这个问题,以便您更好地理解。非常感谢:)
    猜你喜欢
    • 2013-10-30
    • 2018-08-08
    • 1970-01-01
    • 2012-03-20
    • 2021-01-23
    • 2014-05-11
    相关资源
    最近更新 更多