如何找到循环中最小和最大的数字?

时间:2016-11-30 17:13:15

标签: java

我需要从while循环接收的输入中找到最小和最大的值。我不知道该怎么做。 这就是我所拥有的:

import java.text.NumberFormat;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class average {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        int count = 0;
        double ng = 0, total = 0, average;
        int countt;
        {
        BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
        }
        while (ng > -1) {
            if (ng > -1)
            System.out.print("Enter your next grade (-1 to quit) : ");
            ng = keyboard.nextDouble();
            total = total + ng;
            count++;
        }
        countt=count-1;
        average = (total + 1) / countt;
        System.out.println(" ");
        System.out.println("Total number of students: " + countt);
        System.out.println("Average of grades " + average);
        System.out.println("Highest grade " + );

    }
}

5 个答案:

答案 0 :(得分:2)

您可以创建一个TreeSet,向其中添加每个数字,然后简单地取第一个和最后一个条目 - 这些是最小值和最大值,因为TreeSet已经排序。

Set<Double> set = new TreeSet<>();
在你的while循环中

,添加双精度:

set.add(ng);

在while循环之后,获取

Object[] sa = set.toArray();
System.out.println("min: " + sa[0]);
System.out.println("max: " + sa[sa.length - 1]);

答案 1 :(得分:0)

        import java.text.NumberFormat;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class average {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        int count = 0;
        double ng = 0, total = 0, average;
        int countt;
        {
        BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
        }
 int max = 0;
        while (ng > -1) {
            if (ng > -1)
            System.out.print("Enter your next grade (-1 to quit) : ");
            ng = keyboard.nextDouble();
            total = total + ng;
            count++;
            max = ng > max? ng : max;
        }
        countt=count-1;
        average = (total + 1) / countt;
        System.out.println(" ");
        System.out.println("Total number of students: " + countt);
        System.out.println("Average of grades " + average);
        System.out.println("Highest grade " + max);

    }
}

答案 2 :(得分:0)

在循环之外,使用Double.MAX_VALUE初始化一个新变量min,使用Double.MIN_VALUE初始化一个新变量max。 在循环内部,如果ng比使用ng的最大更新最大值更强。如果ng小于min,请使用ng更新min。

答案 3 :(得分:0)

这是一种简单的方法。请参阅评论以获得解释。

我们的想法是添加两个新变量,一个用于最高等级,另一个用于最低等级。每次从键盘输入新值时,都会将输入的值与当前的最高值和最低值进行比较。如果新值高于最高值(或低于最低值),则将新值指定为新的最高/最低等级。

import java.text.NumberFormat;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Average {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        int count = 0;
        double ng = 0, total = 0, average;
        int countt;

        // Add these variables
        boolean firstRun = true;
        double largest = 0;
        double smallest = 0;

        {
            BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
        }
        while (ng > -1) {
            if (ng > -1)
                System.out.print("Enter your next grade (-1 to quit) : ");
            ng = keyboard.nextDouble();

            // Add this part for calculating largest and smallest
            if (ng > -1) {
                // When you run it the first time, assign this first value as the largest and
                // smallest value, since it is the only value you've seen at the moment
                if (firstRun) {
                    firstRun = false;
                    largest = ng;
                    smallest = ng;
                } else { // If it's not the first run, then compare the values
                    // Check if the current value is bigger than your current max value
                    if (ng > largest) {
                        largest = ng;
                    }

                    // Check if the current value is smaller than your current min value
                    if (ng < smallest) {
                        smallest = ng;
                    }
                }
            }

            total = total + ng;
            count++;
        }
        countt=count-1;
        average = (total + 1) / countt;
        System.out.println(" ");
        System.out.println("Total number of students: " + countt);
        System.out.println("Average of grades " + average);

        // Et voila!
        System.out.println("Highest grade " + largest);
        System.out.println("Lowest grade " + smallest);

    }
}

答案 4 :(得分:0)

import java.text.NumberFormat;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class average {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        int count = 0;
        double ng = 0, total = 0, average, smallest = -1, largest = -1;
        int countt;
        {
        BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
        }
        while (ng > -1) {
            if (ng > -1)
            System.out.print("Enter your next grade (-1 to quit) : ");
            ng = keyboard.nextDouble();
            if (smallest == -1 || (ng != -1 && ng < smallest))
                smallest = ng;
            if (ng > largest)
                largest = ng;
            total = total + ng;
            count++;
        }
        countt=count-1;
        average = (total + 1) / countt;
        System.out.println(" ");
        System.out.println("Total number of students: " + countt);
        System.out.println("Average of grades " + average);
        System.out.println("Highest grade " + "");
        System.out.println("largest: " + largest);
        System.out.println("smallest: " + smallest);

    }
}