Need to be able to loop if user selects one of the two options it should terminate, if user selects invalid option keep looping until one of the options is selected
import java.util.Scanner;
public class conversion {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Which conversion do you want?");
System.out.println("Pounds to Kilograms(enter 1)");
System.out.println("Kilograms to Pounds(enter 2)");
double kilograms, pounds;
int choice = input.nextInt();
while(){
if(choice == 1){
System.out.println("What weight value do you want converted?");
pounds = input.nextDouble();
kilograms = pounds / 2.20462;
kilograms = Math.round(kilograms * 100.0) / 100.0;
System.out.println( +pounds+ " pounds is equal to " +kilograms+ " kilograms");
}
else if(choice == 2){
System.out.println("What weight value do you want converted?");
kilograms = input.nextDouble();
pounds = kilograms/0.453592;
pounds = Math.round(pounds * 100.0) / 100.0;
System.out.println( +kilograms+ " kilograms is equal to " +pounds+ " pounds");
}
else if((choice != 1) || (choice != 2)){
System.out.println("Invalid please try again");
System.out.println("Which conversion do you want?");
System.out.println("Pounds to Kilograms(enter 1)");
System.out.println("Kilograms to Pounds(enter 2)");
choice = input.nextInt();
}
}
}
}
- show a menu of choices;
- ask the user which operation they want;
- read in the user response;
- determine if it is an acceptable response, repeat request if not.
- compute and display the result
Display the menu and ask the user which conversion they want:
Which conversion do you want?
Then ask the user for what weight value should be converted.
What weight value do you want converted?
If the user chooses 1, then convert the pounds to Kilograms, else if the user chooses 2 convert the kilograms to pounds, else if the user choice is anything else, tell the user their answer is invalid and request another answer.
Aucun commentaire:
Enregistrer un commentaire