NoSuchElementException:更改执行环境后,Java程序的行为会有所不同

NoSuchElementException:更改执行环境后,Java程序的行为会有所不同,java,eclipse,debugging,runtime,nosuchelementexception,Java,Eclipse,Debugging,Runtime,Nosuchelementexception,所有输出都与程序和eclipse中的输出完全一致,没有任何错误,但在ASU提交站点上会出现一些错误 // Description: This class reads doubles from the keyboard, logs them to an array // and finds the lowest value in the array along with the sum of // all positive nu

所有输出都与程序和eclipse中的输出完全一致,没有任何错误,但在ASU提交站点上会出现一些错误

//  Description: This class reads doubles from the keyboard, logs them to an array
//                  and finds the lowest value in the array along with the sum of
//                  all positive numbers and the count of negative numbers.

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class Assignment2 {

    public static void main(String[] args) { //Main class, takes care of beginning and end operations
        NumberFormat single = new DecimalFormat("###,###,###,###,##0"); //These alter the outputs
        NumberFormat $ = new DecimalFormat("$###,###,###,###,##0.00"); //later in the code
        double[] nums = new double[100];
        int count = 0;
        @SuppressWarnings("resource") //Eclipse gives an error without this
        Scanner scan = new Scanner(System.in);
        while(nums[count] < 100){
                //have a count to keep track of input numbers
                //input values assigned to nums
                for(int i = 0; i < 100; i++) {
                    double temp = scan.nextDouble();
                    nums[i] = temp;
                    if (nums[i] == 0){
                        break;
                    }
            }
            double min = findMin(nums, count);
            double positive = computePositiveSum(nums, count);
            int negative = countNegative(nums, count);
            System.out.println("The minimum number is " + single.format(min));
            System.out.println("The sum of the positive numbers is " + $.format(positive));
            System.out.println("The total number of negative numbers is " + single.format(negative));
        }

有人能帮我理解我做错了什么吗?另外,对我的编码实践的任何批评都是欢迎的,我是一名大一新生,学习java很困难,所以如果我能拥有完美的“语法”,可以说,我会接受它。提前谢谢

对编码实践的评论:不要使用
$
作为变量名。不要在变量名中使用
$
,因为Java编译器有时会生成包含
$
的名称。我认为您的程序能够通过测试1。因此,我怀疑在每次测试之间是否存在换行或其他问题。
NoTouchElementException
表示当输入中没有任何内容时,您的程序调用了
nextDouble
。我不知道该程序在ASU是如何运行的,也不知道它从哪里获得输入,但是如果输入与您期望的不一样,您的程序可能不够宽容。您可能想使用。代码中需要
@SuppressWarnings(“资源”)
的原因是您没有关闭扫描仪实例。通常情况下,关闭是在使用结束后完成的;因此,在while循环之后,需要调用
scan.close()
。通常在
finally{}
块中完成关闭,以确保语句运行。有关
finally{}
块的更多信息,请查看此线程:如果您得到一个您不理解的异常,您应该参考API文档,查看是什么情况导致该异常被发出。Java文档非常好,如果您费心使用它,它可以回答大多数问题。
Grade

Assignment2

Compilation

2 pts. - Passed
Functionality

Test #1

Program Output
The minimum number is 0
The sum of the positive numbers is $0.00
The total number of negative numbers is 0

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at Assignment2.main(Assignment2.java:27)

Expected Output
The minimum number is 0
The sum of the positive numbers is $0.00
The total number of negative numbers is 0

0 pts. - Failure