我檢查後,確保變量的值是正確的形式,然後聲明變量如何使公開,所以我不必窩我所有的代碼?例如我如何從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));
}
}
}
}
}
爲什麼你檢查它是否有下一個double,然後讀取一個int? – Ingo 2013-02-26 19:22:46
謝謝,我不知道爲什麼我沒有聽到。 – user1940007 2013-02-26 19:26:57