将 Double 转换为 Bool
Converting Double to Bool
我正在尝试查找给定点是否在圆内,这是我目前的解决方案
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 的错误。有人可以帮助我吗?
您应该将 <= 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;
你的括号放错地方了。
bool insideCircle = Math.Sqrt((x * x) + (y + y)) <= 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;
}
}
我正在尝试查找给定点是否在圆内,这是我目前的解决方案
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 的错误。有人可以帮助我吗?
您应该将 <= 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;
你的括号放错地方了。
bool insideCircle = Math.Sqrt((x * x) + (y + y)) <= 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;
}
}