【问题标题】:Make a variable accessible by all methods使所有方法都可以访问变量
【发布时间】:2015-09-20 06:03:21
【问题描述】:

我对 java 有点陌生,我最近了解了方法(太酷了!)。我想知道是否可以在我的 main 方法中声明一个变量并在我的其他方法中使用它。

我想做的是使用方法创建一个计算器(只是为了练习这个新概念),但我不想每次都在每个方法中声明变量。

这是代码的骨架结构:

class GS1{



public static void main (String[]args){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the math operation to be completed: ");
    String opt = input.nextLine();
    int x,y;  // I tried declaring variables here
    switch(opt){

    case  "addition" :
    // addition method goes here
    break;
    case "subtraction":
    //subtraction method goes here
    break;
    case "multiplication":
    //multiplication method   goes  here
    break;
    case "division":
    //division method goes here
    break;
    }

}

static void addition(){
    System.out.println("Enter first value for addition");
    x=input.nextint(); // i get error stating both "x" and "input" cannot be resolved as a variable


}

static void subtration(){


}

static void Multiplication(){

}

static void Division(){



}

}

【问题讨论】:

  • 您应该将值作为 参数 传递给方法。然后方法应该返回结果
  • 建议接下来要学习什么:尝试创建一个计算器类,因为使用“全局变量”肯定不是一个好习惯

标签: java variables methods


【解决方案1】:

您应该将变量放在所有方法之外但在类中,创建全局访问。

public class ClassName
{
    public int x;
    public int y;

    public void method1()
    {
        x = 3;
    }

    public void method2()
    {
        y = 1;
    } 
}

【讨论】:

  • 但原来的帖子有静态方法。
  • @faheemfarhan 这是一个通用示例。这无关紧要。
  • 我认为最好还是在问题的上下文中回答。
【解决方案2】:

在类级别移动变量,使其成为类中的字段。

既然你在学习,最好不要使用static字段或方法,除了main方法。

【讨论】:

    【解决方案3】:

    更好地组织你的代码,编写如下代码:

    class Operation {
    
        public double addition(double... value) {
            double result = 0;
            for (double i : value) {
                result += i;
            }
            return result;
        }
    
        public double subtration(.....) {
            // .........................
            return 0.0;
        }
    
        public double multiplication(.....) {
            // .........................
            return 0.0;
        }
    
        public double division(.....) {
            // .........................
            return 0.0;
        }
    }
    
    public class GS1 {
    
        public static void main(String[] args) {
            Operation operations=new Operation();
    
    
            //read value code 
            double result=operations.addition(value);
    
            //print result code
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      你需要这样的东西:

          class GS1 {
      
              public static int x;
              public static Scanner input;
      
              public static void main(String[] args) {
                  input = new Scanner(System.in);
                  System.out.println("Enter the math operation to be completed: ");
                  String opt = input.nextLine();
                  int x, y; // I tried declaring variables here
                  switch(opt){
      
                  case  "addition" :
                  // addition method goes here
                  break;
                  case "subtraction":
                  //subtraction method goes here
                  break;
                  case "multiplication":
                  //multiplication method   goes  here
                  break;
                  case "division":
                  //division method goes here
                  break;
                  }
              }
      
              static void addition() {
                  System.out.println("Enter first value for addition");
                   x=input.nextint(); // i get error stating both "x" and "input" cannot
                  // be resolved as a variable
      
              }
      
              static void subtration() {
      
              }
      
              static void Multiplication() {
      
              }
      
              static void Division() {
      
              }
      
          }
      

      记住在你的字段声明(x 和输入)中使用“static”修饰符,你不能对非静态字段进行静态引用。

      更好的方法是使用对象而不是将所有方法放在一个类 (GS1) 中。例如,在您的 cmets 中创建一个类似 Marged 建议的 Calculator 类

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 2022-08-16
        • 1970-01-01
        相关资源
        最近更新 更多