viewing paste ConvertFahToCelMethod.java | Java

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 
Algorithm: Display Celsius
 
1. Ask the user to enter a temperature in fahrenheit
2. Convert Fahrenheit to Celsius
3. Ask user if they wish to continue
    3.1 If no close, if yes continue
4. Done
 
*/
 
import java.util.Scanner;
 
public  class   ConvertFahToCelMethod{
 
    public  static  void    main(String[] args){
        //Initialize cont to be a string, so y or n works
        String cont = new String("y");
 
        //Initiate scanner to be a scanner
        Scanner scanner = new Scanner(System.in);
 
        //loop until n is printed
        while(cont != "n"){
            //print text & get fahrenheit of the user
            System.out.print("Enter a temperature in Fahrenheit: ");
            double fahrenheit = scanner.nextInt();
            //Display result & text
            System.out.print("Your temperature in Celsius is: "+fahToCel(fahrenheit));
            //Continue or not? y = yes, n = no
            System.out.print("Do you want to continue? ");
            //Ask user if they wish to continue, n = no, y = yes
            cont = scanner.next();
            
        }
    }
    //A double  method named fahToCel 1 number received in the main function
    public static double fahToCel(double num1){
        //Do the math
        double celsius = (num1 - 32)*5/9;
        
        //Return the double value/result
        return celsius;
        
    }
}
Viewed 1192 times, submitted by Streusel.