/* * This program does temperature conversions. * It can convert from Fahrenheit to Celsius or from Celsius * to Fahrenheit, at the user's preference. */ import tio.*; class Convert { public static void main(String[] args) { double fahrenheit = 0.0, celsius = 0.0; boolean keepGoing = true; char conversion; // Keep doing conversions until the user says to stop while(keepGoing == true) { // Ask the user which conversion to perform System.out.print("\nPlease enter\tF for Fahrenheit to Celsius" + " conversion or\n\t\tC for Celsius to " + "Fahrenheit conversion: "); conversion = (char)Console.in.readChar(); Console.in.readChar(); // Clean up the extra newline // Perform the conversion, depending upon what the user entered if(conversion == 'F' || conversion == 'f') { // Perform the F to C conversion // Get the Fahrenheit temperature System.out.println(""); System.out.print("Please enter the Fahrenheit temperature: "); fahrenheit = Console.in.readDouble(); Console.in.readChar(); // Clean up the extra newline // Convert it to Celsius celsius = (5.0/9.0) * (fahrenheit - 32); // Print out the result System.out.println(fahrenheit + " Fahrenheit = " + celsius + " Celsius"); } else { // Perform the C to F conversion // Get the Celsius temperature System.out.println(""); System.out.print("Please enter the Celsius temperature: "); celsius = Console.in.readDouble(); Console.in.readChar(); // Clean up the extra newline // Convert it to Fahrenheit fahrenheit = ((9.0/5.0) * celsius) + 32; // Print out the result System.out.println(celsius + "Celsius = " + fahrenheit + " Fahrenheit"); } // See if we should do another conversion System.out.println(""); System.out.print("Would you like to do another conversion (y/n)?"); keepGoing = ((char)Console.in.readChar() == 'y'); Console.in.readChar(); // Clean up the extra newline } } }