繁体   English   中英

我如何在system.public中使变量

[英]How can I make variables from system.in public

在检查以确保变量的值格式正确之后,然后声明该变量,如何使之公开,因此不必嵌套所有代码? 例如

public class Universalgravitation {
    static Scanner userInput = new Scanner(System.in);
    public static void main(String[] args)
    {           
        double G = .00000000006667;
        System.out.println("Keep in mind the upper limit for all of the values is 2 billion ");
        System.out.print("What is the mass of the first object? ");
        if(userInput.hasNextInt())
        {               
            int Mass1 = userInput.nextInt();
            System.out.print("What is the mass of the second object? ");
        if(userInput.hasNextInt())
        {               
            int Mass2 = userInput.nextInt();
            System.out.print("What is the radial distance between the two objects? ");
        if(userInput.hasNextInt())
        {               
            int Dist = userInput.nextInt();
            System.out.println("The gravitational force in newtons is: " + (G * Mass1 * Mass2) / (Dist * Dist));
        }
        }
        }
    }
}

是描述Java范围定义的快速链接。 链接对其进行了更清晰的描述,但第一个链接更接近您要查找的内容。

简要示例:

class Something{
    public void example(){
        int value=1;
        System.out.println("value from before a block: "+value);
            {
                value=2;
                System.out.println("value from inside a block: "+value);
            }
        System.out.println("value from after a block: "+value);
    }
}

另外,我也不想冒险使您感到困惑,或者不敢超越您在课堂上学到的东西,因此我主要是为了将来参考而提出,但是要考虑的另一件事是将值存储在对象中。

例如,您可以执行以下操作:

class Foo{
    static final double G = .00000000006667;
    private int Mass1;
    private int Mass2;
    private int Dist=1;//defaulting to avoid division by zero

    public int getMass1(){return mass1;}
    public void setMass1(int mass1){this.mass1=mass1;}
    ....

    public double getGravitationalForce(){
        return (G * Mass1 * Mass2) / (Dist * Dist);
    }
}

如果您想拥有本质上是全局的不变值,请将它们声明为final并在声明或构造函数中对其进行初始化。 另外,请尝试使用有意义的名称(在下面的示例中,名称的选择可能不正确)。 例如

public class blammy
{
  private static final double coefficientOfGravity = .00000000006667;


 ... blah blah ...

 System.out.println("blammy: " + (coefficientOfGravity * mass1 * mass2) / (dist * dist));

... blah blah ...
}

首先将变量声明为静态全局变量,然后为它们分配值。

public class Universalgravitation {
    static Scanner userInput = new Scanner(System.in);

    static double G

    static int Mass1;
    static int Mass2;
    static int Dist;

    public static void main(String[] args)
    {           
        G = .00000000006667;
        System.out.println("Keep in mind the upper limit for all of the values is 2 billion ");
        System.out.print("What is the mass of the first object? ");
        if(userInput.hasNextInt())
        {               
            Mass1 = userInput.nextInt();
            System.out.print("What is the mass of the second object? ");
            if(userInput.hasNextDouble())
            {               
                Mass2 = userInput.nextInt();
                System.out.print("What is the radial distance between the two objects? ");
                if(userInput.hasNextDouble())
                {               
                    Dist = userInput.nextInt();
                    System.out.println("The gravitational force in newtons is: " + (G * Mass1 * Mass2) / (Dist * Dist));
                }
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM