-1

我对整个编程事情真的很陌生,我试图弄清楚为什么循环会突然结束并且不会继续到最后的 if 语句。你们能帮我弄清楚是什么问题吗?

import java.util.Scanner;

public class FunnyAverage {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many values to read? ");
        int top = in.nextInt();
        System.out.print("Enter Value: ");
        int one = in.nextInt();
        int number = 1;
        int sum = 0;
        sum = sum + one;

        while (number <= top) {
            if (one % 6 != 0 && one % 17 != 0) {
                System.out.print("Enter Value: ");
                one = in.nextInt();
                number++;
            } else if (one % 6 == 0 && one % 17 == 0) {
                System.out.print("Enter Value: ");
                one = in.nextInt();
                number++;

            }

        }

        if (sum / top != 0) {
            System.out.print("Average: " + sum / top);
        }
        System.out.print("None Divisible");
    }
}
4

2 回答 2

0

看起来你最终处于不存在的else情况下(在while循环内)。因此,number不会增加,您会陷入困境while

尝试onewhile循环内阅读。这样,将提示用户在每个循环中输入一个新数字。

while否则,一旦用户输入的数字与您的支票不符,您将陷入困境。

于 2012-10-05T11:35:57.903 回答
0

如果您提供正确的输入值,最终的 if() 条件就会执行。我运行了您的代码并提供了以下输入来执行最终的 if() 语句。

How many values to read? 1
Enter Value: 1
Enter Value: 1
Average: 1None Divisible

I dont understand what are you trying in the code, but there are many things missing like i assume you want to capture the sum of the input numbers, but sum is not used in the while loop.

于 2012-10-05T11:42:25.760 回答