簡體   English   中英

為什么我的代碼說我正在嘗試將十進制轉換為雙精度,我該如何解決?

[英]Why does my code say that I'm trying to convert decimal to double and how can I fix it?

我想運行這個程序,但它不會讓我運行,因為我收到一條錯誤消息,說我正在嘗試將“十進制”轉換為“雙精度”。 我不明白為什么。 我也不知道如何解決這個問題。

class Program
{
    static void Main(string[] args)
    {
        string inputString = Console.ReadLine();
        decimal[] hvValues = inputString.Split().Select(decimal.Parse).ToArray();

        decimal sinTest = hvValues[1];
        decimal sinV = Math.Sin(sinTest);
        decimal hypotenusan = hvValues[0] / sinV;
        Console.WriteLine(Math.Ceiling(hypotenusan));
    }
}

為簡單起見,由於您似乎是編程新手,只需使用 double 而不是decimal:

class Program
{
    static void Main(string[] args)
    {
        string inputString = Console.ReadLine();
        double[] hvValues = inputString.Split().Select(double.Parse).ToArray();

        double sinTest = hvValues[1];
        double sinV = Math.Sin(sinTest);
        double hypotenusan = hvValues[0] / sinV;
        Console.WriteLine(Math.Ceiling(hypotenusan));
    }
}

這將使它起作用,但是在某些時候您應該嘗試了解doubledecimal之間的區別是什么。 它還有助於您了解編譯器錯誤以及為什么您的 IDE 在代碼的某些部分下放置紅色下划線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM