C# double 值显示不正确的值

C# double value displaying incorrect value

您好,我正在练习 C$,特别是十进制数值。我有这个简单的程序只是为了检查输入的数据是否正确。我输入 12.9 但得到 49。任何帮助将不胜感激。

static void Main(string[] args)
        {
            Console.Write("Enter the first number 1: ");
            double num1 = Console.Read();
            Console.Write(num1);
            //Console.WriteLine(num1+" + "+num2+" + "+num3 + " = "+total);
        }

您需要使用 Console.ReadLine() 而不是 Console.Read()

Read method - Reads the next character from the standard input stream.

ReadLine method - Reads the next line of characters from the standard input stream.

试试这个:

static void Main(string[] args)
{
    Console.Write("Enter the first number 1: ");

    // It might cause an error if the user doesn't write a number 
    // which is parsable to double
    double num1 = double.Parse(Console.ReadLine());
    Console.Write(num1);
}

或者一个安全的方法:

static void Main(string[] args)
{
    Console.Write("Enter the first number 1: ");

    // You may also ask the user to write again an input of number.
    if (double.TryParse(Console.ReadLine(), out double num1))
    {
        Console.Write(num1);
    }
}