Не могу перенести строчки в середину консоли
Не могу понять, почему вывод не запускается посередине.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Записная_книжка
{
class Program
{
static void Main(string[] args)
{
string name;
byte Age;
double average, rus, history, math;
ushort height;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Имя");
name = (Console.ReadLine());
Console.WriteLine("Возраст");
Age = byte.Parse(Console.ReadLine());
Console.WriteLine("Рост");
height = ushort.Parse(Console.ReadLine());
Console.WriteLine("История");
history = double.Parse(Console.ReadLine());
Console.WriteLine("Математика");
math = double.Parse(Console.ReadLine());
Console.WriteLine("Русский");
rus = double.Parse(Console.ReadLine());
average = (rus + history + math) / 3;
string result2 = String.Format("{0:f2}", average);
Console.WriteLine("{0,50}",$"Имя: {name}\nРост: {height} сантиметра\nВозраст: {Age}\nИмея средний балл: {result2} ");
Console.ReadKey();
}
}
}
Ответы (1 шт):
Автор решения: Null Pointer Exception
→ Ссылка
Вывод строки по середине
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleProject
{
class Program
{
static void WriteLineCentered(string text)
{
int width = Console.WindowWidth;
if (text.Length < width)
{
text = text.PadLeft((width - text.Length) / 2 + text.Length, ' ');
}
Console.WriteLine(text);
}
static void Main(string[] args)
{
WriteLineCentered("Текст");
}
}
}
Ваш код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Записная_книжка
{
class Program
{
static void WriteLineCentered(string text)
{
int width = Console.WindowWidth;
if (text.Length < width)
{
text = text.PadLeft((width - text.Length) / 2 + text.Length, ' ');
}
Console.WriteLine(text);
}
static void Main(string[] args)
{
string name;
byte Age;
double average, rus, history, math;
ushort height;
Console.ForegroundColor = ConsoleColor.Red;
WriteLineCentered("Имя");
name = (Console.ReadLine());
WriteLineCentered("Возраст");
Age = byte.Parse(Console.ReadLine());
WriteLineCentered("Рост");
height = ushort.Parse(Console.ReadLine());
WriteLineCentered("История");
history = double.Parse(Console.ReadLine());
WriteLineCentered("Математика");
math = double.Parse(Console.ReadLine());
WriteLineCentered("Русский");
rus = double.Parse(Console.ReadLine());
average = (rus + history + math) / 3;
string result2 = String.Format("{0:f2}", average);
WriteLineCentered($"Имя: {name}");
WriteLineCentered($"Рост: {height} сантиметра");
WriteLineCentered($"Возраст: {Age}");
WriteLineCentered($"Имея средний балл: {result2} ");
Console.ReadKey();
}
}
}