This code snippet accepts a user-defined set of numbers, then accepts those many numbers, and displays the highest and lowest number.
Comparing numbers
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);
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.