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

また、私はあなたを混乱させたり、クラスで学んだことを先取りしたりするリスクを冒したくないので、私は主に将来の参考のためにこれを取り上げますが、考慮すべきもう1つのことはオブジェクトに値を格納することです。

たとえば、次のようなことを行うことができます。

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 に答える