my code is looks like this:
import java.util.*; public class PojectConvert{ public void showFeet(double meters){ double feet = meters * 3.281; System.out.println(meters+" meters is "+feet+" feet"); } public void showMeter(double feet){ double meters = feet * 0.3048; System.out.println(feet +" feet is "+ meters +" meters."); } public void showKilograms(double pounds){ double kilograms = pounds * 0.454; System.out.println(pounds +" pounds is "+kilograms+" kilograms"); } public void showPounds(double kilograms){ double pounds = kilograms * 2.2808; System.out.println(kilograms+" kilograms is "+pounds+" pounds"); } public void showGallons(double liters){ double gallons = liters * 3.7854; System.out.println(liters+" liters is "+gallons+" gallons"); } public void showLiters(double gallons){ double liters = gallons * 0.26417; System.out.println(gallons+" gallons is "+liters+" liters"); } public void menu(){ System.out.print("Unit converter \n"); System.out.println("1 Convert Length"); System.out.println("2 Convert Weight"); System.out.println("3 Convert volume"); System.out.println("Enter in amount to convert: "); Scanner scan=new Scanner(System.in); double m=scan.nextDouble(); boolean quit = false; do{ System.out.println(); System.out.println("1. Meters to feet"); System.out.println("2. Feet to meters"); System.out.println("3. pounds to kilograms"); System.out.println("4. kilograms to pounds"); System.out.println("5. Gallons to liters"); System.out.println("6. Liters to gallons"); System.out.println("7. I give up, I can't go on!"); System.out.println(); System.out.print("Enter your choice: "); int menu = scan.nextInt(); switch(menu) { case 1: showFeet(m); break; case 2: showMeter(m); break; case 3: showKilograms(m); break; case 4: showPounds(m); break; case 5: showGallons(m); break; case 6: showLiters(m); break; case 7: quit = true; System.out.println("We lost another one."); break; default: System.out.println("Invalid Entry!"); } } while (!quit); } public static void main(String[]args){ PojectConvert convert=new PojectConvert(); convert.menu(); } }
she wants it to be able to do this
1. first you should ask the user what type of conversion he wants (1. length, 2. wight, 3. volume)
int t=scan.nextInt();
2. then you should use a switch statement to display only the units corresponding to that type of conversion:
switch (t) {
case 1:
System.out.println("1. Meters to feet");
System.out.println("2. Feet to meters");
break;
case 2:
System.out.println("1. pounds to kilograms");
System.out.println("2. kilograms to pounds");
break;
case 3:
System.out.println("1. Gallons to liters");
System.out.println("2. Liters to gallons");
break;
}
3. Then you should ask for the conversion direction
int direction= scan.nextInt();
4. And then you should ask for the value to be converted:
int m = scan.nextInt();
5. Now you have all the info you need, you can write another switch statement to do the conversion:
switch (t) {
case 1: if (direction==1) showFeet(m);
else showMeter(m);
break;
case 2:if (direction==1) showKilograms(m);
else showPounds(m);
break;
case 3:....
}
The other thing you need to do is: every time you ask the user to make a selection, you have to make sure he enters a valid value.
For example, when asking for the type of conversion (step 1 above) you have to make sure the value entered is 1, 2 or 3.
So you need to use a loop:
int t;
do {
System.out.println(....);
t=scan.nextInt();
} while (t!=1 && t!=2 && t!=3); // as long as t is different from 1, 2 and 3 the user will be asked to entered a value again
i have no idea how to change my program to make all that work. can anyone help me out?