[英]Return to method & passing variables between methods?
我目前正在做我要編程的ATM機作業。 總共有4種方法-主,提款,存款和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)
{
}
}
您可以創建新對象,我們將其稱為SessionData並將所有操作數據存儲在其中。 它可以大大簡化論證傳遞問題。 根據您提供的信息,我可以提供所有這些建議。
我只看一小段代碼就無法完全理解您的工作,但是,如果布爾值等於true,則可以嘗試使用布爾值檢查用戶是否要返回主菜單,然后顯示如果主菜單等於false,則程序繼續執行/結束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.