-
Notifications
You must be signed in to change notification settings - Fork 58
/
Planetary_Weights.java
72 lines (61 loc) · 2.41 KB
/
Planetary_Weights.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Scanner;
public class PlanetaryWeights {
public static void main(String[] args) {
final double MERCURY_GRAVITY = 0.376;
final double VENUS_GRAVITY = 0.889;
final double MARS_GRAVITY = 0.378;
final double JUPITER_GRAVITY = 2.36;
final double SATURN_GRAVITY = 1.081;
final double URANUS_GRAVITY = 0.815;
final double NEPTUNE_GRAVITY = 1.14;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a weight on Earth: ");
double earthWeight = scanner.nextDouble();
System.out.print("Enter a planet: ");
String planet = scanner.next().toLowerCase().capitalize();
while (!isValidPlanet(planet)) {
if (planet.equals("Earth")) {
System.out.println("Please select a planet other than Earth.");
} else {
System.out.println("Error: " + planet + " is not a planet.");
}
System.out.print("Enter a planet: ");
planet = scanner.next().toLowerCase().capitalize();
}
double planetWeight;
switch (planet) {
case "Mercury":
planetWeight = earthWeight * MERCURY_GRAVITY;
break;
case "Venus":
planetWeight = earthWeight * VENUS_GRAVITY;
break;
case "Mars":
planetWeight = earthWeight * MARS_GRAVITY;
break;
case "Jupiter":
planetWeight = earthWeight * JUPITER_GRAVITY;
break;
case "Saturn":
planetWeight = earthWeight * SATURN_GRAVITY;
break;
case "Uranus":
planetWeight = earthWeight * URANUS_GRAVITY;
break;
default:
planetWeight = earthWeight * NEPTUNE_GRAVITY;
break;
}
double roundedWeight = Math.round(planetWeight * 100.0) / 100.0;
System.out.println("The equivalent weight on " + planet + ": " + roundedWeight);
}
public static boolean isValidPlanet(String planet) {
String[] validPlanets = { "Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
for (String validPlanet : validPlanets) {
if (validPlanet.equalsIgnoreCase(planet)) {
return true;
}
}
return false;
}
}