Results 1 to 7 of 7
  1. #1
    rosanator10 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    rosanator10 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #5
    rosanator10 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #7
    rosanator10 is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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

Similar Threads

  1. Finding Second largest number
    By Gsangha in forum New To Java
    Replies: 5
    Last Post: 12-29-2011, 04:49 PM
  2. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-13-2010, 12:42 AM
  3. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  4. finding length on a number
    By thekrazykid in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 09:07 PM
  5. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 09:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •