一切都在程序和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,所以如果我能有完美的“语法”,可以这么说,我会接受它。提前谢谢!