Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使变量可由所有方法访问_Java_Variables_Methods - Fatal编程技术网

Java 使变量可由所有方法访问

Java 使变量可由所有方法访问,java,variables,methods,Java,Variables,Methods,我对java有点陌生,最近学习了一些方法(太酷了!)。我想知道是否可以在我的主方法中声明一个变量,并在其他方法中使用它 我想做的是使用方法创建一个计算器(只是为了实践这个新概念),但我不想每次都在每个方法中声明变量 以下是代码的框架结构: class GS1{ public static void main (String[]args){ Scanner input = new Scanner(System.in); System.out.println("Enter th

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

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

以下是代码的框架结构:

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(){



}

}

您应该将变量放置在所有方法之外,但不在类内,从而创建全局访问

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

    public void method1()
    {
        x = 3;
    }

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

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


由于您正在学习,最好不要使用
静态
字段或方法,除非使用
main
方法。

更好地组织代码,制作如下代码:

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

    }
}

你需要这样的东西:

    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)。例如,创建一个计算器类,如Marged suggest在您的注释中

您应该将值作为参数传递给方法。然后这些方法应该返回结果。建议下一步学习什么:尝试创建一个计算器类,因为使用“全局变量”肯定不是一个好的实践,但原始帖子有静态方法。@faheemfarhan这是一个通用示例。那是不相干的。我认为最好也结合问题的上下文来回答。