My 1st C# program
I am just starting out learning C# as my first programming language, I started learning about a week ago. This is my first program I have written. It will take user input to convert temperature values. Let me know what you guys think and any suggestions/tips are appreciated!
Here is an example of what the program outputs when ran:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter a value to be converted
25
Please specify if your value inputed is Celcius with a 'c', Fahrenheit with a 'f' and Kelvin with a 'k'
c
25 Celsius is equal to 298.15 Kelvin.
25 Celcius is equal to 77 Fahrenheit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
----------------------------------------------------------------------------------------------------------------------------------------------
using System;
namespace Learning_2
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a value to be converted");
int numValue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please specify if your value inputed is Celcius with a 'c', Fahrenhite with a 'f' and Kelvin with a 'k'");
string temperatureScale = Console.ReadLine().ToLower();
//convert Celcius to Kelvin and Farhenheit
if (temperatureScale.Equals("c"))
{
Console.WriteLine("{0} Celsius is equal to {1} Kelvin. \n{0} Celsius is equal to {2} Fahrenheit.", numValue, Celsius_Kelvin(ref numValue, ref temperatureScale), Farhenheit_Celsius(ref numValue, ref temperatureScale));
}
// Convert Kelven to Celsius and Farhenheit
if (temperatureScale.Equals("k"))
{
Console.WriteLine("{0} Kelvin is equal to {1} Celsius. \n{0} Kelvin is equal to {2} Fahrenheit.", numValue, Celsius_Kelvin(ref numValue, ref temperatureScale), Farhenheit_Kelvin(ref numValue, ref temperatureScale));
}
//Convert Fahrenheit to Celsius and Kelvin
if (temperatureScale.Equals("f"))
{
Console.WriteLine("{0} Fahrenheit is equal to {1} Celsius. \n{0} Fahrenheit is equal to {2} Kelvin.", numValue, Farhenheit_Celsius(ref numValue, ref temperatureScale), Farhenheit_Kelvin(ref numValue, ref temperatureScale));
}
}
// Methods
// Convert Celsius and Kelvin back and fourth
static ValueType Celsius_Kelvin(ref int numValue, ref string temperatureScale)
{
if (temperatureScale.Equals("c")) //Celsius to Kelvin
{
double celsiusToKelvin = numValue + 273.15;
return celsiusToKelvin;
}
if (temperatureScale.Equals("k")) //Kelvin to Celsius
{
double kelvinToCelsius = numValue - 273.15;
return kelvinToCelsius;
}
return null;
}
//Convert Farhenheit and Celsius back and fourth
static ValueType Farhenheit_Celsius(ref int numValue, ref string temperatureScale)
{
if (temperatureScale.Equals("c")) //Celsius to Fahrenheit
{
double celsiusToFarhenheit = (numValue * 9 / 5) + 32;
return celsiusToFarhenheit;
}
if (temperatureScale.Equals("f")) //Fahrenheit to Celsius
{
double celsiusToFarhenheit = (numValue - 32) * 5 / 9;
return celsiusToFarhenheit;
}
return null;
}
//Convert Kelvin and Fahrenheit back and fourth
static ValueType Farhenheit_Kelvin(ref int numValue, ref string temperatureScale)
{
if (temperatureScale.Equals("f")) //Fahrenheit to Kelvin
{
int farhenheitToKelvin = (numValue * 9 / 5) + 32;
return farhenheitToKelvin;
}
if (temperatureScale.Equals("k")) //Kelvin Fahrenheit
{
double kelvinToFarhenheit = (numValue - 273.15) * 9 / 5 + 32;
return kelvinToFarhenheit;
}
return null;
}
}
}
Posted Dec 6, 2018, 1:03 AM UTC · edited