Results 1 to 9 of 9
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Question concerning loops

    I have to write a program that generates 1000 random integers between 1 and 10 and finds how many are less than or equal to 5 and how many are more than five. Here is the code I have written, which is not right. The problem is that I cannot figure out what I am doing wrong. I think it might have to do with the i++ I have in the for statement, but I am not sure. I do not know what to replace it with and if I leave it blank the program becomes an infinite loop.

    Java Code:
    public class PracticeExercise2A {
    
        public static void main(String[] args){
    
           	//generate random number between one and ten one thousand times
           	//perform for loop
        	for(int i = (int)(Math.random()*(10) + 1); i <= 1000; i++){
    
        		if(i <= 5)
    
        			System.out.println("There are " + i + " numbers less than or equal to 5");
    
        				else
    
        					System.out.println("There are " + i + " numbers greater than 5");
    
           	}
       }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    change your logic to something like this:

    Java Code:
    //array to hold the random numbers
    int[] randomNumbers = new int[1000];
    
    //loop 1000 times
    for (int i=0; i<1000; i++) {
        //generate randomInt
        //(your code)
        //save to array
        randomNumbers[i] = randomInt;
    }

    then have your methods to get how many are less than 5:

    Java Code:
    int countMore5;
    for (int i=0; i<randomNumbers.length; i++) {
        //if its greater than 5, increment our counter
        if (randomNumbers[i] > 5) {
            countMore5++;
        }
    }
    
    //print result
    System.out.println("Occurences greater than 5: " + countMore5);
    System.out.println("Occurences less than or equal to 5: " + randomNumbers.length-countMore5);

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    change your logic to something like this:

    Java Code:
    //array to hold the random numbers
    int[] randomNumbers = new int[1000];
    
    //loop 1000 times
    for (int i=0; i<1000; i++) {
        //generate randomInt
        //(your code)
        //save to array
        randomNumbers[i] = randomInt;
    }

    then have your methods to get how many are less than 5:

    Java Code:
    int countMore5;
    for (int i=0; i<randomNumbers.length; i++) {
        //if its greater than 5, increment our counter
        if (randomNumbers[i] > 5) {
            countMore5++;
        }
    }
    
    //print result
    System.out.println("Occurences greater than 5: " + countMore5);
    System.out.println("Occurences less than or equal to 5: " + randomNumbers.length-countMore5);
    We haven't made it to arrays yet, but I will rearrange the random.math method in my for statement like you have it to see what it does.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Oh i see, in that case just move the counter into the first for loop that generates the numbers. So you don't save the numbers in an array, but you count the occurences of numbers greater than 5 while the numbers are being generated.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    Oh i see, in that case just move the counter into the first for loop that generates the numbers. So you don't save the numbers in an array, but you count the occurences of numbers greater than 5 while the numbers are being generated.
    By counter, I am assuming you mean i <= 5. I'm not sure where I put it. The way I have it now is:

    Java Code:
    for(int i = (int)(Math.random()*(10) + 1); i <= 1000; i++)
    If I am understanding things correctly, expression one is " int i = (int)(Math.random()*(10) + 1)" and assigns a random number to i; expression two is "i <= 1000" and is a Boolean condition that executes the body of the loop 1000 times, and expression three is "i++" and modifies expression one, in this case incrementing i.

    If I replace i <= 5 with expression one, it won't generate the random number. If I replace i <= 5 with expression two it will only repeat five times instead of 1000. if I replace it with expression three, the program won't compile.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    i <= 5 is the test. A counter is a variable which you add 1 to every time the test is met. So if the number is less than or equal to 5 you add 1 to a variable declared before the loop. By the way
    Java Code:
    i++ 
    
    Is the same as 
    
    i = i + 1

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    by counter, what i meant was:

    int counter;

    if (i<=5) counter++;

    take that random number out of your for loop statement, it looks crazy!

    your code should be readable by anyone, like this:

    Java Code:
    int randomInt,count;
    //loop 1000 times
    for (int i=0; i<1000; i++) {
        //now make random int
        randomInt = (int) (Math.random()*(10)+1);
        //check what we generated
        if (randomInt <=5) count++;
    }
    after the loop is finished, your 'count' counter will hold the number of instances that the number generated was less than or equal to 5.

  8. #8
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    by counter, what i meant was:

    int counter;

    if (i<=5) counter++;

    take that random number out of your for loop statement, it looks crazy!

    your code should be readable by anyone, like this:

    Java Code:
    int randomInt,count;
    //loop 1000 times
    for (int i=0; i<1000; i++) {
        //now make random int
        randomInt = (int) (Math.random()*(10)+1);
        //check what we generated
        if (randomInt <=5) count++;
    }
    after the loop is finished, your 'count' counter will hold the number of instances that the number generated was less than or equal to 5.
    OK, I got it. Jeez, I'm not sure how I was supposed to figure that out with just the one simple example given to us by the professor. It's kind of like trying to figure out a calc problem with an algebra example.

    So, the way this program is written now, it scrolls through and prints each random number one at a time and decides whether it is greater than or equal to or less than 5. Is it possible at this early stage of my programming class to make the program only print the total number of times it was less than or equal to five and the total number of times it is greater than five?

    Thank you very much for your help. I have to run and go to school now, but will check back in later this afternoon.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Yes, you wasn't supposed to print the total in the for loop. you need the counter in the loop so that it checks & counts for each of the 1000 integers, but the print doesnt need to be done 1000 times. so Outside of the for loop, after the loop has finished, you should print the total.

Similar Threads

  1. Question about sentinal loops with strings
    By dienesh77 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 02:57 AM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. Question about loops
    By SwEeTAcTioN in forum New To Java
    Replies: 1
    Last Post: 10-23-2009, 08:15 AM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 07:59 PM
  5. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 06:40 PM

Posting Permissions

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