/**
* Convert from Farenheit to Celsius, or from Celsius to Farenheit
*
* @author Sean Appaneal
* @version v1.0
*/
import java.util.*;
public class TempConverter{
public static void main(String args[]){
//loop the program
int k = 1;
while(k == 1){
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a temperature: ");
double temp = sc.nextDouble();
//get the temp they want to convert
System.out.println("Press '1' for farenheit to celsius, or '2' for celsius to farenheit");
int unit = sc.nextInt();
//get the direction they want to convert from
double convTemp = 0;
if(unit == 1){
//Convert from F to C
convTemp = (temp - 32) * 5/9;
//Print converted temp
System.out.println("Converted Temperature is: "+ convTemp +"ºC");
}
if(unit == 2){
//convert from C to F
convTemp = (9.0/5 * temp) + 32;
//Print converted temp
System.out.println("Converted Temperature is: "+ convTemp +"ºF");
}
//print a blank line before next line
System.out.println(" ");
//ask if they want to continue working or stop
System.out.println("To continue, press 1, to stop, press 0 ");
//take int for continue or stop. 0=stop, 1=continue
k = sc.nextInt();
}
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.