如何将参数值从一种方法传递到同一类中的另一种方法

问题描述

我正在为具有存款,提款和余额查询功能的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();
    }
}

解决方法

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

,

将其定义为类成员:

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();
    }
}
,

您有三个查询,以下是答案:

  1. 将变量全局声明为可从所有方法访问。
  2. 不使用切换大小写(在这种情况下,您需要使用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