1

I'm working on this program for my intro to java class. I've been having a lot of issues. I'm using eclipse. The program is supposed to find the average, range, min, and max numbers. I have everything (at least I do according to her notes, which confuse me...) and have put it all into eclipse. It keeps telling me an error that I need a while(); to end the loop. I've already done this...have I put in the wrong place? What am I doing wrong??

import java.util.Scanner;
public class A_Alnor_SquenceOfNumbers {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = 0;
        double sum = 0;
        double input = 0;
        double average;
        double min = in.nextDouble();
        double max = in.nextDouble();

        do {
            System.out.println("Enter a value, type -1 to finish: ");
            boolean done = false;
            while (!done) {
                input = in.nextDouble();
                if (input == -1) {
                    done = true;
                } else

                    while (in.hasNextDouble()) {
                        input = in.nextDouble();
                        sum = sum + input;
                        count++;
                    }

                if (count > 0) {
                    average = sum / count;
                }
                while (in.hasNextDouble()) {
                    input = in.nextDouble();
                    if (input > max) {
                        max = input;
                    }
                    while (in.hasNextDouble())
                        if (input < min) {
                            min = input;
                        }
                }
            }
            while (done) ;
            {
                System.out.println("The average is " + average + ".");
                System.out.println("The smallest number is " + min + ".");
                System.out.println("The largest number is " + max + ".");
                System.out.println("the range is " + min + " to " + max + ".");
                System.out.println(count);
            }
        }
    }
}
4

3 回答 3

1

您正在使用 do..while 构造,并且 do-statement 在其末尾没有任何匹配的 while 子句,可能是由于括号放错了位置。

于 2013-03-31T01:47:05.697 回答
0

您确实在使用没有正确语法的 do while 循环。删除最外层的 do-statement。

此外:您需要修改很多代码,因为这不会终止。它会检查一次输入是否为 -1(注意这是一个整数,而不是双精度!!!)如果不是这种情况,您会继续向用户询问双精度(在很多 while 语句中)。首先创建伪代码并确保它是正确的,因为这不起作用。

祝你好运!

于 2013-03-31T01:51:16.463 回答
0

Java中有几种循环。您似乎正在使用两种:

while环形:

while (condition) {
    loop-body
}

do环形:

do {
    loop-body
} while (condition);

您的外部循环是一个do循环,但缺少终止while (condition)部分。loop-body外循环的部分do本身包含作为循环的内while循环。检查你的代码结构,你应该能够解决它。

于 2013-03-31T01:48:37.570 回答