我正在尝试修改代码,但得到了title.Anyone中列出的错误,请帮助。
错误:类型BruteCollinearPoints中的方法segments()不适用于参数(Point,Point)
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import edu.princeton.cs.algs4.*;
public class BruteCollinearPoints
{
public BruteCollinearPoints(Point[] points) // finds all line segments containing 4 points
{
int N = points.length;
int i = 0;
for (int p = 0; p < N; p++) {
for (int q = p + 1; q < N; q++) {
double slopeToQ = points[p].slopeTo(points[q]);
for (int r = q + 1; r < N; r++) {
double slopeToR = points[p].slopeTo(points[r]);
if (slopeToQ == slopeToR) {
for (int s = r + 1; s < N; s++) {
double slopeToS = points[p].slopeTo(points[s]);
if (slopeToQ == slopeToS) {
// Create the list of collinear points and sort them.
List<Point> collinearPoints = new ArrayList<Point>(4);
collinearPoints.add(points[p]);
collinearPoints.add(points[q]);
collinearPoints.add(points[r]);
collinearPoints.add(points[s]);
Collections.sort(collinearPoints);
segments(Collections.min(collinearPoints), Collections.max(collinearPoints)); //this is where the error is
}
}
}
}
}
}
}
//public int numberOfSegments(); // the number of line segments
public LineSegment[] segments() // the line segments
{
return segments();
}
}发布于 2016-04-02 23:41:35
segments方法的定义如下:
public LineSegment[] segments() // the line segments
{
return segments();
}这意味着该方法不接受任何参数,但您使用两个参数调用它:
segments(Collections.min(collinearPoints), Collections.max(collinearPoints));要解决此问题,您需要调用不带参数的segments方法,如下所示:
segments();或者重新定义该方法以使用参数。例如:
public LineSegment[] segments(Point a, Point b)
{
// calculate line segments from a to b and return them
}在方面,,正如现在定义的那样,segments方法肯定不会工作,因为它在没有终止条件的情况下递归地调用自己。
发布于 2017-02-25 06:20:00
从您的导入语句可以看出,您正在处理来自Algorithms, 4th Edition course的一项编程任务,称为模式识别。在这个过程中,segments()方法从另一个类返回一个LineSegment[]类型的数组。您可以在提供的“示例客户端程序”中看到它的用法。以下是其中的一部分:
for (LineSegment segment : collinear.segments()) {
StdOut.println(segment);
segment.draw();
}
StdDraw.show();为了实际创建一个新的LineSegment,使用如下代码:
LineSegment seg = new LineSegment(point1, point2)作为参考,这里是在LineSegment.java中调用的创建者
public class LineSegment {
private final Point p; // one endpoint of this line segment
private final Point q; // the other endpoint of this line segment
/**
* Initializes a new line segment.
*
* @param p one endpoint
* @param q the other endpoint
* @throws NullPointerException if either p or q is null
*/
public LineSegment(Point p, Point q) {
if (p == null || q == null) {
throw new NullPointerException("argument is null");
}
this.p = p;
this.q = q;
}
.
.
.
}https://stackoverflow.com/questions/36375137
复制相似问题