jeudi 8 janvier 2015

Smallest and Largest integers using if/else only

Here is my code to display smallest and largest integer by taking 5 inputs from user...it works for smallest values but not for largest and I cant figure out the problem...please help



import java.util.Scanner;

public class LargestAndSmallestIntegers {

public static void main(String[] args) {


Scanner input=new Scanner(System.in);
int a,b,c,d,e;
int small,large;

System.out.println("Enter five integer values...");



a=input.nextInt();

small=a;
large=a;

b=input.nextInt();

if (small<b)
{
large=b;
}

else
{
small=b;
}
c=input.nextInt();

if (small<c)
{
large=c;
}

else
{
small=c;
}

d=input.nextInt();
if (small<d)
{
large=d;
}

else
{
small=d;
}
e=input.nextInt();
if (small<e)
{
large=e;
}

else
{
small=e;
}


input.close();
System.out.printf("%d is smallest and %d is largest", small,large);

}
}

Aucun commentaire:

Enregistrer un commentaire