梅森杯健身怎么用:java编写简单例子

来源:百度文库 编辑:神马品牌网 时间:2024/05/05 13:36:58
DOS下
在屏幕下输入若干整数,以 '-1'标志结束
求这些数的最大值,最小值,平均值输出

public class Test {
public static void main(String[] args) throws Exception {

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int count = 0;
int total = 0;
while(true) {
System.out.print("输入整数,以-1作为结束符:");
byte[] b = new byte[10];
System.in.read(b);
int temp = Integer.parseInt((new String(b).trim()));
if(temp == -1) break;
if(temp > max) max = temp;
if(temp < min) min = temp;
total += temp;
count++;
}

System.out.println("最大值: " + max);
System.out.println("最小值: " + min);
System.out.println("平均值: " + ((double)total / (double)count));
}
}