【问题标题】:How to pass parameters values from one method to another one in the same class如何将参数值从一种方法传递给同一类中的另一种方法
【发布时间】:2020-12-07 17:05:24
【问题描述】:

我正在为 ATM 模拟器编写此代码,具有存款、取款和余额查询功能。代码需要用方法而不是 switch 语句来编写。 我的存款方法有效,但提款和 balanceInquiry 方法都存在问题。我希望所有方法都可以访问 checkAc_bal,以便执行计算。我是 Java 新手,我正试图把头放在方法行为上。非常感谢。

...

import java.util.Scanner;
public class Main
{
    public static void showMenu()
    {
        Scanner sc = new Scanner(System.in) ;
        String input = null ;
        do
        {
            input = showOptions(sc) ;
            if(input != null)
            {
                switch(input.trim())
                {
                    case "1" :    deposit(sc) ;
                        break ;
                    case "2" :    withdraw(sc);
                        break ;
                    case "3" :    balanceInquiry() ;
                        break ;enter code here
                    case "4" :    System.exit(0);
                    default  :  System.out.println("Wrong option entered. Exiting application") ;
                        System.exit(0);
                }
            }
        }while(true) ;
    }

    public static String showOptions(Scanner sc)
    {

        System.out.println("********************Welcome to ATM Machine******************** ");
        System.out.println("Enter Option");
        System.out.println("1. Deposit");
        System.out.println("2. Withdrawal");
        System.out.println("3. Balance Inquiry");
        System.out.println("4. Exit\n");
        String input = sc.nextLine() ;
        return input ;
    }
    
     public static void deposit (Scanner sc) {
        int checkAc_bal = 0;
        System.out.print("Enter amount to be deposited:");
        int deposit;
        Scanner s = new Scanner(System.in);
        deposit = s.nextInt();
        //int checkAc_bal = 0;

        checkAc_bal = checkAc_bal + deposit;

        System.out.println("Your Money has been successfully deposited");
        System.out.println("Balance: " +checkAc_bal);
    }

    public static void withdraw (Scanner sc){
        System.out.print("Enter money to be withdrawn:");
        int withdraw;
        Scanner s = new Scanner(System.in);
        withdraw = s.nextInt();
        if(withdraw<=checkAc_bal)
        {
            checkAc_bal = checkAc_bal - withdraw;
            System.out.println("Please collect your money");
        }
        else
        {
            System.out.println("Insufficient Balance");
        }
        System.out.println("");
    }

    public static void balanceInquiry () {
        System.out.println("Balance: " + checkAc_bal);
    }
    
    public static void main(String[] args) {
        showMenu();
    }
}

【问题讨论】:

  • 了解checkAc_bal 将在类范围内声明,但您对 switch-case 的要求尚不清楚。

标签: java methods parameter-passing pass-by-reference


【解决方案1】:

您有三个疑问,以下是答案:

  1. 将变量全局声明为可从所有方法访问。
  2. 不要使用switch case(在这种情况下你需要使用if-else)
  3. 如何将参数传递给同一类中的方法? 我看到你已经在你的代码中这样做了。您通过参数调用其他方法并接收它们。

或者根据你的需要很好地提出你的问题。

所以这里是完整的代码:

import java.util.Scanner;

public class Main{

    static int checkAc_bal = 0;  // <---- declare on class scope

    public static void showMenu()    {
        Scanner sc = new Scanner(System.in) ;
        String input = null ;
        do {
            input = showOptions(sc) ;
            if(input != null) {
                // removed the switch-case and if-else used
                if(input.trim().equals("1"))      {deposit();} 
                else if(input.trim().equals("2")) {withdraw();}
                else if(input.trim().equals("3")) {balanceInquiry();}
                else if(input.trim().equals("4")) {System.exit(0);}
                else {
                    System.out.println("Wrong option entered. Exiting application") ;
                    System.exit(0);
                }
            }
        }while(true) ;
    }

    public static String showOptions(Scanner sc){

        System.out.println("********************Welcome to ATM Machine******************** ");
        System.out.println("Enter Option");
        System.out.println("1. Deposit");
        System.out.println("2. Withdrawal");
        System.out.println("3. Balance Inquiry");
        System.out.println("4. Exit\n");
        String input = sc.nextLine() ;
        return input ;
    }
    
     public static void deposit () {
        
        System.out.print("Enter amount to be deposited:");
        int deposit;
        Scanner s = new Scanner(System.in);
        deposit = s.nextInt();
        checkAc_bal = checkAc_bal + deposit;

        System.out.println("Your Money has been successfully deposited");
        System.out.println("Balance: " +checkAc_bal);
    }

    public static void withdraw (){
        System.out.print("Enter money to be withdrawn:");
        int withdraw;
        Scanner s = new Scanner(System.in);
        withdraw = s.nextInt();
        if(withdraw<=checkAc_bal) {
            checkAc_bal = checkAc_bal - withdraw;
            System.out.println("Please collect your money");
        } else {
            System.out.println("Insufficient Balance");
        }
        System.out.println("");
    }

    public static void balanceInquiry () {
        System.out.println("Balance: " + checkAc_bal);
    }
    
    public static void main(String[] args) {
        showMenu();
    }
}

输出:

********************Welcome to ATM Machine******************** 
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit

1
Input : 1
Enter amount to be deposited:100
Your Money has been successfully deposited
Balance: 100
********************Welcome to ATM Machine******************** 
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit

1
Input : 1
Enter amount to be deposited:200
Your Money has been successfully deposited
Balance: 300
********************Welcome to ATM Machine******************** 
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit

1
Input : 1
Enter amount to be deposited:200
Your Money has been successfully deposited
Balance: 500
********************Welcome to ATM Machine******************** 
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit

3
Input : 3
Balance: 500
********************Welcome to ATM Machine******************** 
Enter Option
1. Deposit
2. Withdrawal
3. Balance Inquiry
4. Exit

【讨论】:

    【解决方案2】:

    如果您希望其他方法可以访问您的 int,则需要在整个类的范围内而不是在方法内声明它。尝试在 Main 类中声明 checkAc_bal。

    【讨论】:

      【解决方案3】:

      将其定义为类成员:

      import java.util.Scanner;
      public class Main
      {
          static int checkAc_bal = 0;   //<------------------------add this
      
          public static void showMenu()
          {
              Scanner sc = new Scanner(System.in) ;
              String input = null ;
              do
              {
                  input = showOptions(sc) ;
                  if(input != null)
                  {
                      switch(input.trim())
                      {
                          case "1" :    deposit(sc) ;
                              break ;
                          case "2" :    withdraw(sc);
                              break ;
                          case "3" :    balanceInquiry() ;
                              break ;enter code here
                          case "4" :    System.exit(0);
                          default  :  System.out.println("Wrong option entered. Exiting application") ;
                              System.exit(0);
                      }
                  }
              }while(true) ;
          }
      
          public static String showOptions(Scanner sc)
          {
      
              System.out.println("********************Welcome to ATM Machine******************** ");
              System.out.println("Enter Option");
              System.out.println("1. Deposit");
              System.out.println("2. Withdrawal");
              System.out.println("3. Balance Inquiry");
              System.out.println("4. Exit\n");
              String input = sc.nextLine() ;
              return input ;
          }
          
           public static void deposit (Scanner sc) {
      //        int checkAc_bal = 0;  <---------------- remove this 
              System.out.print("Enter amount to be deposited:");
              int deposit;
              Scanner s = new Scanner(System.in);
              deposit = s.nextInt();
              //int checkAc_bal = 0;
      
              checkAc_bal = checkAc_bal + deposit;
      
              System.out.println("Your Money has been successfully deposited");
              System.out.println("Balance: " +checkAc_bal);
          }
      
          public static void withdraw (Scanner sc){
              System.out.print("Enter money to be withdrawn:");
              int withdraw;
              Scanner s = new Scanner(System.in);
              withdraw = s.nextInt();
              if(withdraw<=checkAc_bal)
              {
                  checkAc_bal = checkAc_bal - withdraw;
                  System.out.println("Please collect your money");
              }
              else
              {
                  System.out.println("Insufficient Balance");
              }
              System.out.println("");
          }
      
          public static void balanceInquiry () {
              System.out.println("Balance: " + checkAc_bal);
          }
          
          public static void main(String[] args) {
              showMenu();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-29
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        相关资源
        最近更新 更多