Comparing numbers

sravan953 0 Tallied Votes 153 Views Share

This code snippet accepts a user-defined set of numbers, then accepts those many numbers, and displays the highest and lowest number.

import java.io.*;
class compare
{
static void numbers()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("How many numbers do you want to compare?\n");
        int n=Integer.parseInt(br.readLine());
        System.out.println("Enter "+n+" numbers to compare:\n");
        int y=Integer.parseInt(br.readLine()),a=y,b=y;
        for(int i=1;i<n;i++)
            {
                int x=Integer.parseInt(br.readLine());
                if(x<a)
                    {
                        a=x;
                    }
                else if(x>b)
                    {
                        b=x;
                    }
                }
        System.out.println("The highest numeber is: "+b+"\nLowest number is: "+a);
            }
        }