Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TempConverter {
- public static void main(String[] args) {
- boolean running = true;
- while(running) {
- Scanner sc = new Scanner(System.in);
- boolean understands = false;
- // My menu
- System.out.println("Temperature Converter");
- System.out.println("====================================");
- System.out.println("");
- System.out.println("\t1) Fahrenheit to Celsius");
- System.out.println("\t2) Celsius to Fahrenheit");
- System.out.println("");
- System.out.println("====================================");
- // The user's choice
- int input = sc.nextInt();
- String buff = sc.nextLine();
- // If the user wants to convert F to C
- if(input == 1) {
- System.out.print("Please enter your temperature in Fahrenheit: ");
- double tempInF = sc.nextDouble();
- String buffer = sc.nextLine();
- tempInF = (tempInF - 32) * 5/9.0;
- System.out.println("Your temperature in Celsius: " + tempInF);
- // It just looks better with this
- try {
- Thread.sleep(1200);
- } catch(InterruptedException e) {
- e.getStackTrace();
- }
- }
- // If the user wants to convert from C to F
- if(input == 2) {
- System.out.print("Please enter your temperature in Celsius: ");
- double tempInC = sc.nextDouble();
- String buffer = sc.nextLine();
- tempInC = (tempInC * 9.0/5) + 32;
- System.out.println("Your temperature in Fahrenheit: " + tempInC);
- // Again, it just looks better
- try {
- Thread.sleep(1200);
- } catch(InterruptedException e) {
- e.getStackTrace();
- }
- }
- // If the user tryna be cool and not enter in one of the options
- if(input != 1 && input != 2) {
- System.out.println("That is not an option! Please choose either option 1 or 2!");
- }
- // If the user puts in something nonsensical, then the computer will re-ask him or her
- while(!understands) {
- System.out.println("Do you want to input another temperature?");
- String yOrN = sc.nextLine();
- if(yOrN.equals("y") || yOrN.equals("yes") || yOrN.equals("Y") || yOrN.equals("YES") || yOrN.equals("Yes")) {
- System.out.println("");
- understands = true;
- }
- else if(yOrN.equals("n") || yOrN.equals("no") || yOrN.equals("N") || yOrN.equals("NO") || yOrN.equals("No")) {
- System.out.println("Program is closing. Thank you for using Temperature Calculator.");
- running = false;
- understands = true;
- sc.close();
- }
- else {
- System.out.println("Sorry, I didn't understand that.");
- System.out.println("Please type what you mean in a different way.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement