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

huangapple go评论61阅读模式
英文:

How to pass parameters values from one method to another one in the same class

问题

import java.util.Scanner;

public class Main {
    static int checkAc_bal = 0;

    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;
                    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) {
        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(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();
    }
}
英文:

I'm writing this code for an ATM simulator, with deposit, withdrawal and balance inquiry functionality. The code needs to be written with methods and not switch statements.
My deposit method works, but I have issues with both the withdraw and balanceInquiry methods. I would like to have checkAc_bal accessible to all methods, in order to perform calculations. I'm new on Java, and I'm trying to wrap my head on methods behaviors. Thanks so much.

...

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 &quot;1&quot; :    deposit(sc) ;
break ;
case &quot;2&quot; :    withdraw(sc);
break ;
case &quot;3&quot; :    balanceInquiry() ;
break ;enter code here
case &quot;4&quot; :    System.exit(0);
default  :  System.out.println(&quot;Wrong option entered. Exiting application&quot;) ;
System.exit(0);
}
}
}while(true) ;
}
public static String showOptions(Scanner sc)
{
System.out.println(&quot;********************Welcome to ATM Machine******************** &quot;);
System.out.println(&quot;Enter Option&quot;);
System.out.println(&quot;1. Deposit&quot;);
System.out.println(&quot;2. Withdrawal&quot;);
System.out.println(&quot;3. Balance Inquiry&quot;);
System.out.println(&quot;4. Exit\n&quot;);
String input = sc.nextLine() ;
return input ;
}
public static void deposit (Scanner sc) {
int checkAc_bal = 0;
System.out.print(&quot;Enter amount to be deposited:&quot;);
int deposit;
Scanner s = new Scanner(System.in);
deposit = s.nextInt();
//int checkAc_bal = 0;
checkAc_bal = checkAc_bal + deposit;
System.out.println(&quot;Your Money has been successfully deposited&quot;);
System.out.println(&quot;Balance: &quot; +checkAc_bal);
}
public static void withdraw (Scanner sc){
System.out.print(&quot;Enter money to be withdrawn:&quot;);
int withdraw;
Scanner s = new Scanner(System.in);
withdraw = s.nextInt();
if(withdraw&lt;=checkAc_bal)
{
checkAc_bal = checkAc_bal - withdraw;
System.out.println(&quot;Please collect your money&quot;);
}
else
{
System.out.println(&quot;Insufficient Balance&quot;);
}
System.out.println(&quot;&quot;);
}
public static void balanceInquiry () {
System.out.println(&quot;Balance: &quot; + checkAc_bal);
}
public static void main(String[] args) {
showMenu();
}
}

答案1

得分: 1

如果您希望其他方法能够访问您的整数变量,您需要将其声明在整个类的范围内,而不是方法内部。尝试在Main类中声明checkAc_bal。

英文:

If you want your int to be accessible to other methods, you need to declare it in the scope of the whole class and not inside a method. Try declaring checkAc_bal in the Main class.

答案2

得分: 0

import java.util.Scanner;

public class Main {
    static int checkAc_bal = 0;   //&lt;------------------------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;
                    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) {
        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(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();
    }
}
英文:

define it as a class member:

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

答案3

得分: 0

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

  1. 在全局范围内声明变量,以便从所有方法中访问。
  2. 不要使用switch语句(在这种情况下应使用if-else)。
  3. 如何将参数传递给同一类中的方法?
    我看到你在代码中已经做到了这一点。你通过参数调用了其他方法,并成功接收了它们。

这是完整的代码:

import java.util.Scanner;

public class Main{

    static int checkAc_bal = 0;  // <---- 在类范围内声明

    public static void showMenu()    {
        Scanner sc = new Scanner(System.in) ;
        String input = null ;
        do {
            input = showOptions(sc) ;
            if(input != null) {
                // 移除了使用的switch-case和if-else
                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("输入错误。正在退出应用程序") ;
                    System.exit(0);
                }
            }
        }while(true) ;
    }

    public static String showOptions(Scanner sc){

        System.out.println("********************欢迎使用ATM机******************** ");
        System.out.println("输入选项");
        System.out.println("1. 存款");
        System.out.println("2. 取款");
        System.out.println("3. 查询余额");
        System.out.println("4. 退出\n");
        String input = sc.nextLine() ;
        return input ;
    }

    public static void deposit () {
        
        System.out.print("输入要存款的金额:");
        int deposit;
        Scanner s = new Scanner(System.in);
        deposit = s.nextInt();
        checkAc_bal = checkAc_bal + deposit;

        System.out.println("您的钱已成功存入");
        System.out.println("余额:" + checkAc_bal);
    }

    public static void withdraw (){
        System.out.print("输入要取款的金额:");
        int withdraw;
        Scanner s = new Scanner(System.in);
        withdraw = s.nextInt();
        if(withdraw <= checkAc_bal) {
            checkAc_bal = checkAc_bal - withdraw;
            System.out.println("请取走您的钱");
        } else {
            System.out.println("余额不足");
        }
        System.out.println("");
    }

    public static void balanceInquiry () {
        System.out.println("余额:" + checkAc_bal);
    }

    public static void main(String[] args) {
        showMenu();
    }
}

输出:

********************欢迎使用ATM机******************** 
输入选项
1. 存款
2. 取款
3. 查询余额
4. 退出
1
输入: 1
输入要存款的金额:100
您的钱已成功存入
余额:100
********************欢迎使用ATM机******************** 
输入选项
1. 存款
2. 取款
3. 查询余额
4. 退出
1
输入: 1
输入要存款的金额:200
您的钱已成功存入
余额:300
********************欢迎使用ATM机******************** 
输入选项
1. 存款
2. 取款
3. 查询余额
4. 退出
1
输入: 1
输入要存款的金额:200
您的钱已成功存入
余额:500
********************欢迎使用ATM机******************** 
输入选项
1. 存款
2. 取款
3. 查询余额
4. 退出
3
输入: 3
余额:500
********************欢迎使用ATM机******************** 
输入选项
1. 存款
2. 取款
3. 查询余额
4. 退出
英文:

You have three queries, here are the answers :

  1. Declare the variable globally to be accessible from all the methods.
  2. Not to use switch case (U need to use if-else in that case)
  3. How to pass parameters to methods in same class ?
    I see you already did this in your code. You called other methods by parameters and received them as well.

Or frame your question well , as what exactly you need.

So here is the complete code :

import java.util.Scanner;
public class Main{
static int checkAc_bal = 0;  // &lt;---- 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(&quot;1&quot;))      {deposit();} 
else if(input.trim().equals(&quot;2&quot;)) {withdraw();}
else if(input.trim().equals(&quot;3&quot;)) {balanceInquiry();}
else if(input.trim().equals(&quot;4&quot;)) {System.exit(0);}
else {
System.out.println(&quot;Wrong option entered. Exiting application&quot;) ;
System.exit(0);
}
}
}while(true) ;
}
public static String showOptions(Scanner sc){
System.out.println(&quot;********************Welcome to ATM Machine******************** &quot;);
System.out.println(&quot;Enter Option&quot;);
System.out.println(&quot;1. Deposit&quot;);
System.out.println(&quot;2. Withdrawal&quot;);
System.out.println(&quot;3. Balance Inquiry&quot;);
System.out.println(&quot;4. Exit\n&quot;);
String input = sc.nextLine() ;
return input ;
}
public static void deposit () {
System.out.print(&quot;Enter amount to be deposited:&quot;);
int deposit;
Scanner s = new Scanner(System.in);
deposit = s.nextInt();
checkAc_bal = checkAc_bal + deposit;
System.out.println(&quot;Your Money has been successfully deposited&quot;);
System.out.println(&quot;Balance: &quot; +checkAc_bal);
}
public static void withdraw (){
System.out.print(&quot;Enter money to be withdrawn:&quot;);
int withdraw;
Scanner s = new Scanner(System.in);
withdraw = s.nextInt();
if(withdraw&lt;=checkAc_bal) {
checkAc_bal = checkAc_bal - withdraw;
System.out.println(&quot;Please collect your money&quot;);
} else {
System.out.println(&quot;Insufficient Balance&quot;);
}
System.out.println(&quot;&quot;);
}
public static void balanceInquiry () {
System.out.println(&quot;Balance: &quot; + checkAc_bal);
}
public static void main(String[] args) {
showMenu();
}
}

Output :

********************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

huangapple
  • 本文由 发表于 2020年8月18日 21:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63469866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定