【发布时间】:2018-07-23 16:52:25
【问题描述】:
我正在创建一个程序,将华氏温度转换为摄氏温度,反之亦然。代码有效,但如何让我的textOut 在 C# 中使用 Console.WriteLine 显示在多列中?
这是我的代码(不包括 Program.cs 或 Input.cs)
namespace Assignment2
{
class TemperatureConverter
{
public void Start()
{
int choice = 0;
do
{
DisplayMenu();
choice = Input.ReadIntegerConsole(" Your selection: ");
switch (choice)
{
case 1:
CalculateFahrenheitToCelsius();
break;
case 2:
CalculateCelsiusToFahrenheit();
break;
case 0: //end session (exit loop)
break;
default:
Console.WriteLine("Invalid option. Choose between 0 and 2.");
break;
}
} while (choice != 0);
}
public void DisplayMenu()
{
Console.WriteLine();
Console.WriteLine("*************************************************");
Console.WriteLine(" MAIN MENU ");
Console.WriteLine("*************************************************");
Console.WriteLine(" Convert Fahrenheit to Celsius : 1");
Console.WriteLine(" Convert Celsius to Fahrenheit : 2");
Console.WriteLine(" Exit the Converter : 0");
}
public void CalculateFahrenheitToCelsius()
{
double convertedValue = 0;
string textOut = string.Empty;
for (int i = 0; i <= 212; i += 4)
{
convertedValue = FahrenheitToCelsius(i);
textOut = string.Format("{0,16:f2} F = {1,6:f2} C", i, convertedValue);
Console.WriteLine(textOut);
}
Console.WriteLine();
}
/// <summary>
/// Calculate Celsius to Fahrenheit
/// </summary>
public void CalculateCelsiusToFahrenheit()
{
double convertedValue = 0;
string textOut = string.Empty;
for (int i = 0; i <= 100; i += 5)
{
convertedValue = CelsiusToFahrenheit(i);
textOut = string.Format("{0,16:f2} C = {1,6:f2} F", i, convertedValue);
Console.WriteLine(textOut);
}
Console.WriteLine();
}
private double FahrenheitToCelsius(double celsius)
{
double fahrenheit = (9.0 / 5.0) * celsius + 32.0;
return fahrenheit;
}
private double CelsiusToFahrenheit(double fahrenheit)
{
double celsius = (5.0 / 9.0) * (fahrenheit - 32.0);
return celsius;
}
}
}
【问题讨论】:
-
学业? Assignment2 是命名空间???
-
@AnyMoose 是的,这是一个家庭作业,虽然把它放在列中不是作业的一部分,我只是好奇怎么做 :) (作业照原样完成)
-
您的华氏温度和摄氏温度应该已经在列中对齐。你能提供一个你希望你的输出是什么样子的例子吗?
-
@Dan- 不知道我怎么能告诉你我的意思(一旦我按下回车,cmets 会删除格式)......我只是希望它在两个三个单独的列中显示数据,以便用户不必向下滚动即可查看所有值
标签: c# .net multiple-columns console.writeline textout