Reputation: 3
I have written some c# code for a small program (fahrenheit to celsius and Vice Versa), but I would like to add the number of the input to the answer.
I would like the result to read/show: 178 degrees fahrenheit is 81 celsius degrees.
Code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FahrentheitTest
{
class Program
{
static void Main(string[] args)
{
//conversion from Fahrenheit to Celsius
Console.WriteLine("Input temperature value to view Fahrenheit to Celsius: ");
int fah = int.Parse(Console.ReadLine());
Console.WriteLine();
//conservsion formula from F to C
int FtoC = ((fah - 32) * 5) / 9;
Console.WriteLine("Degrees Fahrenheit is {0} Celsius degrees. ", FtoC);
Console.WriteLine();
//conversion from Celsius to fahrenheit
Console.WriteLine("Input temperature value to view Celsius to Fahrenheit: ");
int cel = int.Parse(Console.ReadLine());
Console.WriteLine();
//conversion formula from C to F
int CtoF = ((cel * 9) /5) + 32;
Console.WriteLine("Degrees Celsius is {0} degrees Fahrenheit. ", CtoF);
Console.WriteLine();
}
}
}
Upvotes: 0
Views: 1152
Reputation: 27862
I would suggest learning how to encapsulate your rules/logic right off the bat.
Your "immediate need" answer is in the ToString implementation.
namespace Weather
{
using System;
public class CeliusToFahrentheit
{
public int? Celius { get; set; }
public int? Fahrentheit
{
get
{
int? returnValue = null;
if (this.Celius.HasValue)
{
returnValue = ((this.Celius * 9) / 5) + 32;
}
return returnValue;
}
}
public override string ToString()
{
string returnValue = string.Empty;
if (this.Celius.HasValue)
{
returnValue = string.Format("{0} degrees celsius is {1} fahrenheit degrees.", this.Celius.Value, this.Fahrentheit.Value);
}
return returnValue;
}
}
}
and calling code
static void Main(string[] args)
{
try
{
CeliusToFahrentheit ctf = new CeliusToFahrentheit();
ctf.Celius = 100;
Console.WriteLine(ctf.Fahrentheit);
string mesg = ctf.ToString();
Console.WriteLine(mesg);
Upvotes: 0
Reputation: 2631
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FahrentheitTest
{
class Program
{
static void Main(string[] args)
{
//conversion from Fahrenheit to Celsius
Console.WriteLine("Input temperature value to view Fahrenheit to Celsius: ");
int fah = int.Parse(Console.ReadLine());
Console.WriteLine();
//conservsion formula from F to C
int FtoC = ((fah - 32) * 5) / 9;
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees. ", fah, FtoC);
Console.WriteLine();
//conversion from Celsius to fahrenheit
Console.WriteLine("Input temperature value to view Celsius to Fahrenheit: ");
int cel = int.Parse(Console.ReadLine());
Console.WriteLine();
//conversion formula from C to F
int CtoF = ((cel * 9) /5) + 32;
Console.WriteLine("{0} Degrees Celsius is {1} degrees Fahrenheit. " , cel, CtoF);
Console.WriteLine();
}
}
}
Upvotes: 0
Reputation: 9
I think you almost got it.
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees.", fah, FtoC);
Wouldn't you just also have the input variable into the same Console.WriteLine, but as the first parameter?
Upvotes: 0
Reputation: 65126
You seem to already have figured out how to print a string with one formatting placeholder:
Console.WriteLine("a is equal to {0}.", a);
You can extend it to two by just adding another placeholder and variable like this:
Console.WriteLine("a is equal to {0} and b is equal to {1}.", a, b);
Upvotes: 0
Reputation: 6356
Modify your format string to include two format values, one for the input and one for the result:
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees. ", fah, FtoC);
Console.WriteLine("{0} Degrees Celsius is {1} degrees Fahrenheit. ", cel, CtoF);
Upvotes: 1