Java确定最低点
我有一个叫做point的类和一个演示类。我试图比较我传入demo类的每个x,y点的y值,但我似乎无法集中精力从每个点检索y值Java确定最低点,java,point,Java,Point,我有一个叫做point的类和一个演示类。我试图比较我传入demo类的每个x,y点的y值,但我似乎无法集中精力从每个点检索y值 public class point { // instance variables private int x; private int y; // constructor public point(int x, int y) { this.x = x; this.y = y; }
public class point {
// instance variables
private int x;
private int y;
// constructor
public point(int x, int y) {
this.x = x;
this.y = y;
}
// getter and setter methods
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void equals() {
if (this.x == x && this.y == y) {
System.out.println("equals");
}
}
public int isLower() {
if (this.y > y) {
return y;
}
else {
return this.y;
}
}
public double findLength() {
return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
}
}
演示课
import java.util.Scanner;
public class pointDemo {
public static void main(String[] args) {
System.out.println("Enter the x and y coordinates for point1:");
Scanner scan = new Scanner(System.in);
int intx = scan.nextInt();
int inty = scan.nextInt();
System.out.println("Enter the x and y coordinates for point2:");
int intx2 = scan.nextInt();
int inty2 = scan.nextInt();
System.out.println("Enter the x and y coordinates for point3:");
int intx3 = scan.nextInt();
int inty3 = scan.nextInt();
System.out.println("Enter the x and y coordinates for point4:");
int intx4 = scan.nextInt();
int inty5 = scan.nextInt();
point point1 = new point(intx, inty);
point point2 = new point(intx2, inty2);
point point3 = new point(intx3, inty3);
point point4 = new point(intx4, intx4);
System.out.println(point1.isLower());
}
}
我想你要做的是比较两点。。。然后,您必须修改
isLower()
您可以对此进行增强,以便在所有给定点之间进行比较,或者将它们存储在数组或集合中,并返回所有点中最低的y
现在,您的
isLower()
函数只是将point1.y与point1(相当于point1.y)中的.y进行比较,使其变得毫无意义(对此表示抱歉)。在数据对象内部执行这种比较状态跟踪会使其变得更加复杂。在实际系统中,这会增加维护成本和设计脆弱性
使点实现可比或实现分离器比较器,使用与那些或比较器相关的排序集,并取最后一个元素(假设您的可比或比较器是上升的)是一个更惯用的和面向对象的选项。
public class AscendingPointComparator implements Comparator<point> {
int compare(point a, point b) {
return a == b ? 0 :
a == null ? -1 :
b == null ? 1 :
a.getX() > b.getX() ? 1 :
a.getX() < b.getX() ? -1 :
a.getY() > b.getY() ? 1 :
a.getY() < b.getY() ? -1 :
0;
}
}
公共类AscendingPointComparator实现Comparator{
int比较(点a、点b){
返回a==b?0:
a==null?-1:
b==null?1:
a、 getX()>b.getX()?1:
a、 getX()b.getY()?1:
a、 getY()
除了您的主方法之外:
SortedSet<point> sortedPoints = new TreeSet<point>(new AscendingPointComparator<point>());
sortedPoints.add(Arrays.asList(point1, point2, point3, point4));
// this is an identity (same reference) comparison
// if you want a value comparison
// implement boolean equals(Object o) in the point class
System.out.println(point1 == sortedPoints.last());
SortedSet sortedPoints=新树集(新的上升点比较器());
添加(Arrays.asList(点1、点2、点3、点4));
//这是一个身份(相同的参考)比较
//如果要进行值比较
//在point类中实现布尔等于(对象o)
System.out.println(point1==sortedPoints.last());
使用数组或java.util.List
存储点并迭代(for
或while
),您需要将另一个点
对象作为参数传递给isLower
(并相应地编写它。其思想是将该点(代码正在运行的地方)与另一点(传递给它的地方)进行比较。如果你要求别人阅读你的代码,请修复缩进。Java中的类名按惯例以标题大小写。point类应改为point。SJuan76使用Comparator查看我的答案。我认为这是一种更好的方法。手写排序算法(除非是学校作业)导致代码重复并且容易出错。Java类库有用于比较的标准接口:Comparator和Comparator。如果上面的答案推荐Comparable及其compareTo方法,您的答案将得到改进。值得注意的是,非常重要的是,您解决了基本的设计缺陷,并解释了直接的解决方案。非常高级的答案。我我必须承认,我已经花了一段时间试图应对这一切。我的回答冗长且充满行业背景,这可能无助于解决问题。我需要找到一个tl;平心而论,我的答案有点傻,因为我构建了一个列表,然后将它添加到一个分类集。我可能应该用Collections.sort()来代替。我的主要目标是在高层次上重用库基础设施和算法,而不是手工编写代码。
SortedSet<point> sortedPoints = new TreeSet<point>(new AscendingPointComparator<point>());
sortedPoints.add(Arrays.asList(point1, point2, point3, point4));
// this is an identity (same reference) comparison
// if you want a value comparison
// implement boolean equals(Object o) in the point class
System.out.println(point1 == sortedPoints.last());