modify into while(no loop) asking user if they want to continue as well as using
ID: 3721187 • Letter: M
Question
modify into while(no loop)
asking user if they want to continue as well as using inputfile and outputfile
java 1(Beginners)
^ example of it to be something like this.
y 1mport java.uti1 class Temperature lass public static double FahToCe1 (double F) va us.jav return (F 32.0) 5.09.o public static double CelToFah (double C) return 9.0 c/5.0 +32; public static double FahToKel (double F) return F 32.0 5.0/9.0 273: la public static double KelToFah (double K) return (K 273.0 9.0 5.032.0 public static double CelToKel (double C) return c + 273.0; ic static double KelToCel (double K) return K 273 ArguementDemo java cojavaTemperaturejava TemperatureCon emperatufe ViewSonicExplanation / Answer
Temperature .java //using while loop
import java.util.Scanner;
/**
*
*/
/**
* @author yourname
*
*/
public class Temperature {
// method to convert from Fahrenheit to Celsius
public static double FahToCel(double F) {
return (F - 32.0) * 5.0 / 9.0;
}
// method to convert from Celsius to Fahrenheit
public static double CelToFah(double C) {
return 9.0 * C / 5.0 + 32;
}
// method to convert from Fahrenheit to Kelvin
public static double FahToKel(double F) {
return (F - 32.0) * 5.0 / 9.0 + 273;
}
// method to convert from Kelvin to Fahrenheit
public static double kelToFah(double K) {
return 9.0 * (K - 273) / 5.0 + 32;
}
// method to convert from Celsius to Kelvin
public static double CelToKel(double C) {
return C + 273.0;
}
// method to convert from Kelvin to Celsius
public static double kelToCel(double K) {
return K - 273.0;
}
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Using while loop
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
System.out.println("Enter the temperature : ");
double t = sc.nextDouble();
while (true) {
System.out.println(" Choose one of the following options ....");
System.out.println("1. Fahrenheit to Celsius");
System.out.println("2. Celsius to Fahrenheit");
System.out.println("3. Fahrenheit to Kelvin");
System.out.println("4. Kelvin to Fahrenheit");
System.out.println("5. Celsius to Kelvin");
System.out.println("6. Kelvin to Celsius");
int option = sc.nextInt();
if (option == 1) {
System.out.println(t + " Fahrenheit = " + FahToCel(t) + " Celsius");
break;
} else if (option == 2) {
System.out.println(t + " Celsius = " + CelToFah(t) + " Fahrenheit");
break;
} else if (option == 3) {
System.out.println(t + " Fahrenheit = " + FahToKel(t) + " Kelvin");
break;
} else if (option == 4) {
System.out.println(t + " Kelvin = " + kelToFah(t) + " Fahrenheit");
break;
} else if (option == 5) {
System.out.println(t + " Celsius = " + CelToKel(t) + " Kelvin");
break;
} else if (option == 6) {
System.out.println(t + " Kelvin = " + kelToCel(t) + " Celsius");
break;
} else {
System.out.println(" You have entered wrong choice !!!! Please try again ");
continue;
}
}
System.out.println("Do you want to continue : y or n");
//clear the buffer
sc.nextLine();
choice=sc.nextLine();
}
}
}
Output:
Enter the temperature :
54
Choose one of the following options ....
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
3. Fahrenheit to Kelvin
4. Kelvin to Fahrenheit
5. Celsius to Kelvin
6. Kelvin to Celsius
1
54.0 Fahrenheit = 12.222222222222221 Celsius
Do you want to continue : y or n
y
Enter the temperature :
45
Choose one of the following options ....
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
3. Fahrenheit to Kelvin
4. Kelvin to Fahrenheit
5. Celsius to Kelvin
6. Kelvin to Celsius
11
You have entered wrong choice
Choose one of the following options ....
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
3. Fahrenheit to Kelvin
4. Kelvin to Fahrenheit
5. Celsius to Kelvin
6. Kelvin to Celsius
6
45.0 Kelvin = -228.0 Celsius
Do you want to continue : y or n
y
Enter the temperature :
5
Choose one of the following options ....
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
3. Fahrenheit to Kelvin
4. Kelvin to Fahrenheit
5. Celsius to Kelvin
6. Kelvin to Celsius
4
5.0 Kelvin = -450.4 Fahrenheit
Do you want to continue : y or n
y
Enter the temperature :
85
Choose one of the following options ....
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
3. Fahrenheit to Kelvin
4. Kelvin to Fahrenheit
5. Celsius to Kelvin
6. Kelvin to Celsius
2
85.0 Celsius = 185.0 Fahrenheit
Do you want to continue : y or n
n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.