Java 如何在公共场合从system.in中生成变量

Java 如何在公共场合从system.in中生成变量,java,if-statement,Java,If Statement,在我检查以确保变量的值是正确的形式,然后声明变量之后,我如何将其公开,这样我就不必嵌套所有代码了?比如说 public class Universalgravitation { static Scanner userInput = new Scanner(System.in); public static void main(String[] args) { double G = .00000000006667; Sy

在我检查以确保变量的值是正确的形式,然后声明变量之后,我如何将其公开,这样我就不必嵌套所有代码了?比如说

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));
        }
        }
        }
    }
}
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));
                }
            }
        }
是一个描述Java中作用域的快速链接。link对它的描述更为清晰,但第一个更接近于您所寻找的内容

一个简单的例子:

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 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));
        }
        }
        }
    }
}
public class blammy
{
  private static final double coefficientOfGravity = .00000000006667;


 ... blah blah ...

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

... blah blah ...
}

为什么你要检查它是否有下一个双精度,然后读一个整数?谢谢,我不知道为什么我没有听清楚。糟糕的建议。如果他在方法的开头声明变量就足够了。