程序输出结果不对啊

DaiwjDev 2013-05-31 11:14:11

import java.util.Scanner;
public class FindNearestPoints{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the numbers of points: ");
int numberOfPoints = input.nextInt();

double[][] points = new double[numberOfPoints][2];
System.out.print("Enter " + numberOfPoints + " points: ");
for(int i = 0; i < points.length; i++){
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
}

int p1 = 0, p2 = 0;
double shortestDistance = distance(points[p1][0], points[p1][1], points[p2][0], points[p2][1]);

for(int i = 0; i < points.length; i++){
for(int j = i + 1; j < points.length; j++){
double distance = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
if(shortestDistance > distance){
p1 = i;
p2 = j;
shortestDistance = distance;
}
}
}

System.out.println("The closest two points are "
+ "(" + points[p1][0] + "," + points[p1][1] + ") and (" + points[p2][0] + "," + points[p2][1] + ")");
}

public static double distance(double x1, double y1, double x2, double y2){
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
}

dai@ubuntu:~/Programs/java$ javac FindNearestPoints.java
dai@ubuntu:~/Programs/java$ java FindNearestPoints
Enter the numbers of points: 3
Enter 3 points: 1 1 4 -3 4 9
The closest two points are (1.0,1.0) and (1.0,1.0)
...全文
130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DaiwjDev 2013-05-31
  • 打赏
  • 举报
回复
难道要沉了吗?
DaiwjDev 2013-05-31
  • 打赏
  • 举报
回复
谢谢楼上各位,看到错误啦!!!
地下室森林 2013-05-31
  • 打赏
  • 举报
回复
那个shortestDistance变成求自身的,所以是最短距离了。要控制程序比较的是两个不同的点坐标
Mourinho 2013-05-31
  • 打赏
  • 举报
回复

int p1 = 0, p2 = 1;
抱抱枯叶 2013-05-31
  • 打赏
  • 举报
回复
你的 int p1 = 0, p2 = 0; double shortestDistance = distance(points[p1][0], points[p1][1], points[p2][0], points[p2][1]); 你p1=p2=0 然后shortestDistance得出的值是0 你觉得你下面在怎么算能算出小于0的值?最总结果肯定就是 (1,1)
微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 微信小程序系统教程共有“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]——核心技术”、“微信小程序系统教程[阶段]客服消息+微信支付+九宝电商系统”。 “微信小程序系统教程[阶段]全套课程”包含: 1.微信小程序系统教程[阶段]_客服消息 2.微信小程序系统教程[阶段]_微信支付 3.微信小程序系统教程[阶段]_九宝电商系统 学习“微信小程序系统教程[阶段]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]”,后在报名“微信小程序系统教程[阶段]”。 阶段讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,没有使用三方技术。这样降低同学们学习成本。 微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。 九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。 “微信小程序系统教程[中级阶段]——核心技术”重点讲解微信小程序事件、组件、API 微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。 购课请咨询qq2326321088

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧