【发布时间】:2014-08-05 05:44:06
【问题描述】:
我成功地在我的代码中找到了最小值、最大值和平均值。像这样的代码:
import java.util.Scanner;
public class Tugas {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int count = 0;
double countTotal = 0;
//... Buat Perulangannya
System.out.print("Enter values (separate by space) : ");
while (input.hasNextDouble() ) {
//... Get the next value.
double value = input.nextDouble();
//... Compare this value to max and min. Replace if needed.
if (value > max) {
max = (int) value;
}
if (value < min) {
min = (int) value;
}
//... Keep track of these values for average calculation.
count++; // Count the number of data points.
countTotal += value; // Keep a running total.
}
//... Be sure user entered at least one data point.
if (count > 0) { //Note 2
//... Display statistics
double rata = countTotal / count;
System.out.println("Min = " + min);
System.out.println("Max = " + max);
System.out.println("Average = " + rata);
} else {
System.out.println("No Data...!");
}
}
}
ok,如果我正在输入: 输入值:1 2 3 4 5, 输出为:min 1,max= 5,average = 3.0
问题是,当我按下“输入”时,我的代码不会自动计数。
当我输入时如何输入:a b 8 9 7 c d 它会一直循环直到得到正确的输入
所以: 输入值:a b 8 9 7 c d 它打印“请输入数字类型” 输入值:6 2 4 6 7 2 3 5 3 分钟:2 最大:7 平均:4.2222
谢谢
【问题讨论】: