Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

here is my code

 private static void stats(String[] args) {
if (args.length == 1) {
  System.out.print("Error: Argument count mismatch");
  return;
}
int[] array = {0};
double average = 0.0;
int total = 0;
int count = 0;
int max = array[0];
int min = max;
for (int i = 1;i < args.length;i++) {
  if (isInteger(args[i]) == false) {
    System.out.print("Error: Argument type mismatch");
    return;
  }
  else {
    count++;
    int a = Integer.parseInt(args[i]);
    total += a;
    average = total / count;
    max = a;
    for (int d = 1; d < array.length; d++) {
      if (array[d] > max) {
        max = array[d];
      }
      if (array[d] < min) { // change to > for largest
        min = array[d];
      }
    }
  }
}
System.out.println("Total " + total);
System.out.println("Max " + max);
System.out.println("Min " + min);
System.out.printf("Average " + "%.2f
" , average);

}

for some reason it won't print out the max and min values, I tried a couple of things and my idea is that I might need to declare max and min again, but to what?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

Consider the following code for your stats() method. Perhaps it will enlighten you.

private static void stats(String[] args)
{
    final int count = args.length - 1;
    int total = 0;
    int max = Integer.MIN_VALUE;
    int min = Integer.MAX_VALUE;
    for (int i = 0; i < count; i++) {
        int a = Integer.parseInt(args[i + 1]);
        total += a;
        if (max < a) max = a;
        if (min > a) min = a;
    }
    System.out.println("Total " + total);
    System.out.println("Max " + max);
    System.out.println("Min " + min);
    System.out.printf("Average " + "%.2f
", (double)total / count);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...