Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Homework help  RSS feed

 
Bailey Kufner
Greenhorn
Posts: 2
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I'm stuck on a question and I just need a hint on how to get the result she wants. Here's the question

Write and test a method that takes as parameter any positive integer n and prints the following.

The elements in the 1st row and column are all 1.
The elements in the 2nd row and column are all 2 (except for those in the 1st row and column).
The elements in the 3rd row and column are all 3 (except for those in the 1st and 2nd row and column).
And so on.

Example on next page.



E.g. n = 3 E.g. n = 5

1 1 1 1 1 1 1 1
1 2 2 1 2 2 2 2
1 2 3 1 2 3 3 3
1 2 3 4 4
1 2 3 4 5
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bailey,

You can do that with the use of for loops. That is called "nested for loops" and they will be of the form:

BTW: with your 2 examples running together, I cannot actually tell just what it is that you are printing out.
Bailey Kufner wrote:Hey,

I'm stuck on a question and I just need a hint on how to get the result she wants. Here's the question
. . .
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you get stuck, write the process out on paper in English, and modify it until you can walk through the description you made and create the desire outcome.
Bailey Kufner wrote:Hey,

I'm stuck on a question and I just need a hint on how to get the result she wants. Here's the question
. . .
 
Campbell Ritchie
Marshal
Posts: 55749
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bailey Kufner wrote:. . .
E.g. n = 3 E.g. n = 5

1 1 1 1 1 1 1 1
1 2 2 1 2 2 2 2
1 2 3 1 2 3 3 3
1 2 3 4 4
1 2 3 4 5
If you change the quote tags to code tags, the patterns become easier to read. The normal text and quote tags do not support multiple whitespace, but the code tags do. You can get extra space by writing
“       ”.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!