While 循环中断条件

发布于 2024-09-29 14:26:24 字数 843 浏览 0 评论 0原文

我想用多边形进行一些计算。所以我需要用户输入一些 x 和 y 坐标。这是通过以下代码执行的:

private static boolean isX = true;
private static Scanner scanner = new Scanner(System.in);

private static double readDouble() {
    double value = 0;
    System.out.print("Koordinate " + (isX ? "X" : "Y") + " : ");
    value = scanner.nextDouble();
    if (!isX)
        System.out.println("--------------------");
    isX = !isX;
    return value;
}

为了计算多边形的轮廓,我需要多边形的总数。输入是循环执行的。当最后一个多边形数据与第一个多边形数据具有相同的坐标时,输入应该结束。例如,第一个输入将是 X: 1 Y:1、X: 1、X: 2 将结束输入。

    double fX = readDouble(); double x = 0.0;
    double fY = readDouble(); double y = 0.0;

    int nVertex = 1;

    while((x != fX) && (y != fY)){
        nVertex++;
        x = readDouble(); y = readDouble();
    }

但循环中的输入仅执行一次。所以中断条件有问题。

有什么想法吗?

I want to do some calculations with polygons. So I need the user to input some x and y coordinates. This is performed by the following code:

private static boolean isX = true;
private static Scanner scanner = new Scanner(System.in);

private static double readDouble() {
    double value = 0;
    System.out.print("Koordinate " + (isX ? "X" : "Y") + " : ");
    value = scanner.nextDouble();
    if (!isX)
        System.out.println("--------------------");
    isX = !isX;
    return value;
}

To calculate the outline of a polygon I need the total amount of polygons. The input is performed in a loop. The input should end when the the last polygon data has the same coordinates as the first. E.g. first input would be X: 1 Y:1, X: 1, X: 2 would end the input.

    double fX = readDouble(); double x = 0.0;
    double fY = readDouble(); double y = 0.0;

    int nVertex = 1;

    while((x != fX) && (y != fY)){
        nVertex++;
        x = readDouble(); y = readDouble();
    }

But the input in the loop is perfomed only ones. So something is wrong with the break condition.

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-10-06 14:26:24

你应该有 or (||),而不是 and (&&)

   while((x != fX) || (y != fY)){
        nVertex++;
        x = readDouble(); y = readDouble();
    }

只要 x 不等于 fX 或 y 不等于 fY - 即

NOT (x == fX AND y == fY)

在伪代码中,你就想继续。使用当前代码,一旦 x 和 y 之一与初始值相同,您将终止循环。

You should have or (||), not and (&&)

   while((x != fX) || (y != fY)){
        nVertex++;
        x = readDouble(); y = readDouble();
    }

You want to contine as long as x is not equal to fX or y is not equal to fY -that is

NOT (x == fX AND y == fY)

in pseudo code. With your current code, you'll terminate the loop as soon as one of x and y are the same as the initial values.

半暖夏伤 2024-10-06 14:26:24

正如保罗所说,你应该有“||” (或运算符)不是“&&” (和运算符)在 while 条件下。您希望循环直到两者相同,因此如果其中一个不相等,您希望继续。

同样重要的是,您不应该使用等于比较来进行浮点比较。浮点的不精确性意味着您不应该依赖这些值完全相等。虽然从文件读取的相同值很可能会在机器表示中产生完全相同的值,但不能保证。您应该考虑更改文件格式以以其他方式指示多边形的末端。如果这不可能,请考虑使用不太精确的比较,例如坐标等于某个小精度水平(例如 1e-6)。所以:

while (Math.abs(x-fx)<1e-6 || Math.abs(y-fy)<1e-6) {

As Paul says, you should have "||" (or operator) not "&&" (and operator) in the while condition. You want to loop until both are the same, so you want to continue if either are unequal.

Just as important, you should not be using equals comparisons for floating point comparisons. The imprecision of floating point means that you should not rely on the values to be exactly equal. While it's highly likely that the same value read from a file will produce exactly the same value in the machine representation, it isn't guaranteed. You should consider changing your file format to indicate the end of a polygon in some other way. If this isn't possible consider using a less precise comparison, such as the coordiantes being equal to some small level of precision (say 1e-6). So:

while (Math.abs(x-fx)<1e-6 || Math.abs(y-fy)<1e-6) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文