C#需要转换帮助

C#需要转换帮助,c#,console,C#,Console,Line2D.cs namespace ConsoleApp1 { public class Line2D { public String name; // any name public Point2d p1; public Point2d p2; public Line2D(Point2d p1, Point2d p2, String name) { this.name = name; th

Line2D.cs

    namespace ConsoleApp1
    {
  public  class Line2D
    {
    public String name; // any name
    public Point2d p1;
    public Point2d p2;
    public Line2D(Point2d p1, Point2d p2, String name)
    {
        this.name = name;
        this.p1 = p1;
        this.p2 = p2;
    }
    public Point2d nearPoint(Point2d p)
    {
        double slope = (this.p2.y - this.p1.y);
        double coffX = slope;
        double coffY = -1;
        double const1= slope * this.p1.x - this.p1.y;

        //AX + BY=C
        //imaginary line perpendicular to line and passing through given point
        double slope1 = -1 * slope;
        double coffX1 = slope;
        double coffY1 = -1;
        double const2 = slope * this.p1.x - this.p1.y;
        double X = (const1 - const2) / (coffX - coffX1);
        double Y = coffX * X - const1;
        String Name = "nearPoint";
        Point2d P = new Point2d (X, Y,Name);
        return p;
    }
  }
 }
点2D.cs

    namespace ConsoleApp1
     {
    public double x;
    public double y;
    public String name;
    public Point2d(double x,double y,string name)
    {
        this.x = x;

        this.y = y;

        this.name = name;
    }
}
 }
Program.cs

     namespace ConsoleApp1
   {
    class Program
     {
    static void Main(string[] args)
      {
        Console.WriteLine("enter X cordinate of point:");
        double x = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("enter Y cordinate of point:");
        double y = Convert.ToDouble(Console.ReadLine());
        Point2d p = new Point2d(x,y,"nischal");
        Console.WriteLine("enter 1st point:"); //Line12
        Point2d p1 = Console.ReadLine();
        Console.WriteLine("enter 2nd point:"); //Line 14
        Point2d p2 = Console.ReadLine();
        Line2D l = new Line2D(p1,p2,"nischal"); // Line 16
    }
}
}


问题:我想创建Line2D的对象并调用其nearPoint方法。但是我被第12行、第14行和第16行卡住了。我希望用户输入数据类型p1和p2的值,即Point2d p1和Point2d p2数据类型,该数据类型在Line2D类上进行计算。但我在第12行和第14行转换时出错了。请一些专家帮我解决这个问题。这是C#console应用程序代码。

来自
console.ReadLine()
您会得到一个
字符串。我很困惑,你没有得到一个编译器错误。通常,您需要从字符串中解析点。你可以用几种方法来做。任何时候你需要一个函数,它接受一个字符串并返回一个Point2d

1) 在
点2D
中实现强制转换运算符。并从代码中调用它:

public class Point2d
{
    public static explicit operator Point2d(string stringRepresentation)
    {
        Point2d point;
        //parse point from string manually
        return d;
    }
}

static void Main (string[] args])
{
    Point2d myPoint = (Point2d) Console.ReadLine();
}
2) 实现一个接受字符串的构造函数

public class Point2d
{
    public Point2d(string stringRepresentation)
    {
        //parse point from string manually
    }
}

static void Main (string[] args])
{
    Point2d myPoint = new Point2d (Console.ReadLine());
}

还有其他可能的方法。但我认为这两个变量最合适。

您的
Point2d
需要两个double类型的变量和一个string类型的变量。因此,最简单的方法可能是要求用户单独输入这些值。由于
Console.ReadLine()
-方法以字符串形式返回用户输入,因此必须解析
Point2d.x
Point2d.y
的值。你可以这样做:

    static void Main(string[] args)
    {
        var p1x = RequestUserInput_Double("Enter 1st point x value:");
        var p1y = RequestUserInput_Double("Enter 1st point y value:");
        var p2x = RequestUserInput_Double("Enter 2nd point x value:");
        var p2y = RequestUserInput_Double("Enter 2nd point y value:");

        var p1 = new Point2d(p1x, p1y, "SomeName");
        var p2 = new Point2d(p2x, p2y, "SomeOtherName");
    }

    public static double RequestUserInput_Double(string requestString)
    {
        Console.WriteLine(requestString);
        var userInput = Console.ReadLine();
        double value;

        // Try to parse the user input into a double value
        while(!Double.TryParse(userInput, out value))
        {
            Console.WriteLine("Invalid format. Please enter a valid double value:");
            userInput = Console.ReadLine();
        }
        return value;
    }
如果愿意,您还可以要求用户输入点的名称(因为这些是字符串,所以您不需要强制转换它们)


当然,您可以对用户输入进行一些奇特的解析,这样就可以在一行中输入类似于
2.0 4.0 MyPoint1
的内容,但为了简单起见,我只需要逐个请求每个值。

ReadLine
为您提供一个
字符串,无法转换为
点2D

要使用字符串创建
Point2D
对象,需要解析字符串。首先,让我们决定用户输入点坐标的格式。我认为
(x,y)
格式是最方便用户使用的

要解析
(x,y)
,我们可以使用正则表达式

var userInput = Console.ReadLine();
var match = Regex.Match(userInput, @"\(([-\d.]+),\s+([-\d.]+)\)");
var x = Convert.ToInt32(match.Groups[1].Value);
var y = Convert.ToInt32(match.Groups[2].Value);
您可以将此逻辑放入一个方法中,并将该方法与
Main
放在一起:

private static Point2D Point2DFromString(string s, string name) {
    var userInput = Console.ReadLine();
    var match = Regex.Match(userInput, @"\(([-\d.]+),\s+([-\d.]+)\)");
    var x = Convert.ToInt32(match.Groups[1].Value);
    var y = Convert.ToInt32(match.Groups[2].Value);
    return new Point2D(x, y, name);
}
现在,您可以执行以下操作:

Point2d p1 = Point2DFromString(Console.ReadLine(), "p1");

您得到的是什么错误
Console.ReadLine()
为您提供了一个字符串,请解析该字符串以生成您的对象。您的代码无法工作var p2y=(double)Console.ReadLine();(错误:无法将字符串转换为双精度);var p2=新点2d(p2x,p2y,“其他名称”);(错误:无法将字符串转换为ConsoleApp1.Point2d);您的代码甚至没有运行。我想创建Line2D的对象(点p1,点p2,字符串名称),其中点p1,点p2类似于数据类型。对此表示抱歉。我忘了你不能在C#中把一个字符串抛给一个double。我更新了上面的示例,并添加了一个helper方法,对用户输入进行了一些简单的解析。这对我有用。如果对你不起作用,请告诉我。你还没有回答我的主要问题。我想创建Line2D的对象(点p1,点p2,字符串名称),其中点p1,点p2类似于数据类型。如何做到这一点?它可以工作,但在运行时输入我得到的值:“无法将类型为”System.Text.RegularExpressions.Group“的对象强制转换为类型为”System.IConvertible“。屏幕截图:@NischalPrajapati oops!打字错误!现在编辑“输入字符串格式不正确”错误我还说明了如何制作Line2D对象,我可以有完整的代码吗?