Java 如何在新方法中使用另一个方法中的变量?

Java 如何在新方法中使用另一个方法中的变量?,java,Java,我在go()方法中声明了balance变量,现在我正试图将该变量的值存储在一个if语句中。但是,编译器告诉我它无法识别变量平衡。有人知道如何解决这个问题吗?只要把go的余额还给ifStatements()作为参数即可 像这样,go()将返回一个整数 import java.util.Scanner; public class ATM { public static void main(String[] args) throws Exception { String ATM; A

我在go()方法中声明了balance变量,现在我正试图将该变量的值存储在一个if语句中。但是,编译器告诉我它无法识别变量平衡。有人知道如何解决这个问题吗?

只要把go的余额还给ifStatements()作为参数即可

像这样,
go()
将返回一个整数

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();}
    }
}
这样,您就可以为
ifStatements()
提供一个参数:

 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();
 }
你可以这样称呼它:

  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();}
    }

希望这有帮助

之所以会出现此错误,是因为您在go()方法中声明了“balance”


您可以像在ifStatement(int balance)中输入一样设置此变量,也可以在类的开头定义它。

您不能这样做。您不能从其他方法访问变量。取而代之的是:如果你有超过方法需要的东西;然后在类中使用一个字段。但是:这是超基本的东西。请阅读一些好的/书籍教程(),而不是在这里张贴这些东西!此外:您希望我们花时间帮助您,因此请您花时间正确缩进/格式化您的输入。谢谢您的回复。很抱歉,我对java和编程基本上是非常陌生的(几天之内),所以我并不是不想正确地缩进/格式化我的输入,只是我不知道怎么做,因为我还在学习基础知识。
myATM.ifStatement(myATM.go());