# Thread: Finding MIddle number help

1. Member
Join Date
Feb 2012
Posts
4
Rep Power
0

## Finding MIddle number help

Can someone show me what to do please. I know that the mid is wrong, look at the output, i know the reason its doing that. But i don't know how to fix it i need help this is in jcreator

Java Code:
```/**Matt R
*/
import java.util.Random;
public class Mid {

public static void main(String[] args) {

//Variables
int min = 50, max = 0, num, mid = 0 , count = 0, third = 0, first = 0, second = 0;
Random generator = new Random();

do{

//Count
count++;

//Num Gen
num = 1 + generator.nextInt(50);

//First
if(count == 1){
first = num;
}
//Second
else if(count == 2){
second = num;
}
//Third
else if(count == 3){
third = num;
}
//max
if(num > max){
max = num;
}
//min
if(num < min){
min = num;
}
//Mid
if(num != min && num != max){
mid = num;
}
}while(count != 3);

//Numbers
System.out.println("\nFirst Number is: " + first);
System.out.println("Second Number is: " + second);
System.out.println("Third Number is: " + third);
System.out.println(max + " > " + mid + " > " + min);
}
}```
This is the output im getting

First Number is: 30
Second Number is: 40
Third Number is: 13
40 > 0 > 13

Process completed.
Last edited by Fubarable; 02-01-2012 at 01:21 AM. Reason: code tags added

2. ## Re: Finding MIddle number help!! ASAP!

We are volunteers and many do not like to be rushed for an urgency that is not theirs (me included). This really is your urgency not ours. Please edit out your ASAP and exclamation points.
Last edited by Fubarable; 02-01-2012 at 12:54 AM.

3. Member
Join Date
Feb 2012
Posts
4
Rep Power
0

## Re: Finding MIddle number help!! ASAP!

I'm sorry i've just been trying forever. I don't know how to edit the title though

4. ## Re: Finding MIddle number help!! ASAP!

Your problem looks to be that you're checking for mid inside the loop itself before the final min and max have been determined. I think that you should check for mid after the loop has concluded.

5. Member
Join Date
Feb 2012
Posts
4
Rep Power
0

## Re: Finding MIddle number help

I just tried that, but it didn't seem to work. I moved it outside the loop, here is the loop
do{

//Count
count++;

//Num Gen
num = 1 + generator.nextInt(50);

//First
if(count == 1){
first = num;
}
//Second
else if(count == 2){
second = num;
}
//Third
else if(count == 3){
third = num;
}
//max
if(num > max){
max = num;
}
//min
if(num < min){
min = num;
}

}while(count != 3);
//Mid
if(num != min && num != max){
mid = num;
}
//Numbers
System.out.println("\nFirst Number is: " + first);
System.out.println("Second Number is: " + second);
System.out.println("Third Number is: " + third);
System.out.println(max + " > " + mid + " > " + min);
}
}

output still came out as

First Number is: 22
Second Number is: 39
Third Number is: 43
43 > 0 > 22

Process completed.

6. ## Re: Finding MIddle number help

Yes you've taken it out of the loop, but how many numbers are you now checking for middle number? All 3?

Actually, I've got a better idea. Why not use your original code, but add a line to the first two checks. Have it so if the num is the new max, that the old max becomes the mid number. Same for the next if block regarding the min. You'll need to set the mid with the old max or min number first before setting max or min. This will add only two lines of code to your original program, one in each of the two if blocks that I mentioned above, and it logically makes sense (work it out on paper to see why).

7. Member
Join Date
Feb 2012
Posts
4
Rep Power
0

## Re: Finding MIddle number help

hmm I'm not sure i fully understand what you are saying. I don't know how to set the mid to the old min or max. could you show me in some code? this is only my second week coding