[英]c# fahrenheit and celsius: adding input to answer
我已經為一個小程序(攝氏溫度和反之亦然)編寫了一些C#代碼,但是我想在答案中加上輸入的數字。
我想讀取/顯示結果:178華氏度是81攝氏度。
碼。
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();
}
}
}
修改格式字符串以包括兩個格式值,一個用於輸入,一個用於結果:
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees. ", fah, FtoC);
Console.WriteLine("{0} Degrees Celsius is {1} degrees Fahrenheit. ", cel, CtoF);
您似乎已經想出了如何使用一種格式的占位符打印字符串:
Console.WriteLine("a is equal to {0}.", a);
您可以通過添加另一個占位符和變量來將其擴展為兩個:
Console.WriteLine("a is equal to {0} and b is equal to {1}.", a, b);
我想你差不多了。
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees.", fah, FtoC);
您不只是將輸入變量也輸入到同一Console.WriteLine中,而是將其作為第一個參數嗎?
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();
}
}
}
我建議您立即學習如何封裝您的規則/邏輯。
您的“緊急需求”答案在ToString實現中。
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;
}
}
}
和調用代碼
static void Main(string[] args)
{
try
{
CeliusToFahrentheit ctf = new CeliusToFahrentheit();
ctf.Celius = 100;
Console.WriteLine(ctf.Fahrentheit);
string mesg = ctf.ToString();
Console.WriteLine(mesg);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.