Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: For loop problem

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default For loop problem

    So, give me a number (1-10) and I will roll a 10 sided dice. I will continue to roll the dice until I have landed on your number 5 times and tell you how many iterations it took. I am trying to make a loop to represent this scenario and I am not sure what I have done wrong. Here is the code I have written so far:

    int x = 1;
                for(int i = 0;; i++)
                {
                    int rNum = (int) (Math.random() * 10) + 1;
                    if (rNum == val) //val is the given number
                    {
                         x++;
                    }
                    if (x == 5)
                    {
                        System.out.println(rNum);
                        break;
                    }
     
                }

    So far the output is just one number, the number I put in! Any help will be appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: For loop problem

    the output is just one number, the number I put in!
    What number do you want to see? What variable has that number?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: For loop problem

    Yeah I noticed my mistake about 5 min after posting the dern question. Sorry for wasting everyone's time here. I just needed to be printing i.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,484
    My Mood
    Mellow
    Thanks
    152
    Thanked 609 Times in 596 Posts

    Default Re: For loop problem

    No worries!

Similar Threads

  1. [SOLVED] Loop Problem
    By Wwong3333 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2012, 03:41 PM
  2. Problem with the loop ..... ;(
    By xcaldk74 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 29th, 2012, 12:25 PM
  3. Do while loop problem
    By kratos75 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 27th, 2011, 06:04 AM
  4. [SOLVED] for loop problem
    By javaneedhelp in forum Loops & Control Statements
    Replies: 3
    Last Post: October 9th, 2011, 10:25 AM
  5. Problem with Loop
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 8th, 2011, 11:12 AM