0

Hello all -- found myself stuck on a certain bit of code dealing with a number pattern I put together. I've written out most of the code, but I'm stuck in 2 areas - thought maybe someone could help shed some light. It seems like it would be very simple, but I just can't get it to work right yet, with whatever I code in.

Here's the output I'm trying to achieve:

12345
23451
34512
45123
51234

This is what I've written out so far (stuck spots marked with "?"):

public class NumPattern {
  public static void main (String[] args) {

      for (int i=1; i <= 5; i++) {

      int next = i;


      for (?) {

        System.out.print (next++);
       
        if (next > 5)
        next = 1;
        }
        
        System.out.print(?);
    }
  }
}

Many thanks in advance!

5
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by Black Box
0

This should do the trick:

public class Main {
  public static void main (String[] args) {

      for (int i=1; i <= 5; i++) {

      int next = i;


        for (int k = 0; k < 5; k++) {

            System.out.print (next++);
       
            if (next > 5) next = 1;
        }
        
        System.out.println("");
    }
  }
}

Remember to change the class name btw. :)

0

This should do the trick:

public class Main {
  public static void main (String[] args) {

      for (int i=1; i <= 5; i++) {

      int next = i;


        for (int k = 0; k < 5; k++) {

            System.out.print (next++);
       
            if (next > 5) next = 1;
        }
        
        System.out.println("");
    }
  }
}

Remember to change the class name btw. :)

Thanks so much - I knew it was going to be something easy - it usually is. lol Lightbulb is now on!

Anyway, thanks again!

0

Don't forget about your friend the mod operator as well:

for (int i=0; i < 5; i++) {
            for (int k=0; k < 5; k++) {
                System.out.print((i+k)%5 + 1);
            }
            System.out.println("");
        }

:)

0

And to really tip it off: the System.out.println() method doesn't need the empty string "".

Maybe we should take this to the next level and start optimizing this in assembly?

0

Don't forget about your friend the mod operator as well:

for (int i=0; i < 5; i++) {
            for (int k=0; k < 5; k++) {
                System.out.print((i+k)%5 + 1);
            }
            System.out.println("");
        }

:)

tight, tight... :)

0

> Maybe we should take this to the next level and start optimizing this in assembly?
It has got nothing to do with assembly. What he proposed / pointed out was a valid point.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.