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方法。但是我遇到了Line 12 Line 14 16.我希望用户输入数据类型的p1和p2的值,这是Point2d p1和Point2d p2数据类型,它们在Line2D类上进行计算。但是我在第12行和第14行的转换过程中遇到错误。请一些专家帮我解决这个问题。它是C#控制台应用程序代码。
答案 0 :(得分:1)
从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());
}
还有其他方法可行。但我认为这两个是最合适的。
答案 1 :(得分:0)
你的Point2d
需要两个double类型的变量和一个string类型的变量。因此,最简单的方法可能是要求用户单独输入这些值。由于Console.ReadLine()
- Method将用户输入作为字符串返回,因此您必须解析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
的内容,但为了简单起见,我只想单独询问每个值
答案 2 :(得分:0)
ReadLine
为您提供string
,无法将其转换为Point2D
。
要使用字符串创建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");