【发布时间】:2016-08-08 13:12:07
【问题描述】:
我是一个初学者,我编写了一个 java 程序,它允许您输入 n 个数字,它仅在输入数字 -5 时显示最大值、最小值和平均值,我的程序无法正确显示,我需要一些帮助。我想在输入字符串而不是整数时使用 try/catch 来捕获错误。
import java.util.Scanner;
public class Average{
public static void main(String[]args){
System.out.print("Enter any integer numbers or -5 to quit:");
Scanner scan =new Scanner(System.in);
double avg = 0.0;
int number = -1;
double avg = 0.0;
double sum = 0;
int count = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
try {
while((scan.nextInt())!= -5)
{
if (count != 0) {
avg = ((double) sum) / count;
count++;
}
if (number > max){
max = number;
}
if(number < min){
min = number;
}
catch (InputMismatchException e) {
System.out.println("please enter only integer numbers");
System.out.println("Average : " + avg);
System.out.println("maximum : " + max);
System.out.println("minimum : " + min);
}
}
}
}
}
【问题讨论】:
-
问题标题的糟糕选择。建议将其更改为您的具体问题。除此之外,您的问题实际上缺少问题部分。
-
我的程序没有正确显示平均值、最大值和最小值。而且我还想使用 try catch 来缓存错误。例如:当用户输入字符串而不是整数时。请帮帮我。
-
@Sufyan 请编辑您问题的标题和描述
-
@C-Otto 好的,很抱歉给您带来不便。
标签: java