控制语句抛出算术异常



这应该是相当简单的,但显然我的控制布尔值有问题,当我输入一个负数而不是结束循环时,它会抛出一个异常,任何帮助都是感激的。

import java.util.*;
public class AverageMinMax {
public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);
double average;
int count = 0, sum = 0, next;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
boolean areMore = true;
System.out.println("Please enter the  numbers you wish to evaluate:");
System.out.println("Followed by a negative number");
// not sure why i used a do while loop but it works i guess
do {
next = keyboard.nextInt();
sum = sum + next;
next++;
if (next < 0)
areMore = false;
} while (areMore == true);

{
sum = sum + next;
average = sum / count;
{// the minmum and max values
if (next > max)
max = next;
if (next < min)
min = next;
}
System.out.println("Your average is: " + average);
System.out.println("Your maximum number is: " + max);
System.out.println("Your minimum number is: " + min);
}
}
}

错误:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.test.task.AverageMinMax.main(AverageMinMax.java:29)

您的问题:

  • 您增加next而不是计数。
  • 局外人(sum = sum + next)也是错误的。你不应该在循环结束后添加next。
  • Min-max应在循环内计算。

改进建议:

  1. 当它比do-while更干净时使用
  2. 使用hasNextInt检查是否为数字
  3. 如果你的输入是charany special character,而循环中断。
import java.util.*;
public class AverageMinMax {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double sum = 0, average =0;
int count = 0, next = 0;

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
System.out.println("Please enter the  numbers you wish to evaluate:");
System.out.println("Followed by a char or any special character to calculate and exit");
// use while it is more cleaner.
// use hasNextInt to check if it is number or not
// if your input is a char or any special character while loop breaks
while (keyboard.hasNextInt()) {
next = keyboard.nextInt();
sum = sum + next;

// the minmum and max values
if (next > max)
max = next;
if (next < min)
min = next;

count++;
}
average = sum / count;
System.out.println("Your average is: " + average);
System.out.println("Your maximum number is: " + max);
System.out.println("Your minimum number is: " + min);
}
}

问题

  1. 计数不增加,其原因除以0错误
  2. max, min, sum, average的计算似乎不正确

解决方案
  1. 来自用户
  2. 的重复读取输入(整数)
  3. 如果输入为正,则对sum,更新min,更新max和更新count进行相加
  4. 如果为负值,则中断输入循环
  5. 如果有输入,则计算平均值
  6. 如果没有输入,则不计算平均值
import java.util.Scanner;
class AverageMinMax {
public static void main(String[] args) {
final Scanner keyboard = new Scanner(System.in);
int count = 0, sum = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
System.out.println("Please enter the  numbers you wish to evaluate.");
System.out.println("Followed by a negative number to stop input");
do {
final int next = keyboard.nextInt();
if (next < 0) {
break;
}
sum = sum + next;
max = Math.max(max, next);
min = Math.min(min, next);
count++;
} while (true);
if (count > 0) {
double average = (sum * 1.0) / count; // need to multiply by 1.0 to have fraction
System.out.println("Your average is: " + average);
System.out.println("Your maximum number is: " + max);
System.out.println("Your minimum number is: " + min);
} else {
System.out.print("No input to process");
}
}
}

你得到算术异常,因为你初始化计数为0。但在那之后当你从控制台获得输入时。而不是增加计数和计数++。你正在循环中执行next++。

并且获得最小最大值也应该需要在循环中完成,目前它们只是在块中。虽然他们不负责你除以零的错误。

这一行是

average = sum / count;

最新更新