.编写一个控制台应用程序,构建一个表示平面点坐标的类Point,它提供两个公有的构造函数,一个没有参数的Point构造函数和一个有两个double参数的构造函数。另外在该类中提供一个静态方法计算两个点之间的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
    public class Point {
        double x;
        double y;
        
        public Point() {
         x = 0;
         y = 0;
        }        public Point(double a, double b) {
            x = a;
            y = b;
        }}
    class Program
    {
        static void Main(string[] args)
        {
            double a,b,c,d=0;
            Console.WriteLine("请输入第一个点的坐标:");
            
            a = Double.Parse(Console.ReadLine());
            b = Double.Parse(Console.ReadLine());
            Console.WriteLine("请输入第二个点的坐标:");
            c = Double.Parse(Console.ReadLine());
            d = Double.Parse(Console.ReadLine());
            Point test1 = new Point(a,b);
            Point test2 = new Point(c,d);
                      
            Console.ReadLine();
            
        }
        public static void Calculate(Point a, Point b)
        {
            double length;
            double lengthF;
            double xC;
            double yC;
            xC = a.x - b.x;
            yC = a.y - b.y;
            lengthF = Math.Pow(xC, 2.0) + Math.Pow(yC, 2.0);
            length = Math.Sqrt(lengthF);
            Console.WriteLine(length);
            Console.ReadLine();        }
    }
}
产生错误:
错误 2 “ConsoleApplication1.Point.x”不可访问,因为它受保护级别限制 D:\ks\ConsoleApplication1\ConsoleApplication1\Program.cs 47 26 ConsoleApplication1
求大师帮忙!
c#控制台应用程序namespace

解决方案 »

  1.   

     public double x;
     public double y;
      

  2.   


                try
                {
                    a = Convert.ToDouble(Console.ReadLine());
                    b = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("请输入第二个点的坐标:");
                    c = Convert.ToDouble(Console.ReadLine());
                    d = Convert.ToDouble(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
      

  3.   

    你的问题是输入的字符串格式不正确,相当于你要把"abc"转换成一个double(双精度浮点型)类型的值肯定是不行的,输入对就没事
      

  4.   

    Console.ReadLine()是读取输入的一整行
    你是连接两行输入的数字吗?
      

  5.   

    谢谢,输入坐标可以了,可是没有显示结果,就是两个坐标相加的结果,直接按enter就退出命令行了,为什么没有结果出来呢?
      

  6.   

    Calculate这个方法你有地方调用么?
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        public class Point
        {
            public double x;
            public double y;        public Point()
            {
                x = 0;
                y = 0;
            }        public Point(double a, double b)
            {
                x = a;
                y = b;
            }    }
        class Program
        {
            static void Main(string[] args)
            {
                double a, b, c, d = 0;
                try
                {
                    Console.WriteLine("请输入第一个点的坐标:");
                    a = Double.Parse(Console.ReadLine());
                    b = Double.Parse(Console.ReadLine());
                    Console.WriteLine("请输入第二个点的坐标:");
                    c = Double.Parse(Console.ReadLine());
                    d = Double.Parse(Console.ReadLine());
                    Point test1 = new Point(a, b);
                    Point test2 = new Point(c, d);
                    Calculate(test1, test2);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
            public static void Calculate(Point a, Point b)
            {
                double length;
                double lengthF;
                double xC;
                double yC;
                xC = a.x - b.x;
                yC = a.y - b.y;
                lengthF = Math.Pow(xC, 2.0) + Math.Pow(yC, 2.0);
                length = Math.Sqrt(lengthF);
                Console.WriteLine(length);
            }
        }
    }