Reputation: 35
Basically I want to be able to input a number that isn't an option and then be given the option to choice again and get the statement to repeat.
Scanner keyboard = new Scanner(System.in);
double weight;
int Choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune");
Choice =keyboard.nextInt();
if (Choice == 1){
System.out.println("Your weight on Venus would be " + (weight * 0.78));
}
else if (Choice == 2){
System.out.println("Your weight on Mars would be " + (weight * .39));
}
else if (Choice == 3){
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
}
else if (Choice == 4){
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
}
else if (Choice == 5){
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
}
else if (Choice == 6) {
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
}
else
System.out.println("This was not a choice, try again!");
Choice = keyboard.nextInt();
}
Upvotes: 0
Views: 154
Reputation: 7076
You have to learn the basic in programming language... do..while and switch statements.
Here is the sample program.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double weight;
int choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
do {
System.out
.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune \n7.EXIT");
System.out.println();
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("Your weight on Venus would be "
+ (weight * 0.78));
break;
case 2:
System.out.println("Your weight on Mars would be "
+ (weight * .39));
break;
case 3:
System.out.println("Your weight on Jupiter would be "
+ (weight * 2.65));
break;
case 4:
System.out.println("Your weight on Saturn would be "
+ (weight * 1.17));
break;
case 5:
System.out.println("Your weight on Uranus would be"
+ (weight * 1.05));
break;
case 6:
System.out.println("Your weight on Neptune would be "
+ (weight * 1.23));
case 7:
System.out.println("Leaving...");
break;
default:
System.out.println("This was not a choice, try again!");
break;
}
} while (choice != 7);
}
Use the Java Naming conventions to give names to variables. Start with lowercase letter.
Upvotes: 0
Reputation: 11153
This is an easier way to go, using a do-while
loop and a switch
.
Also fixed choice
Scanner keyboard = new Scanner(System.in);
double weight;
int choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
do {
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune\n 7. Exit");
choice = keyboard.nextInt();
switch(choice) {
case 1:
System.out.println("Your weight on Venus would be " + (weight * 0.78));
break;
case 2:
System.out.println("Your weight on Mars would be " + (weight * .39));
break;
case 3:
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
break;
case 4:
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
break;
case 5:
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
break;
case 6:
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
break;
case 7:
System.out.println("Bye");
break;
default:
System.out.println("This was not a choice, try again!");
break;
}
} while (choice != 7);
Upvotes: 2
Reputation: 68887
This can be done with a while loop. Also consider using arrays.
String[] planets = {"Mars", "Jupiter", .... };
double[] factors = {0.45, 1.5, ....};
double mass = keyboard.nextDouble();
int choice = keyboard.nextInt();
while (choice < 0 || choice >= planets.length)
{
System.out.println("Not a valid option!");
choice = keyboard.nextInt();
}
double weight = mass * factors[choice];
System.out.println("Your weight on " + planets[choice] + " would be " + weight);
So, basically, you ask it once politely. Afterwards, start shouting that it is wrong, until the user gave a proper input.
Upvotes: -1