Window Pane Cid
Window Pane Cid

Reputation: 11

How do I find the the smallest and biggest number in a loop?

I need to find the smallest and largest value from the inputs my while loop receives. I do not know how to do this. This is what I have:

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 " + );

    }
}

Upvotes: 1

Views: 125

Answers (5)

Apofis82
Apofis82

Reputation: 26

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);

    }
}

Upvotes: 0

Juliette_Evans
Juliette_Evans

Reputation: 232

Here's a simple way you can do it. See the comments for explanation.

The idea is to add two new variables, one for your highest grade, and one for your lowest. Each time you enter a new value from your keyboard, you compare the entered value with your current highest and lowest values. If the new value is higher than your highest (or lower than your lowest), you assigned the new value as the new highest/lowest grade.

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);

    }
}

Upvotes: 0

baao
baao

Reputation: 73301

You can create a TreeSet, add every number to it and then simply take the first and last entry - those are min and max as the TreeSet is sorted.

Set<Double> set = new TreeSet<>();

in your while loop, add the double:

set.add(ng);

and after the while loop, get

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

Upvotes: 2

Chuidiang
Chuidiang

Reputation: 1055

Out of the loop, initialize a new variable min with Double.MAX_VALUE and a new variable max with Double.MIN_VALUE. Inside the loop, if ng is greather than max update max with ng. If ng smaller than min, update min with ng.

Upvotes: 0

francesco.s
francesco.s

Reputation: 306

        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);

    }
}

Upvotes: 0

Related Questions