Newbie to Java here. I have written a time conversion program that accepts any number from 0 to 9e+18 as valid input ans asks for conversion parameters from the user, eg, from weeks to seconds. Wrote all the calculations in a huge block of if-else-if-else lines inside a do-while loop. Program exits whenever user enters 'q' for quit. My current program works its conversion flawlessly between different time units.
My question is, how do I utilize switch statements in lieu of this horrible block of if-else?
if (fromUnit.equals("w") && toUnit.equals("s")) {
results = userNumber * SECONDS_PER_WEEK;
System.out.printf("There are %,.2f seconds", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "week" : "weeks");
} else if (fromUnit.equals("h") && toUnit.equals("s")) {
results = userNumber * SECONDS_PER_HOUR;
System.out.printf("There are %,.2f seconds", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "hour" : "hours");
} else if (fromUnit.equals("d") && toUnit.equals("s")) {
results = userNumber * SECONDS_PER_DAY;
System.out.printf("There are %,.2f seconds", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "day" : "days");
} else if (fromUnit.equals("m") && toUnit.equals("s")) {
results = userNumber * SECONDS_PER_MINUTE;
System.out.printf("There are %,.2f seconds", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "minute" : "minutes");
} else if (fromUnit.equals("s") && toUnit.equals("s")) {
results = userNumber * SECONDS_PER_SECOND;
System.out.printf("There are %,.2f seconds per second.\n", results);
} else if (fromUnit.equals("w") && toUnit.equals("m")) {
results = userNumber * MINUTES_PER_WEEK;
System.out.printf("\nThere are %,.2f minutes", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "week" : "weeks");
} else if (fromUnit.equals("d") && toUnit.equals("m")) {
results = userNumber * MINUTES_PER_DAY;
System.out.printf("\nThere are %,.2f minutes", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "day" : "days");
} else if (fromUnit.equals("h") && toUnit.equals("m")) {
results = userNumber * MINUTES_PER_HOUR;
System.out.printf("\nThere are %,.2f minutes", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "hour" : "hours");
} else if (fromUnit.equals("m") && toUnit.equals("m")) {
results = userNumber * MINUTES_PER_MINUTE;
System.out.printf("\nThere are %,.2f minutes", results);
System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "minute" : "minutes");
} else if (fromUnit.equals("h") && toUnit.equals("h")) {
results = userNumber * HOURS_PER_HOUR;
System.out.printf("\nThere are %,.2f hours in %,d hours.\n", results, userNumber);
} else if (fromUnit.equals("d") && toUnit.equals("h")) {
results = userNumber * HOURS_PER_DAY;
System.out.printf("\nThere are %,.2f hours in %,d days.\n", results, userNumber);
} else if (fromUnit.equals("w") && toUnit.equals("h")) {
results = userNumber * HOURS_PER_WEEK;
System.out.printf("\nThere are %,.2f hours in %,d weeks.\n", results, userNumber);
} else if (fromUnit.equals("d") && toUnit.equals("d")) {
results= userNumber * DAYS_PER_DAY;
System.out.printf("\nThere are %,.2f days in %,d days.\n", results, userNumber);
} else if (fromUnit.equals("w") && toUnit.equals("d")) {
results = userNumber * DAYS_PER_WEEK;
System.out.printf("\nThere are %,.2f days in %,d weeks.\n", results, userNumber);
} else if (fromUnit.equals("s") && toUnit.equals("ms")) {
results = userNumber * MICROSECONDS_PER_SECOND;
System.out.printf("\nThere are %,.3f microseconds in %,d seconds.\n", results, userNumber);
} else if (fromUnit.equals("d") && toUnit.equals("w")) {
results = userNumber * WEEKS_PER_DAY;
System.out.printf("\nThere are %,.3f weeks in %,d days.\n", results, userNumber);
} else if (fromUnit.equals("h") && toUnit.equals("d")) {
results = userNumber * DAYS_PER_HOUR;
System.out.printf("\nThere are %,.3f days in %,d hours.\n", results, userNumber);
} else if (fromUnit.equals("s") && toUnit.equals("h")) {
results = userNumber * HOURS_PER_MINUTE * MINUTES_PER_SECOND;
System.out.printf("\nThere are %,.3f hours in %,d seconds.\n", results, userNumber);
} else if (fromUnit.equals("ms") && toUnit.equals("s")) {
results = userNumber * SECONDS_PER_MICROSECOND;
System.out.printf("\nThere are %,.3f seconds in %,d microseconds.\n", results, userNumber);
} else if (fromUnit.equals("m") && toUnit.equals("ms")) {
results = userNumber * MICROSECONDS_PER_MINUTE;
System.out.printf("\nThere are %,.3f microseconds in %,d minutes.\n", results, userNumber);
} else if (fromUnit.equals("h") && toUnit.equals("ms")) {
results = userNumber * MICROSECONDS_PER_HOUR;
System.out.printf("\nThere are %,.3f microseconds in %,d hours.\n", results, userNumber);
} else if (fromUnit.equals("d") && toUnit.equals("ms")) {
results = userNumber * MICROSECONDS_PER_DAY;
System.out.printf("\nThere are %,.3f microseconds in %,d days.\n", results, userNumber);
} else if (fromUnit.equals("w") && toUnit.equals("ms")) {
results = userNumber * MICROSECONDS_PER_WEEK;
System.out.printf("\nThere are %,.3f microseconds in %,d weeks.\n", results, userNumber);
} else {
System.out.println("\nCannot calculate!\n");
}
} while (userNumber >= 0);
}
}
Aucun commentaire:
Enregistrer un commentaire