import java.util.Scanner;
從策劃到設(shè)計制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁設(shè)計、主機(jī)域名、虛擬空間、網(wǎng)絡(luò)營銷、VI設(shè)計、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
public class Demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Point p1,p2;
System.out.println("請輸入第1個點(diǎn)的x、y坐標(biāo):");
p1=new Point(sc.nextDouble(),sc.nextDouble());
System.out.println("請輸入第2個點(diǎn)的x、y坐標(biāo):");
p2=new Point(sc.nextDouble(),sc.nextDouble());
System.out.println("點(diǎn)"+p1+"與點(diǎn)"+p2+"的距離是"+p1.distance(p2));
}
}
class Point
{
Point(double x,double y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return "("+x+","+y+")";
}
double distance(Point p)
{
return Math.sqrt(Math.pow(this.x-p.x,2)+Math.pow(this.y-p.y,2));
}
private double x,y;
}
import?java.awt.Point;
import?java.util.Scanner;
public?class?Test?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
System.out.println("請輸入有幾組:");
Scanner?scanner?=?new?Scanner(System.in);
int?groupCount?=?scanner.nextInt();
double?results[]?=?new?double[groupCount];
for?(int?i=0;igroupCount;i++)?{
System.out.println("請輸入第"+(i+1)?+?"組2點(diǎn)的坐標(biāo)(以,分隔):");
String?line?=?scanner.next();
String[]?values?=?line.split(",");
if?(values.length?!=?4)?{
System.out.println("輸入的數(shù)據(jù)格式不對!");
i?=?i--;
}
else?{
double?p1?=?Double.valueOf(values[0]);
double?p2?=?Double.valueOf(values[1]);
double?p3?=?Double.valueOf(values[2]);
double?p4?=?Double.valueOf(values[3]);
results[i]?=?getDistance(p1,?p2,?p3,?p4);
}
}
for?(int?i=0;iresults.length;i++)
System.out.println(results[i]);
}
public?static?double?getDistance(double?p1,double?p2,double?p3,double?p4)?{
double?d?=?0.0;
d?=?Point.distance(p1,?p2,?p3,?p4);
return?d;
}
}
兩維空間里,兩點(diǎn)間的距離公式是L = sqrt((x1-x2)^2+(y1-y2)^2);
多維空間的公式照推。
假設(shè)文件中數(shù)據(jù)如下:
3
2,5,6
6,8,5
則對應(yīng)的代碼如下:
BufferedReader reader = new BufferedReader(new FileReader("C:/test.txt"));
//獲取維數(shù)
String s1 = reader.readLine();
num = Integer.parseInt(s1);
//獲取第一個坐標(biāo)的信息,將其坐標(biāo)信息保存在數(shù)組中
String s2 = reader.readLine();
String[]str2 = s2.split(",");
int p1[] =new int[num];
for(int i=0;inum;i++) {
p1[i] = Integer.parseInt(str2[i]);
}
//獲取第二個坐標(biāo)的信息,同上,略過
……
//將對應(yīng)坐標(biāo)之差保存到另外一個數(shù)組
int a []=new int[num];
for(int i = 0;inum;i++)
a[i] = p1[i]-p2[i];
//求這個數(shù)組各元素的平方和
int sum = 0;
for(int i=0;inum;i++) {
sum += a[i]*a[i];
}
求距離
int L = Math.sqrt(sum);
以上代碼直接在瀏覽器中完成,可能會有編碼問題,樓主細(xì)察
import
java.util.Scanner;
public
class
TestObject
{
/**
*
@param
args
*/
public
static
void
main(String[]
args)
{
//
TODO
Auto-generated
method
stub
Scanner
in
=
new
Scanner(System.in);
System.out.println("請輸入第一個坐標(biāo)點(diǎn):");
int
x1
=
in.nextInt();
int
y1
=
in.nextInt();
System.out.println("請輸入第二個坐標(biāo)點(diǎn):");
int
x2
=
in.nextInt();
int
y2
=
in.nextInt();
int
distance
=
(int)
Math.sqrt(Math.abs((x1
-
x2)*(x1
-
x2))+Math.abs((y1
-
y2)*(y1
-
y2)));
System.out.println("兩點(diǎn)間距離是:"+distance);
}
}