OpenPaneLength
我在go()方法中声明了balance变量,现在我试图将该变量的值存储在我的一个if语句中。但是,编译器告诉我它无法识别变量余额。有人知道如何解决这个问题吗?
答案 0 :(得分:1)
只需从go开始提供余额,并将ifStatements()作为参数。
像这样go()
会返回一个整数。
public int go() throws Exception {
Scanner userInput = new Scanner(System.in);
System.out.println("Welcome to online ATM banking\nHow much do you want in your bank account?\nEnter your number");
return userInput.nextInt();
}
就像那样,你可以给你的ifStatements()
一个参数:
public void ifStatement(int balance) throws Exception {
//Creats if statements that change the outcome of the program depending on what the user as inputte dinto thto the program.
//This has been done using int variables whihc have been converted into strings so that they can communicate wiht the userOption variable, the userOption variable's value has been set to whatever the user inputs int the program.
String Withdraw;
String Deposit;
String Inquire;
String Quit;
String usersOption;
int a = 1;
int b = 2;
int c = 3;
int d = 4;
String s = Integer.toString(a);
String ss = Integer.toString(b);
String sss = Integer.toString(c);
String ssss = Integer.toString(d);
//Declares variables
Scanner userInput = new Scanner(System.in); // Allows user input.
System.out.println("What do you want to do?\n1.Withdraw\n2.Deposit\n3.Inquire\n4.Quit\nEnter your number");
usersOption = userInput.nextLine();//Sets user input to a variable called userOption.
if (usersOption.equals(s)){
System.out.println("*****************************************\n Withdrawl\n*****************************************\nHow much would you like to draw?\nEnter your number");
String withdrawl;
withdrawl = userInput.nextLine();
balance = balance - withdrawl;
}else {System.out.println();}
if (usersOption.equals(ss)) {
System.out.println("*****************************************\n Deposit\n*****************************************\nHow much do you want to deposit?");
userInput.nextLine(); }else {System.out.println();}
if (usersOption.equals(sss)) {
System.out.println("*****************************************\n Your balance is 100\n*****************************************");
}else {System.out.println();}
if (usersOption.equals(ssss)) {
System.out.println("*****************************************\n Goodbye!\n*****************************************");
System.exit(0); }else {System.out.println();}
}
你可以这样称呼它:
myATM.ifStatement(myATM.go());
希望有所帮助
答案 1 :(得分:1)
您收到该错误的原因是您宣布"平衡"在go()方法中。
您可以在ifStatement(int balance)中将此变量设置为输入,也可以在类的开头定义它。