What would be the best way (or any way) to print out a pattern, that may cut off at any point dependant on the x & y length?

for example, if i wanted a pattern 4 up (y = 4) and 14 across (x = 14)
I would need it to print:

m//m//m//m//m/
m//m//m//m//m/
m//m//m//m//m/
m//m//m//m//m/

I'm thinking a nested for loop so:

for(i=1; i<y; i++){


//then another thing inside so that would make 4 lines total, but with the pattern printing along the x axis…

but how??

Thanks in advance for any help given.

Michael

Recommended Answers

All 8 Replies

Hello 'meowmonkey', not sure if that what you want ... Print a rectangle of y lines and x item on every line
Anw here's the code:

int Y = 4;
int X = 12;

for(int y=0; y < Y; y++){
    for(int x=0; x < X; x++){
        System.out.print("#");
    }
    System.out.println();
}

Just in addition, obviously you would change the "#" in amirbwb's code with whatever you wanted to print.

That would print a whole bunch of "#" right?
I need it to be a pattern like 'm//m//m//' that cuts off at any point, so could be the m, the first or the second /

that makes no sense. either it has a fixed length (the int you pass) or it might be cut off.

No worries, solved it.
Required an if loop inside a for inside a while within another for.

I explained what I wanted at the top
One symbol (I used an "m" in the example") separated by 2 "/" symbols each time
so if it was 5 long it would be m//m/ or 8 long m//m//m/ 6 long being m//m//

well ... that's basically the same as:

System.out.print("#");

but you just had to do something like:

// in the class itself
char symbol = "m";
//in the method:
System.out.println(this.symbol + "//");

also: try and use the right terms. it might be confusing if you start mashing terms together in the wrong way, for instance when you speak of an "if loop". an if statement is a conditional statement, not an iteration (loop).

Hi Stultuske
I think the OP's problem is a bit more interesting than that. Your solution only works if the desired length is a multiple of 3. As I understand it the requirement is

 y   output
 -   ------
 1   m
 2   m/
 3   m//
 4   m//m
 .   ...
 9   m//m//m//
10   m//m//m//m

etc

@meowmonkey: can you post your solution (if that's not a problem!) so that we can have a look if what you did was indeed the best that could have been done...
Well, you asked for the best way and having 3 levels of iteration (for->while->for) doesn't sound quite optimal...

Be a part of the DaniWeb community

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