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

axzmvihb  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(474)

我正在为一个atm模拟器写这段代码,具有存款、取款和余额查询功能。代码需要用方法而不是switch语句来编写。我的存款方式行得通,但取款和余额查询方式都有问题。我希望所有方法都能访问checkacu\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();
    }
}
gkl3eglg

gkl3eglg1#

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

3j86kqsm

3j86kqsm2#

将其定义为类成员:

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

qlvxas9a3#

您有三个问题,以下是答案:
全局声明变量可从所有方法访问。
不使用switch case(在这种情况下需要使用if else)
如何将参数传递给同一类中的方法?我看你已经在你的代码里写过了。您通过参数调用其他方法并接收它们。
或者把你的问题框好,你到底需要什么。
下面是完整的代码:

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

相关问题