Skip to content

Instantly share code, notes, and snippets.

@feehe21
Last active September 13, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/932b62543eb853c047386fb48cb2397f to your computer and use it in GitHub Desktop.
Save feehe21/932b62543eb853c047386fb48cb2397f to your computer and use it in GitHub Desktop.
import java.util.*;
public class tempConverter
{
public static void main(String args[]){
//initialze variables
double startTemp = 0;
int whichConversion;
double endTemp = 0;
String endUnit = "no unit";
int keepGoing = 1;
Scanner scan = new Scanner(System.in);
while(keepGoing == 1){
//ask for temperature and specify conversion
System.out.println("Enter a temperature");
startTemp = scan.nextInt();
System.out.println("For Celsius to Fahrenheit, enter 1");
System.out.println("For Fahrenheit to Celsius, enter 2.");
whichConversion = scan.nextInt();
if(whichConversion == 1){//C to F
//(9/5 * C) + 32 = F
endTemp = (9/5.0) * startTemp + 32;
endUnit = "Fahrenheit";
}else if(whichConversion == 2){//F to C
//(F – 32) * 5/9 = C
endTemp = (startTemp - 32) * (5.0/9);
endUnit = "Celsius";
}
endTemp = (int)(10 * endTemp)/10.0;//round to nearest tenth
//return results
System.out.println("Your converted temperature:");
System.out.println(endTemp + " degrees " + endUnit);
//restart/finish
System.out.println("Press 1 for a new conversion, press 2 to end");
keepGoing = scan.nextInt();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment