运行程序测试不同的循环结构后无输出

时间:2019-07-14 17:54:53

标签: java loops

这是一个旨在生成一系列从1到1000的总计为1000的连续整数的程序。我的函数While1000有效,但是如果我尝试修改此代码以使用其他循环结构来获得相同的输出这是行不通的。使用do-while和for循环时,我修改的代码会阻塞。在Eclipse中运行它们时,我没有任何反应。没有语法错误或异常警告。

以下是代码的不同版本:

public class While1000 {

    public static void main(String args[]) {
        int i, j, k, sum = 0;
        for (i = 1; i <= 1000; i++) {
            j = i;
            sum = i;
            while (sum < 1000)
                sum += ++j;

            if (sum == 1000) {
                for (k = i; k <= j; k++)
                    System.out.print(k + " ");
                System.out.println();
            }
        }
    }

}
public class dowhile1000 {
    public static void main(String args[]) {
        int i, j, k, sum = 0;
        for (i = 1; i <= 1000; i++) {
            do {
                j = i;
                sum += ++j;
            }
            while (sum >= 1000);
            if (sum == 1000) {
                for (k = i; k <= j; k++)
                    System.out.print(k + " ");
                System.out.println();
            }

        }
    }
}
public class For1000 {
    public static void main(String args[]) {
        int i, j, k, sum = 0;
        for (i = 1; i <= 1000; i++) {
            for (j = i; sum >= 1000; ++j) {
                sum += j;
            }

            if (sum != 1000) continue;
            for (k = i; k <= j; k++)
                System.out.print(k + " ");
            System.out.println();
        }
    }
}

正确的输出应该是:

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 

198 199 200 201 202

1000

2 个答案:

答案 0 :(得分:0)

  1. 在循环中,您必须重新初始化变量
  2. 停止条件很重要

dowhile1000

for (i = 1; i <= 1000; i++) {
    --> j = i;   // Shouldn't be initialization do-while
    --> sum = 0; // Should be initialization to 0 after each iteration
    do {
        sum += ++j;
    }
    --> while (sum < 1000); // Keep adding to the sum until is greater than 999
    if (sum == 1000) {
        for (k = i + 1; k <= j; k++)
            System.out.print(k + " ");
        System.out.println();
    }

}

For1000

for (i = 1; i <= 1000; i++) {
    --> sum = 0; // Should be initialization to 0 after each iteration
    --> for (j = i; sum < 1000; ++j) { // Keep adding to the sum until is greater than 999
        sum += j;
    }

    if (sum != 1000) continue;
    for (k = i; k < j; k++)
        System.out.print(k + " ");
    System.out.println();
}

答案 1 :(得分:0)

您有点草率,带有方括号和缩进。那不会提高代码的可读性。

dowhile1000:如果while条件在进入循环之前为false,则do-while循环和while循环将有所不同。事实并非如此。因此,do-while条件必须与while条件相同。第一次运行循环后,您的do-while条件不成立,因此循环终止。当然,j和sum必须在while循环之前初始化。在代码中运行每个循环时,j都会重置。

For1000:只要语句2中的条件为true,就会重复for循环。在for-j循环中,进入循环时条件永远不会为真,因此永远不会执行循环。总和必须在每个新的j之前重置。因为在for条件为真之后j递增,所以在for-k循环中,测试必须为k

public class While1000 {

    public static void main(String args[]) {

    int i, j, k, sum = 0;
    for (i = 1; i <= 1000; i++) {

        j = i;
        sum = i;

        while (sum < 1000)
        sum += ++j;

        if (sum == 1000) {
        for (k = i; k <= j; k++)
            System.out.print(k + " ");
        System.out.println();
        }
    }
    }
}

public class Dowhile1000 {

    public static void main(String args[]) {

    int i, j, k, sum = 0;
    for (i = 999; i <= 1000; i++) {

        j = i;
        sum = 0;

        do {
        sum += j++;
        } while (sum < 1000);

        if (sum == 1000) {
        for (k = i; k < j; k++)
            System.out.print(k + " ");
        System.out.println();
        }

    }
    }
}

public class For1000 {

    public static void main(String args[]) {

    int i, j, k, sum = 0;
    for (i = 1; i <= 1000; i++) {
        sum = 0;
        for (j = i; sum < 1000; ++j) {
        sum += j;
        }

        if (sum != 1000)
        continue;
        for (k = i; k < j; k++)
        System.out.print(k + " ");
        System.out.println();
    }
    }
}