我是C#的新手,所以如果我的代码格式错误,我会道歉。目前我正在尝试研究如何从计算数据距离的类中返回一个值。
这是我到目前为止所提出的,当我提出或者两点时,唯一出现的是DistanceGenerator.Coursework我看不出我做错了什么,有什么帮助吗?
namespace DistanceGenerator
{
class Program
{
static void Main(string[] args)
{
//Displays data in correct Format
List<float> inputList = new List<float>();
TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
String input = Convert.ToString(tr.ReadToEnd());
String[] items = input.Split(',');
Console.WriteLine("Point Latitude Longtitude Elevation");
for (int i = 0; i < items.Length; i++)
{
if (i % 3 == 0)
{
Console.Write((i / 3) + "\t\t");
}
Console.Write(items[i]);
Console.Write("\t\t");
if (((i - 2) % 3) == 0)
{
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine();
// Ask for two inputs from the user which is then converted into 6 floats and transfered in class Coordinates
bool exit = false;
do
{
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
string point = Console.ReadLine();
string[] pointInput = point.Split(' ');
int pointNumber = Convert.ToInt16(pointInput[0]);
int pointNumber2 = Convert.ToInt16(pointInput[1]);
double latitude = (Convert.ToDouble(items[pointNumber * 3]));
double longtitude = (Convert.ToDouble(items[(pointNumber * 3) + 1]));
double elevation = (Convert.ToDouble(items[(pointNumber * 3) + 2]));
double latitude2 = (Convert.ToDouble(items[pointNumber2 * 3]));
double longtitude2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 1]));
double elevation2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 2]));
//Calculate the distance between two points
Distance curDistance = new Distance(latitude, longtitude, elevation, latitude2, longtitude2, elevation2);
Console.WriteLine(curDistance);
Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");
string reset;
do
{
reset = Console.ReadLine().Trim();
}
while (reset != "1" && reset != "-1");
if (reset == "-1") exit = true;
}
while (!exit);
}
}
}
//这里是班级
namespace DistanceGenerator
{
class Distance
{
private double latitude;
private double longtitude;
private double elevation;
private double latitude2;
private double longtitude2;
private double elevation2;
public Distance(double latitude, double longtitude, double elevation, double latitude2, double longtitude2, double elevation2)
{
// TODO: Complete member initialization
this.latitude = latitude;
this.longtitude = longtitude;
this.elevation = elevation;
this.latitude2 = latitude2;
this.longtitude2 = longtitude2;
this.elevation2 = elevation2;
}
private double curDistance()
{
const double PIx = 3.141592653589793;
const double RADIO = 6371;
double dlat = ((latitude2) * (PIx / 180)) - ((latitude) * (PIx / 180));
double dlon = ((longtitude2) * (PIx / 180)) - ((longtitude) * (PIx / 180));
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos((latitude) * (PIx / 180)) * Math.Cos((latitude2) * (PIx / 180)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double ultimateDistance = (angle * RADIO);
return ultimateDistance;
}
}
}
答案 0 :(得分:6)
curDistance
方法中的 Main
是Distance
类的实例。
curDistnace
课程中的Distnace
方法为private
,使其无法在课堂外使用 - 您可以将其设为public
:
public double curDistance()
您不想输出类,而是输出计算的值。为此,您需要在curDistance
实例上调用方法 Distance
(您可能会混淆地将其命名为curDistance
):
Console.WriteLine(curDistance.curDistance());