package celsiusfahrenheitconverter;
import java.util.Scanner;
/** This java code reads
* Fahrenheit temperature the
* user enters and displays back
* temperature in Celsius. This also
* Reads the Celsius temperature
* entered and displays back
* Fahrenheit temperature.
*
* @author
*/
public class CelsiusFahrenheitConverter
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// declare variables
String from;
String to;
String temp;
int choice;
int fahrenheit;
int celsius;
Scanner input = new Scanner(System.in);
do
{
choice = -1;
System.out.println("Convert tmperature:");
System.out.println(" Convert from Fahrenheit to Celsius:");
System.out.println(" Convert from Celsius to Fahrenheit:");
System.out.println("Enter 3 - Exit:");
choice = input.nextInt();
switch (choice)
{
// case 1 asks user for a Fahrenheit temp to convert to Celsius
// case 2 asks user for a Celsius temp to convert to Fahrenheit
case 1:
System.out.println("Enter the temperature in Fahrenheit:");
fahrenheit = input.nextInt();
celsius = convertToCelsius(fahrenheit);
from = "Fahrenheit";
to = "Celsius";
break;
case 2:
System.out.println("Enter the temperature in Celsius:");
celsius = input.nextInt();
fahrenheit = convertToFahrenheit(celsius);
from = "Celsius";
to = "Fahrenheit";
break;
default:
continue; // no input or output
}
System.out.print("\n");
System.out.print(fahrenheit);
System.out.print(from);
System.out.print("=");
System.out.print(celsius);
System.out.print(to);
System.out.print('\n');
}
while (choice != 3);
}
/**
* Converts form Celsius to
* Fahrenheit
* @param temp The temperature
* in Celsius
* @return The temperature in
* Fahrenheit
*/
public static int convertToFahrenheit(int temp)
{
return (9 * temp)/ 5 + 32;
}
/**
* Converts from Fahrenheit
* to Celsius
* @param temp the temperature
* in Fahrenheit
* @return the temperature in Celsius
*/
public static int convertToCelsius(int temp)
{
return (temp - 32) / 9 * 5;
}
}
/*run:
Convert tmperature:
Convert from Fahrenheit to Celsius:
Convert from Celsius to Fahrenheit:
Enter 3 - Exit:*/
This post has been edited by gmillerlight: 07 June 2009 - 06:54 PM

Start a new topic
Add Reply




MultiQuote

| 


