2013-02-26 49 views
0

我檢查後,確保變量的值是正確的形式,然後聲明變量如何使公開,所以我不必窩我所有的代碼?例如我如何從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)); 
     } 
     } 
     } 
    } 
} 
+0

爲什麼你檢查它是否有下一個double,然後讀取一個int? – Ingo 2013-02-26 19:22:46

+0

謝謝,我不知道爲什麼我沒有聽到。 – user1940007 2013-02-26 19:26:57

回答

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中。如果你想有一個在本質上有所全球不變的價值觀,宣佈他們最終並初始化它們無論是在聲明或構造

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

聲明您的變量爲靜態全局變量開始,然後像這樣給它們賦值。

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

不好的建議。如果他在方法開始處聲明變量就足夠了。 – Ingo 2013-02-26 19:24:24

+0

這仍然嵌套ifs – user1940007 2013-02-26 19:30:12

0

例如,你可以這樣做以下。另外,嘗試使用一個有意義的名字(下面我的例子中的名字選擇可能不正確)。例如

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


... blah blah ... 

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

... blah blah ... 
}