一切都完全按照程序和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));
}
这是主要方法,列出的 3 个方法并不重要,因为错误在第 27 行,double temp = scan.nextDouble(); 我意识到这是不必要的,但我使用 thsat 来查看它是否可以解决问题,因为 nums[i] = scan.nextDouble(); 也有错误。投稿现场结果如下:
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
谁能帮我理解我做错了什么?也欢迎对我的编码实践提出任何批评,我是一名新生,我很难学习 Java,所以如果我能拥有完美的“语法”,可以这么说,我会接受。提前致谢!