我对该程序的输出(主要是if / else语句)的输出有点问题。问题是if语句的输出会重复,并给出2个单独的答案,而else语句会像预期的那样打印出错误的整数和结果。
我正在尝试获得类似这样的输出。
示例:
高于阈值62的值的总和为 blahblahblah
低于阈值62的值总和为 blahblahblah
但是,我的程序正在打印出这样的内容(我试图让它在两个输出的整数变量插槽中打印出50):
示例编号:30、60、50、20、10、40
高于阈值30的值的总和为30
高于阈值50的值的总和为80
低于阈值10的值的总和为90
import java.util.*;
import java.io.*;
public class ThresholdTotals {
public static void main (String [] args) throws Exception {
Scanner sc = new Scanner (new File ("values.text"));
int num = 0;
int max = 0;
int sum = 0;
while (sc.hasNextInt()) {
num = sc.nextInt();
max = sc.nextInt(); // used to print the 2nd highest max
if (num >= max) {
sum += num;
System.out.println("The sum of the values above the threshold of " + num + " is " + sum);
// prints out a certain integer and the sum of the values greater than it
}
else {
sum += num;
System.out.println("The sum of the values below the threshold of " + num + " is " + sum);
// prints out a certain integer and the sum of the values less than it
}
}
sc.close();
}
}