英文:
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 "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();
}
}
答案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; //<------------------------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; //<------------------------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();
}
}
答案3
得分: 0
你有三个查询,以下是答案:
- 在全局范围内声明变量,以便从所有方法中访问。
- 不要使用switch语句(在这种情况下应使用if-else)。
- 如何将参数传递给同一类中的方法?
我看到你在代码中已经做到了这一点。你通过参数调用了其他方法,并成功接收了它们。
这是完整的代码:
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 :
- Declare the variable globally to be accessible from all the methods.
- Not to use switch case (U need to use if-else in that case)
- 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; // <---- 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();
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论