ok so I need to make this program that involves the gravity constant. but i let the user decide that
double g;
String unit;
if (n == JOptionPane.YES_OPTION) {
g = 9.8;
System.out.print(g);
unit = "meters/s";
}
else {
g = 32;
System.out.print(g);
unit = "feet/s";
}
and then i put it into this formula OUTSIDE of the if statement
double ycoord = (velo0*sinF*time)-((g)((time*time)))/2;
i know that the scope of the if statement ends after the last curly brace but i'm wondering if there is any way to call for one of the values of g
thanks in advance!
If you have the above bit of code inside a method, then it's scope is restricted to that method. However you can create a class variable g and set it within your method.
Public Class Test {
//g can only be accessed within this class
//however you can access g with the following getter method
private double g;
public static void setG() {
this.g = 9.5;
}
public static void setGWithInput(Double input) {
this.g = input;
}
public static void printG() {
//you can access the value of g anywhere from your class
System.out.println("Value of g is" + this.g);
}
//create a public getter to access the value of g form outside the class
public double getG() {
return this.g;
}
}