Nested for loops

D

David

Hi all, I'm new to java and google groups alike. I needed some help
on creating the following out put.

1
22
333
4444
55555
666666
7777777
88888888
999999999


I've seen it done in class but can't seem to remember. There were
only two for loops in the code. Can someone help me with the code?
 
C

Christian

David said:
Hi all, I'm new to java and google groups alike. I needed some help
on creating the following out put.

1
22
333
4444
55555
666666
7777777
88888888
999999999


I've seen it done in class but can't seem to remember. There were
only two for loops in the code. Can someone help me with the code?

Homework is there to be done by yourself. It is to teach you how to code
java. If you don't do it yourself your course will have zero effect.

Christian
 
D

David

David schrieb:






Homework is there to be done by yourself. It is to teach you how to code
java. If you don't do it yourself your course will have zero effect.

Christian- Hide quoted text -

- Show quoted text

Good advice Christian, however this is not homework. it's just a new
concept that was introduced yesterday Friday and I didn't have a very
good grasp on it. I wanted to use this weekend to practice the heck
out of it but I couldn't start practicing because the basic structure
eluded me.

I could wait until Monday to ask the teacher but I'd rather be
familier with it now and concentrate on Monday's teaching on Monday.

How long have you been programming? Are you a serious programmer or
just for fun?
 
P

Peter Duniho

Hi all, I'm new to java and google groups alike. I needed some help
on creating the following out put.

1
22
333
4444
55555
666666
7777777
88888888
999999999


I've seen it done in class but can't seem to remember. There were
only two for loops in the code. Can someone help me with the code?

It's good you're not asking people to do your homework.

That said, it may be more helpful to you, learning-wise, to be given a
general description of what you're looking for, and let you work the
details out on your own. So...

In the output you're looking at, note that there are two things that are
changing: the digit being displayed, and the length of the string being
displayed.

Note also that you even recall there were two loops. So, perhaps one loop
changes the digit, while the other loop controls the length of the string.

Finally, note that the length appears to be related directly to the
digit. In other words, the loop controlling the length of the string may
(i.e. will :) ) use the digit controlled by the one loop as its own
input. In this way, the loop controlling the digit also winds up
indirectly controlling the length of the string.

Here's a simple loop that changes the value of an integer from 1 to 9:

for (int i = 1; i <= 9; i++) { /* do something */ }

Here's a simple loop that writes a fixed number of strings ("1" to "10")
to the standard output:

for (int j = 1; j <= 10; j++) { System.out.printf("%d", j); }

Finally, note that while I have used constants in the loop expressions,
anywhere there's a hard-coded literal integer, you could instead put an
integer variable.

All of the above are the building blocks you need to answer your
question. You should now try on your own to assemble them in a useful way.

Finally, while it's not quite as bad to post questions like this one as
compared to asking for help with your homework, I would say that a
newsgroup not specifically intended for learning how to program is
probably not the best place for advice related to basic programming
techniques. Ideally, this is something better handled by contacting your
teacher and/or classmates for help, but if not, I suspect there are forums
out there that are specifically intended for helping people new to
programming.

I can't speak for the others reading this newsgroup, but while I don't
object to the occasional "I don't know how to program, please help"
question, it's not really something that should come up a lot in a
newsgroup like this one. That sort of thing can really reduce the
signal-to-noise ratio for the newsgroup if it gets to be commonplace.

Pete
 
D

David

It's good you're not asking people to do your homework.

That said, it may be more helpful to you, learning-wise, to be given a  
general description of what you're looking for, and let you work the  
details out on your own.  So...

In the output you're looking at, note that there are two things that are  
changing: the digit being displayed, and the length of the string being  
displayed.

Note also that you even recall there were two loops.  So, perhaps one loop  
changes the digit, while the other loop controls the length of the string.

Finally, note that the length appears to be related directly to the  
digit.  In other words, the loop controlling the length of the string may  
(i.e. will :) ) use the digit controlled by the one loop as its own  
input.  In this way, the loop controlling the digit also winds up  
indirectly controlling the length of the string.

Here's a simple loop that changes the value of an integer from 1 to 9:

     for (int i = 1; i <= 9; i++) { /* do something */ }

Here's a simple loop that writes a fixed number of strings ("1" to "10")  
to the standard output:

     for (int j = 1; j <= 10; j++) { System.out.printf("%d", j); }

Finally, note that while I have used constants in the loop expressions,  
anywhere there's a hard-coded literal integer, you could instead put an  
integer variable.

All of the above are the building blocks you need to answer your  
question.  You should now try on your own to assemble them in a useful way.

Finally, while it's not quite as bad to post questions like this one as  
compared to asking for help with your homework, I would say that a  
newsgroup not specifically intended for learning how to program is  
probably not the best place for advice related to basic programming  
techniques.  Ideally, this is something better handled by contacting your  
teacher and/or classmates for help, but if not, I suspect there are forums  
out there that are specifically intended for helping people new to  
programming.

I can't speak for the others reading this newsgroup, but while I don't  
object to the occasional "I don't know how to program, please help"  
question, it's not really something that should come up a lot in a  
newsgroup like this one.  That sort of thing can really reduce the  
signal-to-noise ratio for the newsgroup if it gets to be commonplace.

Pete- Hide quoted text -

- Show quoted text -

I like your style Pete, thank you very much.
 
D

David

It's good you're not asking people to do your homework.

That said, it may be more helpful to you, learning-wise, to be given a  
general description of what you're looking for, and let you work the  
details out on your own.  So...

In the output you're looking at, note that there are two things that are  
changing: the digit being displayed, and the length of the string being  
displayed.

Note also that you even recall there were two loops.  So, perhaps one loop  
changes the digit, while the other loop controls the length of the string.

Finally, note that the length appears to be related directly to the  
digit.  In other words, the loop controlling the length of the string may  
(i.e. will :) ) use the digit controlled by the one loop as its own  
input.  In this way, the loop controlling the digit also winds up  
indirectly controlling the length of the string.

Here's a simple loop that changes the value of an integer from 1 to 9:

     for (int i = 1; i <= 9; i++) { /* do something */ }

Here's a simple loop that writes a fixed number of strings ("1" to "10")  
to the standard output:

     for (int j = 1; j <= 10; j++) { System.out.printf("%d", j); }

Finally, note that while I have used constants in the loop expressions,  
anywhere there's a hard-coded literal integer, you could instead put an  
integer variable.

All of the above are the building blocks you need to answer your  
question.  You should now try on your own to assemble them in a useful way.

Finally, while it's not quite as bad to post questions like this one as  
compared to asking for help with your homework, I would say that a  
newsgroup not specifically intended for learning how to program is  
probably not the best place for advice related to basic programming  
techniques.  Ideally, this is something better handled by contacting your  
teacher and/or classmates for help, but if not, I suspect there are forums  
out there that are specifically intended for helping people new to  
programming.

I can't speak for the others reading this newsgroup, but while I don't  
object to the occasional "I don't know how to program, please help"  
question, it's not really something that should come up a lot in a  
newsgroup like this one.  That sort of thing can really reduce the  
signal-to-noise ratio for the newsgroup if it gets to be commonplace.

Pete- Hide quoted text -

- Show quoted text -

public class ForLoop {

public static void main (String[] args) {
for (int row = 1; row <= 9; row = row + 1){
for (col = 1; col <= row; col = col + 1) {
System.out.print(row);
}
System.out.println();
}
}
}

deciphering code is much easier than creating it.
 
D

Daniele Futtorovic

On Feb 16, 8:53 pm, "Peter Duniho" <[email protected]>
wrote:
public class ForLoop {

public static void main (String[] args) {
for (int row = 1; row <= 9; row = row + 1){
for (col = 1; col <= row; col = col + 1) {
System.out.print(row);
}
System.out.println();
}
}
}

row += 1
or simply
row++
or
++row
are a tad prettier than
row = row + 1

If you haven't been introduced to some of the operators above, have a
look at:
deciphering code is much easier than creating it.

I'm afraid you'll find that, at a professional level, the exact opposite
is the case.

DF.
 
C

Christian

David said:
Good advice Christian, however this is not homework. it's just a new
concept that was introduced yesterday Friday and I didn't have a very
good grasp on it. I wanted to use this weekend to practice the heck
out of it but I couldn't start practicing because the basic structure
eluded me.

I could wait until Monday to ask the teacher but I'd rather be
familier with it now and concentrate on Monday's teaching on Monday.

How long have you been programming? Are you a serious programmer or
just for fun?

I am programmming Java now for about 2 and a half year. Started just
before I began studying Computer Science.
Though most of my programming is for the pleasure I am meanwhile getting
paid to do research on p2p systems, where my software is done in Java.

Still as its just a few hours per week I do program I wouldn't call me
that serious. After 2 years I still feel like a welp, when Patricia or
alike people here talk about their experience.

Christian
 
R

Roedy Green

it's just a new
concept that was introduced yesterday Friday and I didn't have a very
good grasp on it. I wanted to use this weekend to practice the heck
out of it but I couldn't start practicing because the basic structure
eluded me.

the same principle applies. You are doing this to learn. If the
solution is handed to you on a plate, you are depriving yourself of
the learning experience. The goal is to teach you how to solve
problems, not to find the answers.
 
D

David

On Feb 16, 8:53 pm, "Peter Duniho" <[email protected]>
wrote:
public class ForLoop {
      public static void main (String[] args) {
            for (int row = 1; row <= 9; row = row + 1){
                for (col = 1; col <= row; col = col + 1) {
                    System.out.print(row);
                }
                System.out.println();
            }
      }
}

     row += 1
or simply
     row++
or
     ++row
are a tad prettier than
     row = row + 1

If you haven't been introduced to some of the operators above, have a
look at:
deciphering code is much easier than creating it.

I'm afraid you'll find that, at a professional level, the exact opposite
is the case.

DF.

Great, thank you all for the responses. I have enough to keep me busy
for now. Thanks again, I appreciate it.
 
Joined
Nov 18, 2011
Messages
2
Reaction score
0
help me please, i cant figure out this problem.

7
66
555
4444
33333
222222
1111111

:adore:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top