如何从system.in public中创建变量

时间:2013-02-26 19:15:01

标签: java if-statement

在我检查以确保变量的值是否以正确的形式,然后声明变量如何使该公共,所以我不必嵌套我的所有代码?例如

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

3 个答案:

答案 0 :(得分:1)

Here是一个描述Java范围的快速链接。 This链接描述的更清楚一点,但第一个更贴近您正在寻找的内容。

一个简单的例子:

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

另外,我不想让你感到困惑,或者跳过你在课堂上学到的东西,所以我主要是为了将来的参考,但另外要考虑的是将值存储在一个Object中。 / p>

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

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

答案 1 :(得分:0)

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

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


 ... blah blah ...

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

... blah blah ...
}

答案 2 :(得分:-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));
                }
            }
        }