有没有办法在另一种方法中访问变量?

Is there a way to access variables in another method?

提问人:ddhwwl 提问时间:1/29/2023 最后编辑:Mark Rotteveelddhwwl 更新时间:1/29/2023 访问量:83

问:

我是 java 的新手,我想知道是否有办法从另一个方法中的一个方法访问变量。在使用方法编写程序时,我意识到我不能只从一个方法中获取一个变量,然后在另一个方法中使用它。所以我想知道是否有办法做到这一点。

这是我到目前为止的代码

import java.util.Scanner;

public class program{
    
    public static void mass(){
        double a,e,p,v;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the volume: ");
        v=scanner.nextDouble();
        p=0.8;
        System.out.println("Enter the alcohol volume:(in percents) ");
        e=scanner.nextDouble();
        e=e/100;
        a=v*e*p;
        
        System.out.println("mass is: "+a);
    }
    public static void Concentration(){
        double w,r,m;
        String person;
        System.out.println("Enter the person: m for male, f for female, j for teenager ");
        Scanner sc=new Scanner(System.in);
        person=sc.nextLine();
        switch(person){
            case "m": r=0.7;
            break;
            case "f": r=0.6;
            break;
            case "j": r=0.5;
            break;
        }
        System.out.println("Enter person's weight: ");
        m=sc.nextDouble();
        //w=a/(m*r); //a from the method mass

    }
    public static void main(String[]args){
       mass();
       Concentration();
        /*If(w>=0.5){ //w from the method concentration
            System.out.println("You cannot drive!");
            Else{
                System.out.println("You can drive");
            }
        } */

    }
}
Java 变量 方法

评论

0赞 Lorenzo 1/29/2023
可以在方法外部声明变量。

答:

-2赞 Willson Vulembere 1/29/2023 #1

要使变量在类的所有函数中均可访问,您可以在当前类中静态相关变量

软件包 javaapplication6;

导入 java.util.Scanner;

@author武伦贝雷

公共类 JavaApplication6 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    mass();
    Concentration();
    if (w >= 0.5) {
        System.out.println("You cannot drive!");
    } else {
        System.out.println("You can drive");

    }
}

static double w;

public static void mass() {
    double a, e, p, v;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the volume: ");
    v = scanner.nextDouble();
    p = 0.8;
    System.out.println("Enter the alcohol volume:(in percents) ");
    e = scanner.nextDouble();
    e = e / 100;
    a = v * e * p;

    System.out.println("mass is: " + a);
}

public static void Concentration() {
    double r, m;
    String person;
    System.out.println("Enter the person: m for male, f for female, j for teenager ");
    Scanner sc = new Scanner(System.in);
    person = sc.nextLine();
    switch (person) {
        case "m":
            r = 0.7;
            break;
        case "f":
            r = 0.6;
            break;
        case "j":
            r = 0.5;
            break;
    }
    System.out.println("Enter person's weight: ");
    m = sc.nextDouble();
    //w=a/(m*r); //a from the method mass

}

}

评论

0赞 Community 1/31/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。
0赞 Lajos Arpad 1/29/2023 #2

有两种类型的变量:

  • 局部变量(块范围)
  • 成员变量(类/实例级别范围)

请参见:https://www.geeksforgeeks.org/variable-scope-in-java/

因此,如果要在多个方法中重用变量,则需要将其从局部变量转换为成员变量。在你的例子中,你只使用方法,所以在方法之外会声明为statica

static double a;

并避免在方法中声明它,因此声明行将更改为mass

double e,p,v;

请注意,缺少 以避免变量阴影a

您可能希望将方法更改为实例级方法,在这种情况下,您可以根据计划和需求不使用关键字进行声明。astatic

此外,一种众所周知的方法是实现 getter 和 setter,以确保每当您获取或设置值时,如果存在常见操作,则它们只实现一次,而不是重复代码

下面你可以看到为实现指定目标而对代码进行的最简单的更改:

import java.util.Scanner;

public class program{
    
    static double a;
    
    public static void mass(){
        double e,p,v;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the volume: ");
        v=scanner.nextDouble();
        p=0.8;
        System.out.println("Enter the alcohol volume:(in percents) ");
        e=scanner.nextDouble();
        e=e/100;
        a=v*e*p;
        
        System.out.println("mass is: "+a);
    }
    public static void Concentration(){
        double w,r,m;
        String person;
        System.out.println("Enter the person: m for male, f for female, j for teenager ");
        Scanner sc=new Scanner(System.in);
        person=sc.nextLine();
        switch(person){
            case "m": r=0.7;
            break;
            case "f": r=0.6;
            break;
            case "j": r=0.5;
            break;
        }
        System.out.println("Enter person's weight: ");
        m=sc.nextDouble();
        w=a/(m*r); //a from the method mass

    }
    public static void main(String[]args){
       mass();
       Concentration();
        /*If(w>=0.5){ //w from the method concentration
            System.out.println("You cannot drive!");
            Else{
                System.out.println("You can drive");
            }
        } */

    }
}

最后,您还可以用作返回值,例如:a

import java.util.Scanner;

public class program{
    
    public static double mass(){
        double a,e,p,v;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the volume: ");
        v=scanner.nextDouble();
        p=0.8;
        System.out.println("Enter the alcohol volume:(in percents) ");
        e=scanner.nextDouble();
        e=e/100;
        a=v*e*p;
        
        System.out.println("mass is: "+a);
        return a;
    }
    public static void Concentration(double a){
        double w,r,m;
        String person;
        System.out.println("Enter the person: m for male, f for female, j for teenager ");
        Scanner sc=new Scanner(System.in);
        person=sc.nextLine();
        switch(person){
            case "m": r=0.7;
            break;
            case "f": r=0.6;
            break;
            case "j": r=0.5;
            break;
        }
        System.out.println("Enter person's weight: ");
        m=sc.nextDouble();
        //w=a/(m*r); //a from the method mass

    }
    public static void main(String[]args){
       Concentration(mass());
        /*If(w>=0.5){ //w from the method concentration
            System.out.println("You cannot drive!");
            Else{
                System.out.println("You can drive");
            }
        } */

    }
}