平均值,最小值,最大值

时间:2017-10-01 22:19:27

标签: java

所以在我的程序中,用户一次输入一个数字列表,当我用“end”语句结束列表编号时,设置为-1,一旦我这样做,我得到了我的平均值,最大和最小,我的问题是,当我得到最小输出时,它每次都是-1,我很难从数组中删除-1,任何想法???

import java.util.Scanner; //import scanner to user scanner tool

public class Average { //creating public class

    public static void main(String[]args) { //creating public static main

        Scanner input = new Scanner(System.in); //creating scanner input to grab user input

        System.out.println("Please enter a list of numbers, entering -1 to end the list: ");

        double[] numbers = new double[20]; //creating 20 count array
        double sum = 0; 
        int count = 0;
        double average;
        int end = -1;

        for(int i = 0; i<numbers.length; i++) {

            System.out.print("Enter a number: ");
            numbers[i] = input.nextDouble();

            if(numbers[i]== end) {

             break;

            }
            sum+= numbers[i];
            count++;
        }    
        // gets average from user input of numbers
        average = sum/count;
        System.out.println("Average is: " + average);
        double max = maxim(numbers);
        System.out.println("Max: " + max);
        double min = minim(numbers);
        System.out.println("Min: " + min);

    }

//method for finding out maximum number from user input

    public static double maxim(double[] array) {

        double maxNum = array[0];
        for(int i = 1; i<array.length; i++) {
            if(array[i] > maxNum) {
                maxNum = array[i];
            }
        }
     return maxNum; 
    }
 //method for finding out minimum number from user input   

    public static double minim(double[] array) {

        double minNum = array[0];
        for(int i = 1; i<array.length; i++) {
               if(array[i] < minNum && array[i]!=-1) {
                minNum = array[i];
            }

        }
     return minNum; 
    }

}

2 个答案:

答案 0 :(得分:0)

您可以将输入转换为临时变量,并且只有在它不是结束标志(-1)时才将其存储在数组中。 E.g:

for(int i = 0; i<numbers.length; i++) {

    System.out.print("Enter a number: ");
    double temp = input.nextDouble();

    if(temp == end) {
        break;
    }
    numbers[i] = temp;
    sum += numbers[i];
    count++;
}

答案 1 :(得分:0)

您需要对负责从计算统计信息的代码读取用户输入的部分代码进行说明。要从结果数组中删除-1,您需要简单的不要将此值放到结果中。当用户输入随机数时,首先检查它是否为-1,然后将其输入结果。这样的事情:

import java.util.Scanner;  

public class Average {  

    public static void main(String[]args) { //creating public static main
        double[] numbers = readInputNumbers();
        System.out.println("Average is: " + average(numbers));
        System.out.println("Max: " + max(numbers));
        System.out.println("Min: " + min(numbers));
    }

    public double[] readInputNumbers() {
      Scanner input = new Scanner(System.in); //creating scanner input to grab user input
      System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
      double[] numbers = new double[20];
      final int endInput = -1;
      for(int i = 0; i < numbers.length; i++) {
          System.out.print("Enter a number: ");
          double nextNumber = input.nextDouble();
          if(nextNumber == endInput) {
              break;
          } else {
              numbers[i] = nextNumber;
          }
      }
      return numbers;   
  }

    public static double max(double[] array) {
        double maxNum = array[0];
        for(int i = 1; i<array.length; i++) {
            if(array[i] > maxNum) {
                maxNum = array[i];
            }
        }
     return maxNum; 
    }

    public static double min(double[] array) {
        double minNum = array[0];
        for(int i = 1; i<array.length; i++) {
               if(array[i] < minNum && array[i]!=-1) {
                minNum = array[i];
            }

        }
     return minNum; 
    }

    public static double average(double[] numbers) {
        double sum = 0;
        for(int i = 0; i < numbers.length; i++) {
            sum = sum + numbers[i];
        }
        return sum / numbers.length; 
    }

}

在自8版以来的jdk中你可以像这样简化这个任务:

import java.util.Scanner; 
import java.util.stream.*; 

public class Average {  

    public static void main(String[]args) { //creating public static main
        double[] numbers = readInputNumbers();
        DoubleSummaryStatistics statistics = DoubleStream.of(numbers).summaryStatistics();
        System.out.println("Average is: " + statistics.getAverage()));
        System.out.println("Max: " + statistics.getMax());
        System.out.println("Min: " + statistics.getMin());
    }

    public double[] readInputNumbers() {
        Scanner input = new Scanner(System.in); //creating scanner input to grab user input
        System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
        double[] numbers = new double[20];
        final int endInput = -1;
        for(int i = 0; i < numbers.length; i++) {
            System.out.print("Enter a number: ");
            double nextNumber = input.nextDouble();
            if(nextNumber == endInput) {
                break;
            } else {
                numbers[i] = nextNumber;
            }
        }
        return numbers;   
    }
}