0

在我检查以确保变量的值是正确的形式,然后声明变量之后,我怎样才能将它公开,所以我不必嵌套我的所有代码?例如

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

3 回答 3

1

是描述 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);
    }
}
于 2013-02-26T19:25:12.427 回答
0

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

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


 ... blah blah ...

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

... blah blah ...
}
于 2013-02-26T19:32:28.710 回答
-1

将您的变量声明为静态全局变量,然后像这样为它们赋值。

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));
                }
            }
        }
于 2013-02-26T19:22:12.737 回答