使用C#Console.WriteLine在多列中显示textOut

时间:2018-07-23 16:52:25

标签: c# .net multiple-columns console.writeline textout

我正在创建一个程序,它将华氏温度转换为摄氏温度,反之亦然。该代码有效,但是如何在C#中使用textOut使Console.WriteLine显示在多列中?

这是我的代码(不包括Program.csInput.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;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

如果您要做的只是直接写行,则可以用以下两种方法之一创建列:

A)包括标签。

Console.Writeline("\tColumn1\tColumn2\tColumn3");

B)如果您的控制台使用的是等宽字体,则可以计算字符。但这将是脖子上的痛苦。