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

Java 如何将参数值从一个方法传递到同一类中的另一个方法,java,methods,parameter-passing,pass-by-reference,Java,Methods,Parameter Passing,Pass By Reference,我正在为ATM模拟机编写这段代码,它具有存款、取款和余额查询功能。代码需要使用方法而不是switch语句编写。 我的存款方式有效,但我对取款和余额查询方式都有问题。我希望所有方法都能访问checkAc_bal,以便执行计算。我对Java是个新手,我正试图对方法和行为了如指掌。非常感谢 将其定义为类成员: import java.util.Scanner; public class Main { static int checkAc_bal = 0; //<----------

我正在为ATM模拟机编写这段代码,它具有存款、取款和余额查询功能。代码需要使用方法而不是switch语句编写。 我的存款方式有效,但我对取款和余额查询方式都有问题。我希望所有方法都能访问checkAc_bal,以便执行计算。我对Java是个新手,我正试图对方法和行为了如指掌。非常感谢


将其定义为类成员:

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

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

您有三个问题,下面是答案:

将变量全局声明为可从所有方法访问。 不要使用开关盒,否则需要使用 如何将参数传递给同一类中的方法? 我看到您已经在代码中这样做了。您通过参数调用了其他方法,并收到了它们。 或者很好地提出你的问题,比如你到底需要什么

下面是完整的代码:

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();
    }
}
理解将在类范围内声明的checkAc_bal,但您对开关盒的要求不清楚。
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