• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested for loops that display patterns  RSS feed

 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm trying to figure out the following question:
"Write nested for loops that will display the following pattens. Prompt the user for an integer value. Continue to re-prompt the user as long as the input is less than 1 or greater than 7. When you get valid input display the two paterns below. Note: the size, but not the shape, of the pattern is dependent on the integer collected from the user. See sample output below for the shape of each pattern requested:"

Bold indicates user input
C:\Users\teacher\Desktop>java question5
Please enter an integer (1 to 7): 0
Please enter an integer (1 to 7): 8
Please enter an integer (1 to 7): 20000
Please enter an integer (1 to 7): 5
*
* *
* * *
* * * *
* * * * *

* * * * *
* * * *
* * *
* *
*
C:\Users\teacher\Desktop>java question5
Please enter an integer (1 to 7): 2
*
* *

* *
*
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I have figured out so far is as follows:



What is displayed when I run it is as follows (for this example I'm using 5 as the integer):
Please enter an integer (1 to 7): 5
*
* *
* * *
* * * *
* * * * *

I can't seem to get the second pattern, I've tried a few different scenarios, if anyone has any suggestions that would be greatly appreciated.
The correct way (when using 5 as the example integer) should display the following pattern:
*
* *
* * *
* * * *
* * * * *

* * * * *
* * * *
* * *
* *
*
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Karen,
compile and run this code. You'll get what you want.


Edited to remove code. We don't approve of handing out complete code as answers to questions here, as that isn't a good way for the posters to learn anything. (Paul C - moderator)
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have got the first half to work. Now, the second half is the first half backwards. Remember you can iterate an array like this:-
for (int i = myArray.size - 1; i >= 0; i--)...
In a 5‑element array that counts from 4…0. Now you want to run from 5 to 1 which you can do by simplifying that loop and putting your own variable names in.

Your row printing works but there is a much simpler way to do it.

Note I put spaces in my code. It si hard to read i=1; i = 1 is much easier to read. Please put a single space on each side of your binary operators. Also spaces before each {. Not ){ please but ) { or similar.
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Karen,
compile and run this code. You'll get what you want.


Edited to remove code. We don't approve of handing out complete code as answers to questions here, as that isn't a good way for the posters to learn anything. (Paul C - moderator)


Not too sure what you mean by this... First of all, when I compile and run this could I do not get the final result of that I want. Second of all, I was not requesting anyone to hand out complete codes as answers, hence why I tried to work through it. I was just inquiring for some insight/guidance!!
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have got the first half to work. Now, the second half is the first half backwards. Remember you can iterate an array like this:-




I did do it in reverse order, but I'm still not getting the final result. The two patterns appear to overlap, instead of mirroring each other. (Below is the pattern I get when I run it with the second/reversed pattern).

*
* * * * *
* *
* * * *
* * *
* * *
* * * *
* *
* * * * *
*
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen,

the original post by Sankalp Bhagat had the complete solution posted. The staff prefers people not do that, so when we see it, we tend to edit the post to remove it. That's why it looks a bit strange to you.
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Barlow wrote:
Hello Karen,
compile and run this code. You'll get what you want.


Edited to remove code. We don't approve of handing out complete code as answers to questions here, as that isn't a good way for the posters to learn anything. (Paul C - moderator)


Not too sure what you mean by this... First of all, when I compile and run this I do not get the final result I want. Second of all, I was not requesting anyone to hand out complete codes as answers, hence why I tried to work through it. I was just inquiring for some insight/guidance!!
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the original post by Sankalp Bhagat had the complete solution posted. The staff prefers people not do that, so when we see it, we tend to edit the post to remove it. That's why it looks a bit strange to you.


Well, as what was probably observed, I did try to work through it. Everyone, learns differently, and sometimes people can understand it better when they have the solution as a template to work through. I'm not looking to be a computer programmer, I'm just trying to understand a language that is completely foreign to me. And being an adult, with a learrning disabitlity, I've learned aids to help myself through life thus far. All I'd like to say in regards to this quote is to please keep in mind that some people aren't trying to take an easy ride, rather they're just trying to understand as thorough as possible, and that means sometimes to see the solution in orderr to go over it step by step.
Thanks for your concern, and I do understand your intent, but keep in mind that sometimes denying people solutions may be more of a hindrance.
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Barlow wrote:. . . The two patterns appear to overlap . . .
That is because you are printing both inside the same loop. You need one loop to go up and one to come down. At least that is probably the simplest way to do it.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Barlow wrote:
Karen Barlow wrote:
Hello Karen,
compile and run this code. You'll get what you want.


Edited to remove code. We don't approve of handing out complete code as answers to questions here, as that isn't a good way for the posters to learn anything. (Paul C - moderator)


Not too sure what you mean by this... First of all, when I compile and run this I do not get the final result I want. Second of all, I was not requesting anyone to hand out complete codes as answers, hence why I tried to work through it. I was just inquiring for some insight/guidance!!



your logic was correct. Just reverse that like Campbell Ritchie has said
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!