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 3 of 3

Thread: Restarting at the beginning of an array

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Restarting at the beginning of an array

    Hi! I'm supposed to create a simple game that gains you points as the dice rolls and moves through the array. Basically, you enter an array. The dice is rolled, you move x amount of steps through the array. If you land on a 0, you lose. I'm having trouble figuring out how to restart at the beginning of the array once it moves through.

    Here's what I have:

    class A3Q2
    {
        public static void main (String[] args)
        {
          int [] a;
          int dice;
          char x = 'y';
          int position = 0;
          int points = 0;
     
          System.out.println("Please enter an array.");
          a = ITI1120.readIntLine( );
     
          while (x == 'y')
          {
            dice = (int)(Math.random() * 5 + 1);
            System.out.println("The number of steps taken was " + dice + ".");
            position = position + dice;
     
            while (position > a.length)
            {
              position = position - a.length;
            }
     
            if (a[position] != 0)
            {
              points = points + a[position];
              System.out.println("You move in position  " + position + " and you gain " + a[position] + " point(s). Total points: " + points);
              System.out.println("Do you want to move again? (y/n)");
              x = ITI1120.readChar();
            }
            else
            {
              System.out.println("You move in position  " + position + " and you gain 0 points.");
              System.out.println("Sorry, you lost.");
              x = 'n';
            }
          }
        }
    }

    My attempt at this is the second loop in the code, which I thought would work, but it doesn't... help please!


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Restarting at the beginning of an array

    Hi bonbon242,
    I don't know if I understand your problem, but I think that if position is 4 and a.length()=3, you want return at the first element of your array.
    For me, if you use mod function, you do it...
    Try
    while (position >=a.length){
    position = (position % a.length);
    }

    in the place of your while loop.
    Tell me something!
    Bye!

  3. The Following User Says Thank You to lucasantos For This Useful Post:

    bonbon242 (October 31st, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Restarting at the beginning of an array

    Oh! Thanks so much that's exactly what I was looking for

Similar Threads

  1. Save the information from an ArrayList after restarting the program
    By noFear in forum Java Theory & Questions
    Replies: 4
    Last Post: August 14th, 2010, 09:53 AM
  2. Java Question Need Help Restarting the Values of Variables from Random Numbers So Out
    By JavaStudent1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 03:25 PM
  3. Error in printing method as always it prints the last value
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 1st, 2009, 11:37 AM
  4. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 03:14 PM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 09:52 PM