将Double转换为Bool

时间:2015-01-19 15:55:14

标签: c# geometry

我试图找出给定的点是否在一个圆圈内,这是我当下的解决方案

using System;

//Write an expression that checks if given point (x, y) is inside a circle K({0, 0}, 2).

class Program
{
    static void Main()
    {
        Console.Write("Enter x: ");
        double x = double.Parse(Console.ReadLine());
        Console.Write("Enter y: ");
        double y = double.Parse(Console.ReadLine());
        bool insideCircle = (double)Math.Sqrt((x * x) + (y + y) <= 2);

    }
}

我收到错误,我无法从bool转换为double。有人能帮助我吗?

3 个答案:

答案 0 :(得分:2)

你的括号在错误的地方。

bool insideCircle = Math.Sqrt((x * x) + (y + y)) <= 2.0;

答案 1 :(得分:1)

您应该将<= 2移到Math.Sqrt()之外。

        Console.Write("Enter x: ");
        double x = double.Parse(Console.ReadLine());
        Console.Write("Enter y: ");
        double y = double.Parse(Console.ReadLine());
        bool insideCircle = Math.Sqrt((x * x) + (y + y)) <= 2;

答案 2 :(得分:0)

我相信你的意思是:

class Program
{
    static void Main()
    {
        Console.Write("Enter x: ");
        double x = double.Parse(Console.ReadLine());
        Console.Write("Enter y: ");
        double y = double.Parse(Console.ReadLine());

        // <= 2 is outside the brackets, not inside
        bool insideCircle = Math.Sqrt((x * x) + (y + y)) <= 2;
    }
}