Advertisement
LeeMace

Temperature Converter

May 12th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | Gaming | 0 0
  1. public static class TemperatureConverter {
  2.     public static double CelsiusToFahrenheit(string temperatureCelsius) {
  3.         // Convert argument to double for calculations.
  4.         double celsius = double.Parse(temperatureCelsius);
  5.  
  6.         // Convert Celsius to Fahrenheit.
  7.         double fahrenheit = (celsius * 9 / 5) + 32;
  8.  
  9.         return fahrenheit;
  10.     }
  11.  
  12.     public static double FahrenheitToCelsius(string temperatureFahrenheit) {
  13.         // Convert argument to double for calculations.
  14.         double fahrenheit = double.Parse(temperatureFahrenheit);
  15.  
  16.         // Convert Fahrenheit to Celsius.
  17.         double celsius = (fahrenheit - 32) * 5 / 9;
  18.  
  19.         return celsius;
  20.     }
  21. }
  22.  
  23.  
  24. public class TestTemperatureConverter : MonoBehaviour
  25. {
  26.     // Start is called before the first frame update
  27.     void Start()
  28.     {
  29.         Debug.Log("Please select the convertor direction");
  30.         Debug.Log("1. From Celsius to Fahrenheit.");
  31.         Debug.Log("2. From Fahrenheit to Celsius.");
  32.         Debug.Log(":");
  33.  
  34.         string selection = Input.inputString;
  35.         double F = 0, C = 0;
  36.  
  37.         switch (selection) {
  38.             case "1":
  39.                 Debug.Log("Please enter the Celsius temperature: ");
  40.                 F = TemperatureConverter.CelsiusToFahrenheit(Input.inputString);
  41.                 Debug.Log("Temperature in Fahrenheit: " + F.ToString("F2"));
  42.                 break;
  43.  
  44.             case "2":
  45.                 Debug.Log("Please enter the Fahrenheit temperature: ");
  46.                 C = TemperatureConverter.FahrenheitToCelsius(Input.inputString);
  47.                 Debug.Log("Temperature in Celsius: " + C.ToString("F2"));
  48.                 break;
  49.  
  50.             default:
  51.                 Debug.Log("Please select a convertor.");
  52.                 break;
  53.         }
  54.                 // Keep the console window open in debug mode.
  55.                 Debug.Log("Press any key to exit.");
  56.         }
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement