需要 C# 转换帮助

标签 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;
        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;
    }
  }
 }

点二维.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;
    }
}
 }

程序.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.ReadLine() 你得到一个 string。我很困惑你没有得到编译器错误。通常,您需要从字符串中解析点。您可以通过多种方式做到这一点。任何时候你需要一个接受字符串并返回 Point2d 的函数

1) 在您的 Point2D 中实现一个转换运算符。并从您的代码中调用它:

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());
}

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

关于需要 C# 转换帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47807135/

相关文章:

c# - 我的注册 View 没有发布输入

javascript - 如何从控制台中删除 pixi.js 横幅?

delphi - 在控制台应用程序中屏蔽密码输入

c# - 将第二个参数传递给函数

c# - 这个 "tool"是什么以及如何禁用它?

c# - 在 C# 中将 EF 对象作为普通数组返回

linux - 程序显示控制台输出,即使 stdout 和 stderr 被重定向

iphone - SQLite3 数据库未通过 NSLog 在控制台上显示条目(将数据保存到数据库有效)

javascript - Chrome 中是否有用于将光标重新聚焦到控制台内的键盘快捷键?

c# - 创建一个没有设计器的 winform 用户控件