Skip to content

Instantly share code, notes, and snippets.

@QuanSai
Created October 29, 2012 09:33
Show Gist options
  • Save QuanSai/3972608 to your computer and use it in GitHub Desktop.
Save QuanSai/3972608 to your computer and use it in GitHub Desktop.
Converts Fahrenheit to Celsius and vise versa. Wrote it to help my girl.
import java.util.*;
public class Convert
{
public static void main(String[] args)
{
Scanner inChoice = new Scanner(System.in), //input object for choice
inF = new Scanner(System.in), //input object for F
inC = new Scanner(System.in); //input object for C
float f, c; //floats for f and c
int choice; //integer representing the conversion choice
/* Have the user choose to see if they want F to C or C to F */
System.out.printf("-Choices-\n1 = F to C\n2 = C to F\nWhat's your choice? ");
choice = inChoice.nextInt();
/* switch the state of your program based on the user's choice */
switch(choice)
{
case 1: //if the choice is 1
System.out.println("What's F?");
f = inF.nextFloat();
c = (f - 32.0f)*5.0f/9.0f;
System.out.printf("That's %.2f Celsius.\n", c);
break;
case 2: //if the choice is 2
System.out.println("What's C?");
c = inC.nextFloat();
f = (c * 9.0f)/5.0f + 32;
System.out.printf("That's %.2f Fahrenheit.\n", f);
break;
default: //no choice
System.out.println("!Error: That choice doesn't exist!");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment