0

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);
            }
        }
1
Contributor
0
Replies
2
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.