import java.util.*;
import java.lang.Math;
public class TempConverter
{
//Constants
public static final String FCONVERT = "\nConverted to Farenheit: ";
public static final String KCONVERT = "\nConverted to Kelvin: ";
public static void main(String[] args)
{
String userInputStr; //temporary String variable to hold String input from user
char endReply; // yes or no so y/n is fitting
int tempType; // 1 or 2 to describe the type of conversion
double celcius, farenheit, kelvin; //temperature will have decimals
boolean end; //boolean (true/false) to end while loop
Scanner inputStream = new Scanner(System.in);
// initialize variables
celcius = 0.;
farenheit = 0.;
kelvin = 0.;
end = true;
System.out.print("This program converts degress in Celcius to Farenheit or Kelvin \n");
while(end)
{
// get degress in celcius from user
System.out.print("\nTemperature in Celcius: ");
userInputStr = inputStream.nextLine();
celcius = Double.parseDouble(userInputStr);
System.out.print(celcius);
// input from user, what do they want to conver to
while(true)
{
System.out.print("\nConvert to (1)Farenheit or (2)Kelvin: ");
userInputStr = inputStream.nextLine();
tempType = Integer.parseInt(userInputStr);
if ((tempType == 1) || (tempType == 2))
{
break;
}
else
{
System.out.print(tempType);
System.out.println("\nPlease input either 1 or 2");
}
}
System.out.print(tempType);
//conversion
if (tempType == 1) //convert to farenheit
{
farenheit = (celcius*1.8)+32; //multiple by 1.8 and then add 32, result is a double
System.out.print(FCONVERT);
System.out.print(farenheit);
}
else //convert to kelvin
{
kelvin = celcius + 273.15; //add 273.15, result is a double
System.out.print(KCONVERT);
System.out.print(kelvin);
}
// input from user, ask if they want to do another conversion
while(true)
{
System.out.print("\nWould you like to do another conversion? (y/n): ");
userInputStr = inputStream.nextLine();
endReply = userInputStr.charAt(0);
if ((endReply == 'y') || (endReply == 'Y')) //do another conversion
{
System.out.print(endReply);
break;
}
else if ((endReply == 'n') || (endReply == 'N')) //stop here
{
System.out.print(endReply);
end = false;
break;
}
else //invalid input, repeat prompt
{
System.out.print(endReply);
System.out.print("\nPlease input y or n\n");
}
}
}
}
}
TempConverter.main(null);