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

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

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

问题

  1. import java.util.Scanner;
  2. public class Main {
  3. static int checkAc_bal = 0;
  4. public static void showMenu() {
  5. Scanner sc = new Scanner(System.in);
  6. String input = null;
  7. do {
  8. input = showOptions(sc);
  9. if (input != null) {
  10. switch (input.trim()) {
  11. case "1":
  12. deposit(sc);
  13. break;
  14. case "2":
  15. withdraw(sc);
  16. break;
  17. case "3":
  18. balanceInquiry();
  19. break;
  20. case "4":
  21. System.exit(0);
  22. default:
  23. System.out.println("Wrong option entered. Exiting application");
  24. System.exit(0);
  25. }
  26. }
  27. } while (true);
  28. }
  29. public static String showOptions(Scanner sc) {
  30. System.out.println("********************Welcome to ATM Machine******************** ");
  31. System.out.println("Enter Option");
  32. System.out.println("1. Deposit");
  33. System.out.println("2. Withdrawal");
  34. System.out.println("3. Balance Inquiry");
  35. System.out.println("4. Exit\n");
  36. String input = sc.nextLine();
  37. return input;
  38. }
  39. public static void deposit(Scanner sc) {
  40. System.out.print("Enter amount to be deposited:");
  41. int deposit;
  42. Scanner s = new Scanner(System.in);
  43. deposit = s.nextInt();
  44. checkAc_bal = checkAc_bal + deposit;
  45. System.out.println("Your Money has been successfully deposited");
  46. System.out.println("Balance: " + checkAc_bal);
  47. }
  48. public static void withdraw(Scanner sc) {
  49. System.out.print("Enter money to be withdrawn:");
  50. int withdraw;
  51. Scanner s = new Scanner(System.in);
  52. withdraw = s.nextInt();
  53. if (withdraw <= checkAc_bal) {
  54. checkAc_bal = checkAc_bal - withdraw;
  55. System.out.println("Please collect your money");
  56. } else {
  57. System.out.println("Insufficient Balance");
  58. }
  59. System.out.println("");
  60. }
  61. public static void balanceInquiry() {
  62. System.out.println("Balance: " + checkAc_bal);
  63. }
  64. public static void main(String[] args) {
  65. showMenu();
  66. }
  67. }
英文:

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.

...

  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. public static void showMenu()
  5. {
  6. Scanner sc = new Scanner(System.in) ;
  7. String input = null ;
  8. do
  9. {
  10. input = showOptions(sc) ;
  11. if(input != null)
  12. {
  13. switch(input.trim())
  14. {
  15. case &quot;1&quot; : deposit(sc) ;
  16. break ;
  17. case &quot;2&quot; : withdraw(sc);
  18. break ;
  19. case &quot;3&quot; : balanceInquiry() ;
  20. break ;enter code here
  21. case &quot;4&quot; : System.exit(0);
  22. default : System.out.println(&quot;Wrong option entered. Exiting application&quot;) ;
  23. System.exit(0);
  24. }
  25. }
  26. }while(true) ;
  27. }
  28. public static String showOptions(Scanner sc)
  29. {
  30. System.out.println(&quot;********************Welcome to ATM Machine******************** &quot;);
  31. System.out.println(&quot;Enter Option&quot;);
  32. System.out.println(&quot;1. Deposit&quot;);
  33. System.out.println(&quot;2. Withdrawal&quot;);
  34. System.out.println(&quot;3. Balance Inquiry&quot;);
  35. System.out.println(&quot;4. Exit\n&quot;);
  36. String input = sc.nextLine() ;
  37. return input ;
  38. }
  39. public static void deposit (Scanner sc) {
  40. int checkAc_bal = 0;
  41. System.out.print(&quot;Enter amount to be deposited:&quot;);
  42. int deposit;
  43. Scanner s = new Scanner(System.in);
  44. deposit = s.nextInt();
  45. //int checkAc_bal = 0;
  46. checkAc_bal = checkAc_bal + deposit;
  47. System.out.println(&quot;Your Money has been successfully deposited&quot;);
  48. System.out.println(&quot;Balance: &quot; +checkAc_bal);
  49. }
  50. public static void withdraw (Scanner sc){
  51. System.out.print(&quot;Enter money to be withdrawn:&quot;);
  52. int withdraw;
  53. Scanner s = new Scanner(System.in);
  54. withdraw = s.nextInt();
  55. if(withdraw&lt;=checkAc_bal)
  56. {
  57. checkAc_bal = checkAc_bal - withdraw;
  58. System.out.println(&quot;Please collect your money&quot;);
  59. }
  60. else
  61. {
  62. System.out.println(&quot;Insufficient Balance&quot;);
  63. }
  64. System.out.println(&quot;&quot;);
  65. }
  66. public static void balanceInquiry () {
  67. System.out.println(&quot;Balance: &quot; + checkAc_bal);
  68. }
  69. public static void main(String[] args) {
  70. showMenu();
  71. }
  72. }

答案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

  1. import java.util.Scanner;
  2. public class Main {
  3. static int checkAc_bal = 0; //&lt;------------------------add this
  4. public static void showMenu() {
  5. Scanner sc = new Scanner(System.in);
  6. String input = null;
  7. do {
  8. input = showOptions(sc);
  9. if (input != null) {
  10. switch (input.trim()) {
  11. case "1":
  12. deposit(sc);
  13. break;
  14. case "2":
  15. withdraw(sc);
  16. break;
  17. case "3":
  18. balanceInquiry();
  19. break;
  20. case "4":
  21. System.exit(0);
  22. default:
  23. System.out.println("Wrong option entered. Exiting application");
  24. System.exit(0);
  25. }
  26. }
  27. } while (true);
  28. }
  29. public static String showOptions(Scanner sc) {
  30. System.out.println("********************Welcome to ATM Machine******************** ");
  31. System.out.println("Enter Option");
  32. System.out.println("1. Deposit");
  33. System.out.println("2. Withdrawal");
  34. System.out.println("3. Balance Inquiry");
  35. System.out.println("4. Exit\n");
  36. String input = sc.nextLine();
  37. return input;
  38. }
  39. public static void deposit(Scanner sc) {
  40. System.out.print("Enter amount to be deposited:");
  41. int deposit;
  42. Scanner s = new Scanner(System.in);
  43. deposit = s.nextInt();
  44. checkAc_bal = checkAc_bal + deposit;
  45. System.out.println("Your Money has been successfully deposited");
  46. System.out.println("Balance: " + checkAc_bal);
  47. }
  48. public static void withdraw(Scanner sc) {
  49. System.out.print("Enter money to be withdrawn:");
  50. int withdraw;
  51. Scanner s = new Scanner(System.in);
  52. withdraw = s.nextInt();
  53. if (withdraw <= checkAc_bal) {
  54. checkAc_bal = checkAc_bal - withdraw;
  55. System.out.println("Please collect your money");
  56. } else {
  57. System.out.println("Insufficient Balance");
  58. }
  59. System.out.println("");
  60. }
  61. public static void balanceInquiry() {
  62. System.out.println("Balance: " + checkAc_bal);
  63. }
  64. public static void main(String[] args) {
  65. showMenu();
  66. }
  67. }
英文:

define it as a class member:

  1. import java.util.Scanner;
  2. public class Main
  3. {
  4. static int checkAc_bal = 0; //&lt;------------------------add this
  5. public static void showMenu()
  6. {
  7. Scanner sc = new Scanner(System.in) ;
  8. String input = null ;
  9. do
  10. {
  11. input = showOptions(sc) ;
  12. if(input != null)
  13. {
  14. switch(input.trim())
  15. {
  16. case &quot;1&quot; : deposit(sc) ;
  17. break ;
  18. case &quot;2&quot; : withdraw(sc);
  19. break ;
  20. case &quot;3&quot; : balanceInquiry() ;
  21. break ;enter code here
  22. case &quot;4&quot; : System.exit(0);
  23. default : System.out.println(&quot;Wrong option entered. Exiting application&quot;) ;
  24. System.exit(0);
  25. }
  26. }
  27. }while(true) ;
  28. }
  29. public static String showOptions(Scanner sc)
  30. {
  31. System.out.println(&quot;********************Welcome to ATM Machine******************** &quot;);
  32. System.out.println(&quot;Enter Option&quot;);
  33. System.out.println(&quot;1. Deposit&quot;);
  34. System.out.println(&quot;2. Withdrawal&quot;);
  35. System.out.println(&quot;3. Balance Inquiry&quot;);
  36. System.out.println(&quot;4. Exit\n&quot;);
  37. String input = sc.nextLine() ;
  38. return input ;
  39. }
  40. public static void deposit (Scanner sc) {
  41. // int checkAc_bal = 0; &lt;---------------- remove this
  42. System.out.print(&quot;Enter amount to be deposited:&quot;);
  43. int deposit;
  44. Scanner s = new Scanner(System.in);
  45. deposit = s.nextInt();
  46. //int checkAc_bal = 0;
  47. checkAc_bal = checkAc_bal + deposit;
  48. System.out.println(&quot;Your Money has been successfully deposited&quot;);
  49. System.out.println(&quot;Balance: &quot; +checkAc_bal);
  50. }
  51. public static void withdraw (Scanner sc){
  52. System.out.print(&quot;Enter money to be withdrawn:&quot;);
  53. int withdraw;
  54. Scanner s = new Scanner(System.in);
  55. withdraw = s.nextInt();
  56. if(withdraw&lt;=checkAc_bal)
  57. {
  58. checkAc_bal = checkAc_bal - withdraw;
  59. System.out.println(&quot;Please collect your money&quot;);
  60. }
  61. else
  62. {
  63. System.out.println(&quot;Insufficient Balance&quot;);
  64. }
  65. System.out.println(&quot;&quot;);
  66. }
  67. public static void balanceInquiry () {
  68. System.out.println(&quot;Balance: &quot; + checkAc_bal);
  69. }
  70. public static void main(String[] args) {
  71. showMenu();
  72. }
  73. }

答案3

得分: 0

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

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

这是完整的代码:

  1. import java.util.Scanner;
  2. public class Main{
  3. static int checkAc_bal = 0; // <---- 在类范围内声明
  4. public static void showMenu() {
  5. Scanner sc = new Scanner(System.in) ;
  6. String input = null ;
  7. do {
  8. input = showOptions(sc) ;
  9. if(input != null) {
  10. // 移除了使用的switch-case和if-else
  11. if(input.trim().equals("1")) {deposit();}
  12. else if(input.trim().equals("2")) {withdraw();}
  13. else if(input.trim().equals("3")) {balanceInquiry();}
  14. else if(input.trim().equals("4")) {System.exit(0);}
  15. else {
  16. System.out.println("输入错误。正在退出应用程序") ;
  17. System.exit(0);
  18. }
  19. }
  20. }while(true) ;
  21. }
  22. public static String showOptions(Scanner sc){
  23. System.out.println("********************欢迎使用ATM机******************** ");
  24. System.out.println("输入选项");
  25. System.out.println("1. 存款");
  26. System.out.println("2. 取款");
  27. System.out.println("3. 查询余额");
  28. System.out.println("4. 退出\n");
  29. String input = sc.nextLine() ;
  30. return input ;
  31. }
  32. public static void deposit () {
  33. System.out.print("输入要存款的金额:");
  34. int deposit;
  35. Scanner s = new Scanner(System.in);
  36. deposit = s.nextInt();
  37. checkAc_bal = checkAc_bal + deposit;
  38. System.out.println("您的钱已成功存入");
  39. System.out.println("余额:" + checkAc_bal);
  40. }
  41. public static void withdraw (){
  42. System.out.print("输入要取款的金额:");
  43. int withdraw;
  44. Scanner s = new Scanner(System.in);
  45. withdraw = s.nextInt();
  46. if(withdraw <= checkAc_bal) {
  47. checkAc_bal = checkAc_bal - withdraw;
  48. System.out.println("请取走您的钱");
  49. } else {
  50. System.out.println("余额不足");
  51. }
  52. System.out.println("");
  53. }
  54. public static void balanceInquiry () {
  55. System.out.println("余额:" + checkAc_bal);
  56. }
  57. public static void main(String[] args) {
  58. showMenu();
  59. }
  60. }

输出:

  1. ********************欢迎使用ATM机********************
  2. 输入选项
  3. 1. 存款
  4. 2. 取款
  5. 3. 查询余额
  6. 4. 退出
  7. 1
  8. 输入: 1
  9. 输入要存款的金额:100
  10. 您的钱已成功存入
  11. 余额:100
  12. ********************欢迎使用ATM机********************
  13. 输入选项
  14. 1. 存款
  15. 2. 取款
  16. 3. 查询余额
  17. 4. 退出
  18. 1
  19. 输入: 1
  20. 输入要存款的金额:200
  21. 您的钱已成功存入
  22. 余额:300
  23. ********************欢迎使用ATM机********************
  24. 输入选项
  25. 1. 存款
  26. 2. 取款
  27. 3. 查询余额
  28. 4. 退出
  29. 1
  30. 输入: 1
  31. 输入要存款的金额:200
  32. 您的钱已成功存入
  33. 余额:500
  34. ********************欢迎使用ATM机********************
  35. 输入选项
  36. 1. 存款
  37. 2. 取款
  38. 3. 查询余额
  39. 4. 退出
  40. 3
  41. 输入: 3
  42. 余额:500
  43. ********************欢迎使用ATM机********************
  44. 输入选项
  45. 1. 存款
  46. 2. 取款
  47. 3. 查询余额
  48. 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 :

  1. import java.util.Scanner;
  2. public class Main{
  3. static int checkAc_bal = 0; // &lt;---- declare on class scope
  4. public static void showMenu() {
  5. Scanner sc = new Scanner(System.in) ;
  6. String input = null ;
  7. do {
  8. input = showOptions(sc) ;
  9. if(input != null) {
  10. // removed the switch-case and if-else used
  11. if(input.trim().equals(&quot;1&quot;)) {deposit();}
  12. else if(input.trim().equals(&quot;2&quot;)) {withdraw();}
  13. else if(input.trim().equals(&quot;3&quot;)) {balanceInquiry();}
  14. else if(input.trim().equals(&quot;4&quot;)) {System.exit(0);}
  15. else {
  16. System.out.println(&quot;Wrong option entered. Exiting application&quot;) ;
  17. System.exit(0);
  18. }
  19. }
  20. }while(true) ;
  21. }
  22. public static String showOptions(Scanner sc){
  23. System.out.println(&quot;********************Welcome to ATM Machine******************** &quot;);
  24. System.out.println(&quot;Enter Option&quot;);
  25. System.out.println(&quot;1. Deposit&quot;);
  26. System.out.println(&quot;2. Withdrawal&quot;);
  27. System.out.println(&quot;3. Balance Inquiry&quot;);
  28. System.out.println(&quot;4. Exit\n&quot;);
  29. String input = sc.nextLine() ;
  30. return input ;
  31. }
  32. public static void deposit () {
  33. System.out.print(&quot;Enter amount to be deposited:&quot;);
  34. int deposit;
  35. Scanner s = new Scanner(System.in);
  36. deposit = s.nextInt();
  37. checkAc_bal = checkAc_bal + deposit;
  38. System.out.println(&quot;Your Money has been successfully deposited&quot;);
  39. System.out.println(&quot;Balance: &quot; +checkAc_bal);
  40. }
  41. public static void withdraw (){
  42. System.out.print(&quot;Enter money to be withdrawn:&quot;);
  43. int withdraw;
  44. Scanner s = new Scanner(System.in);
  45. withdraw = s.nextInt();
  46. if(withdraw&lt;=checkAc_bal) {
  47. checkAc_bal = checkAc_bal - withdraw;
  48. System.out.println(&quot;Please collect your money&quot;);
  49. } else {
  50. System.out.println(&quot;Insufficient Balance&quot;);
  51. }
  52. System.out.println(&quot;&quot;);
  53. }
  54. public static void balanceInquiry () {
  55. System.out.println(&quot;Balance: &quot; + checkAc_bal);
  56. }
  57. public static void main(String[] args) {
  58. showMenu();
  59. }
  60. }

Output :

  1. ********************Welcome to ATM Machine********************
  2. Enter Option
  3. 1. Deposit
  4. 2. Withdrawal
  5. 3. Balance Inquiry
  6. 4. Exit
  7. 1
  8. Input : 1
  9. Enter amount to be deposited:100
  10. Your Money has been successfully deposited
  11. Balance: 100
  12. ********************Welcome to ATM Machine********************
  13. Enter Option
  14. 1. Deposit
  15. 2. Withdrawal
  16. 3. Balance Inquiry
  17. 4. Exit
  18. 1
  19. Input : 1
  20. Enter amount to be deposited:200
  21. Your Money has been successfully deposited
  22. Balance: 300
  23. ********************Welcome to ATM Machine********************
  24. Enter Option
  25. 1. Deposit
  26. 2. Withdrawal
  27. 3. Balance Inquiry
  28. 4. Exit
  29. 1
  30. Input : 1
  31. Enter amount to be deposited:200
  32. Your Money has been successfully deposited
  33. Balance: 500
  34. ********************Welcome to ATM Machine********************
  35. Enter Option
  36. 1. Deposit
  37. 2. Withdrawal
  38. 3. Balance Inquiry
  39. 4. Exit
  40. 3
  41. Input : 3
  42. Balance: 500
  43. ********************Welcome to ATM Machine********************
  44. Enter Option
  45. 1. Deposit
  46. 2. Withdrawal
  47. 3. Balance Inquiry
  48. 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:

确定