我已经为一个小程序写了一些c#代码(华氏度到摄氏度和副Versa),但是我想在答案中添加输入的数量。
我希望读取/显示结果: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();
}
}
}
答案 0 :(得分:1)
修改格式字符串以包含两个格式值,一个用于输入,另一个用于结果:
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees. ", fah, FtoC);
Console.WriteLine("{0} Degrees Celsius is {1} degrees Fahrenheit. ", cel, CtoF);
答案 1 :(得分:0)
您似乎已经知道如何使用一个格式化占位符打印字符串:
Console.WriteLine("a is equal to {0}.", a);
只需添加另一个占位符和变量,就可以将它扩展为两个:
Console.WriteLine("a is equal to {0} and b is equal to {1}.", a, b);
答案 2 :(得分:0)
我想你差不多了。
Console.WriteLine("{0} Degrees Fahrenheit is {1} Celsius degrees.", fah, FtoC);
您是否也将输入变量放在同一台Console.WriteLine中,但作为第一个参数?
答案 3 :(得分:0)
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();
}
}
}
答案 4 :(得分:0)
我建议学习如何直接封装你的规则/逻辑。
您的“迫切需要”答案在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);