The code below compares three numbers and it prints out the Maximum and Minimum value, let me hope it will rescue somebody :)

import java.util.Scanner;

public class MaxMin
{
   public static void main(String args[])
   {
      int w,x, y;
System.out.println("\n\nTHE PROGRAM BELOW CALCULATES AND OUT PUTS THE MAXIMUM AND MINIMUM OF THREE DIFFERENT MARKS OBTAINED");
System.out.println("\n\nTHE PROGRAM HAS BEEN PRODUCED FOR THE JAVA ASSIGNMENT GIVEN BY MR GOLOOBA RONALD");
System.out.println("\n***********************************************************************");
System.out.println("\n Enter Your first value: ");
Scanner in = new Scanner(System.in);
w = in.nextInt();
System.out.println("\nEnter Your second value: ");
x = in.nextInt();
System.out.println("\nEnter Your third value: ");
y = in.nextInt();


if (w>x && w>y)  {
System.out.println("\n The maximum Value is :"+w);
}
else if(x>w && x>y){
System.out.println("\n The maximum Value is :"+x);
}
else if(y>w && y>x){
System.out.println("\nThe maximum Value is :"+y);
}
else{
System.out.println("\nThe minimum Value is :"+y);
}
if (w<x && w<y)  {
System.out.println("\n The minimum Value is :"+w);
}
else if(x<w && x<y){
System.out.println("\n The minimum Value is :"+x);
}
else if(y<w && y<x){
System.out.println("\nThe minimum Value is :"+y);
}
else{
System.out.println("\nThe minimum Value is :"+y);
}


System.out.println("\n************************************************************************");
System.out.println("\n\nRegards from Mukiibi Ismail");
System.out.println("\n************************************************************************");
}
}

it makes it needles complicated.
just a short example:

int one = in.nextInt();
int max = one;
int low = one;
int two = in.nextInt();
if ( max < two ) max = two;
if ( low > two ) low = two;
int three = in.nextInt();
if ( max < three ) max = three;
if ( low > three ) low = three;

and this is just if you really need to store all the values.

a simpler way would be:

System.out.println("how many numbers will you read?");
int nr = in.nextInt();
int[] nrs = new int[nr];
nrs[0] = in.nextInt();
int max = nrs[0];
int low = nrs[0];

for ( int i = 1; i < nr; i++){
  nrs[i] = in.nextInt();
  if ( nrs[i] > max ) max = nrs[i];
  if ( nrs[i] < low
}

naturally, this still leaves the code vulnerable, since: what will happen if you enter "three" as input? indeed, an exception will be thrown. so decent exception handling should also be provided, if this is to be a complete and good example.

I would also like to share this code.

public class SomeClass
{
    public int compareNums(int a, int b, int c)
    {
        return compareNums(compareNums(a, b), c);
    }

    public int compareNums(int a, int b)
    {
        if(a < b)return a;
        else return b;
    }
}
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.