2016-10-25 4 views
-2
import java.util.Scanner; 
public class ATM { 
public static void main(String[] args) throws Exception { 
    String ATM; 
    ATM myATM = new ATM(); 
    myATM.go(); 
    ifStatement(); 
    //Main method, declares variables and calls the go() and ifStatement() methds. 
} 

public void go() throws Exception { 
    int balance; 
    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"); 

    balance = userInput.nextInt(); 

    //Starts the program and sets a value to the variable "balance". 
} 

    public static void ifStatement() 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();} 
    } 
} 

go() 메소드에서 balance 변수를 선언했지만 이제는 해당 변수의 값을 if 문 중 하나에 저장하려고합니다. 그러나 컴파일러는 가변 균형을 인식하지 못한다고 말합니다. 아무도이 문제를 해결하는 방법을 알고 있습니까?새 메서드에서 다른 메서드의 변수를 어떻게 사용합니까?

+3

당신은 그렇게하지 않습니다. 액세스 변수는 다른 방법으로는 사용할 수 없습니다. 대신에 : 당신이 필요한 것 이상을 가지고 있다면; 그런 다음 클래스 내에서 ** 필드 **를 사용합니다. 그러나 이것은 ** 슈퍼 슈퍼 기본 ** 물건입니다. 그런 것들을 여기에 넣지 말고 좋은/책 튜토리얼 (https://docs.oracle.com/javase/tutorial/)을 읽어주십시오! 게다가 : 우리는 당신을 돕기 위해 우리 시간을 보내길 원합니다, 그래서 당신은 당신의 입력을 들여 쓰기/포맷하기위한 시간을 보내시기 바랍니다. – GhostCat

+1

답장을 보내 주셔서 감사합니다. 미안 해요, 나는 자바와 프로그래밍에 대해 매우 익숙하다. (며칠 만에) 일반적으로 내 입력을 적절히 들여 쓰고 싶지 않은 것처럼하지 않는다. 여전히 기초를 배우고. –

답변

1

그냥 사용하여 잔액을 돌려주고 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

이유 왜 당신을 도와줍니다 당신이 go() 메서드 내에서 "balance"를 선언했기 때문에 그 오류가 발생했습니다.

ifStatement (int balance)에이 변수를 입력하거나이 변수를 클래스의 시작 부분에서 정의 할 수 있습니다.

관련 문제