返回方法&在方法之间传递变量?

时间:2014-05-24 14:54:14

标签: java methods return

我目前正在做一项关于我必须编程的ATM机的任务。共有4种方法 - MAIN,WITHDRAWAL,DEPOSIT和CHECKBALANCE。从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)
    {
    }

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我不完全理解你只看了一小部分代码,但是,如果布尔值等于如果它等于false则显示主菜单然后程序移动到/结束。

相关问题