在我检查以确保变量的值是正确的形式,然后声明变量之后,我怎样才能将它公开,所以我不必嵌套我的所有代码?例如
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));
}
}
}
}
}