确定在循环中每次生成一个新点(带有坐标),并在起始点停止。

huangapple go评论63阅读模式
英文:

determine a new point (with coordinates) each time in a loop, stop at the starting point

问题

在主方法中,我首先想要使用readPoint方法读取起始点(该方法调用两次readCoordinate方法来读取点的x和y坐标)。

接下来,我想要在循环中每次读取一个新的点。我希望循环一直运行,直到再次输入起始点。

我该如何继续?

package domain;

public class Point {

    private int x;
    private int y;

    public Point(int x, int y) {
        setX(x);
        setY(y);
    }

    private void setX(int x) {
        checkCoordinate(x);
        this.x = x;
    }

    private void setY(int y) {
        checkCoordinate(y);
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    private void checkCoordinate(int coordinate) {
        if (coordinate < 0)
            throw new IllegalArgumentException("The coordinate must be greater than or equal to 0");
    }

    public boolean compareWithPoint(Point point) {
        if (this.x == point.x && this.y == point.y)
            return true;
        else
            return false;
    }

    public double calculateDistanceToPoint(Point point) {
        int x1 = point.x;
        int y1 = point.y;
        int x2 = this.x;
        int y2 = this.y;

        double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        return distance;
    }
}
package ui;
import java.util.Scanner;
import domain.Point;

public class PointApplication {

    public static void main(String args[]) {
        PointApplication application = new PointApplication();
        Scanner input = new Scanner(System.in);
        Point startingPoint = application.readPoint("输入第一个坐标:", "输入第二个坐标:", input);

        // 进入循环,读取新的点,直到输入起始点
        while (true) {
            Point newPoint = application.readPoint("输入新的坐标:", "输入新的坐标:", input);
            if (startingPoint.compareWithPoint(newPoint)) {
                System.out.println("输入了起始点,结束循环。");
                break;
            }
        }
    }

    private Point readPoint(String question1, String question2, Scanner sc) {
        System.out.print(question1);
        int number1 = sc.nextInt();

        System.out.print(question2);
        int number2 = sc.nextInt();

        Point newPoint = new Point(number1, number2);
        return newPoint;
    }

    private int readCoordinate(String question, Scanner sc) {
        System.out.print(question);
        int coordinate = sc.nextInt();
        return coordinate;
    }
}
英文:

In the main method, I first want to read in the starting point using the method readPoint (which calls twice readCoordinate in the x and y coordinates of the point).

Next, I want to read in a loop each time a new point. I want to keep the loop until the starting point is entered again.

How do I proceed?

package domain;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
setX(x);
setY(y);
}
private void setX(int x) {
checkCoordinate(x);
this.x = x;
}
private void setY(int y) {
checkCoordinate(y);
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
private void checkCoordinate(int coordinate) {
if (coordinate &lt; 0)
throw new IllegalArgumentException(&quot;The coordinate must be greater than or equal to 0&quot;);
}
public boolean compareWithPoint(Point point) {
if (this.x == point.x &amp;&amp; this.y == point.y)
return true;
else
return false;
}
public double calculateDistanceToPoint(Point point) {
int x1 = point.x;
int y1 = point.y;
int x2 = this.x;
int y2 = this.y;
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
return distance;
}
}

<br>

package ui;
import java.util.Scanner;
import domain.Point;
public class PointApplication {
public static void main(String args[]) {
PointApplication application = new PointApplication();
Scanner input = new Scanner(System.in);
Punt startingPoint = application.readPoint(&quot;Enter first coordinate:&quot;, &quot;Enter second coordinate: &quot;, input);
}
private Point readPoint(String question1, String question2, Scanner sc) {
Scanner input = new Scanner(System.in);
System.out.print(question1);
int number1 = input.nextInt();
System.out.print(question2);
int number2 = input.nextInt();
Point newPoint = new Point(number1, number2);
return newPoint;
}
private int readCoordinate(String vraag, Scanner sc) {
Scanner input = new Scanner(System.in);
System.out.print(question);
int coordinate = input.nextInt();
return coordinate;
}
}

答案1

得分: 1

不考虑存储所有输入的点,您可以按以下方式操作:

Point startingPoint = application.readPoint("输入第一个坐标:", "输入第二个坐标:", input);
Point nextPoint = null;

while (nextPoint == null || !startingPoint.compareWithPoint(nextPoint)) {
  nextPoint = application.readPoint("输入第一个坐标:", "输入第二个坐标:", input);
  // TODO: 处理 nextPoint
}

这个 while 循环将一直运行直到 `nextPoint` 不再等于 `null`(应该在第一次迭代后为真),并且 `compareWithPoint` 方法返回 true
英文:

Without taking into account storing all the entered points, you could do something along the lines of:

Point startingPoint = application.readPoint(&quot;Enter first coordinate:&quot;,&quot;Enter second coordinate: &quot;,input);
Point nextPoint = null;
while (nextPoint == null || !startingPoint.compareWithPoint(nextPoint)) {
nextPoint = application.readPoint(&quot;Enter first coordinate:&quot;,&quot;Enter second coordinate: &quot;,input);
// TODO: Do something with nextPoint
}

This while-loop will run until both nextPoint no longer equals null (should be true after the first iteration) and the method compareWithPoint returns true.

huangapple
  • 本文由 发表于 2020年8月13日 14:04:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63389066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定