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(?);
}
}
}``````

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; …``````

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

:)

## All 7 Replies

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. :)

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!

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

:)

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?

``````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... :)

> 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.

... I know that. :s It wasn't intended to mean that his optimisation wasn't valid...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.