How to print accent on a Console Application project . NET Core

Asked

Viewed 423 times

3

using System;

namespace MediaDoisNumeros
{
    class Program
    {
        static void Main(string[] args)
        {
            Int16 numero1, numero2;
            Double media;

            Console.Write("Digite o numero 1 : ");
            numero1 = Convert.ToInt16(Console.ReadLine());
            Console.Write("Digite o numero 2 : ");
            numero2 = Convert.ToInt16(Console.ReadLine());

            media = (numero1 + numero2) / 2;

            Console.WriteLine("A média dos dois numeros é {0}", media);
            Console.ReadKey();
        }
    }
}

Printed output on console :

A m ®dia of the two numbers ® 5

How to make it printed correctly :

The average of the two numbers is 5

  • Welcome, take a [tour] on the site and see [Ask].

  • Apart from this code having some problems of style and robustness, what’s the problem? The média in the text is giving problem? What error pointed? Where is compiling? The file .cs is recorded in UTF-8 by the editor?

  • 1

    The robustness of the code is of no concern. The problem is that accented text is not displayed correctly. Yes The average is not displayed correctly. The error pointed out is how to use accentuation in a Console Application . NET Core project. Yes file . Cs is saved in Unicode ( UTF-8 with signature ) - code page 65001 by the editor.

  • O erro apontado é como utilizar acentuação em um projeto Console Application .NET Core This is not an error that the compiler points to. It just appears wrong? then the problem must be where it is being shown, as I do not know where it is compiling, running, I can not help.

  • 1

    I thank you for trying to understand the problem, but it seems to me that you are not concerned with helping, but rather that I ask the question according to your precepts. I’m sorry I don’t have your level of expertise to ask the question in a way you like.

  • 1

    @Ericcastro if I understand correctly, try to do the following: Console.Outputencoding = System.Text.Encoding.UTF8;

  • 1

    @miltoncamara thank you very much guy I just found this solution in stackoverflow in English I just tested worked even, and I saw your answer. Thanks! https://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console

Show 2 more comments

1 answer

3


To be able to print accents it is necessary to define the encoding of the output of the console, to this add Console.OutputEncoding = System.Text.Encoding.UTF8 at the beginning of your code.

Thus remaining:

using System;

namespace MediaDoisNumeros
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            Int16 numero1, numero2;
            Double media;

            Console.Write("Digite o numero 1 : ");
            numero1 = Convert.ToInt16(Console.ReadLine());
            Console.Write("Digite o numero 2 : ");
            numero2 = Convert.ToInt16(Console.ReadLine());

            media = (numero1 + numero2) / 2;

            Console.WriteLine("A média dos dois numeros é {0}", media);
            Console.ReadKey();
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.