Last active
September 13, 2018 16:12
-
-
Save feehe21/932b62543eb853c047386fb48cb2397f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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