This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes adding values in nested loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding values in nested loops" Watch "adding values in nested loops" New topic
Author

adding values in nested loops

ione walker
Ranch Hand

Joined: Apr 19, 2001
Posts: 31
Yet another question,
I wish to print out "me" k times in a single line.
Even I can do that.
===================
for( int k = 0 ; k < 10 ; k++ )
{
System.out.print( "me" );
}
================

my questiion is this.
if I want to have "me" printed out
a total of i times with k per line
I thought that I could do it like this
========================
for( int i = 0 ; i < 10 ; i = i + k )
{
for ( int k = 0 ; k < 2 ; k++ )
{
System.out.print( k + " me" );
}
System.out.println( i + " me" );
}
================
BUT I am getting an
cannot resolve symbol
symbol : variable k
location: class HundredTest
for( int i = 0 ; i < 10 ; i = i + k )
I tried to initalize k befor the loops but
that didn't work either.
Thanks again for you help


------------------
=======================
Ione Walker
walkeri@uas.net
========================


=======================<BR>Ione Walker<BR>walkeri@usa.net<BR>========================
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The way that you have it set up, your variables are local to the blocks of code that they are declared in.
Declare the variables outside of the blocks and use them in the blocks and it should work.
========================

================


"JavaRanch, where the deer and the Certified play" - David O'Meara
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
I've got to question the whole i = i + k bit. Why not just i++?
Through each iteration of the k loop you reset k to 0, so i is incrementing as 0, 2, 4, 6, 8 which would give you five lines. In your for loop you say you will print 10 lines. Seems a little confusing to me.
Surya Renduchintala
Greenhorn

Joined: May 01, 2001
Posts: 10
cindy as u said the problem is due to localisation. but dont u see that i and k are used inside the loop itself ?.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yeah, but k was declared and initialized in the INNER loop, but referenced before that in the OUTER loop.
k needs to be declared and initialized before the outer loop, and the reset to zero each time the inner loops are started again.
There is really nothing wrong with the way i is being used. It's just my personal thing that if I was going to declare and initialize one of the two I would do them both the same way.
Of course I have to agree with Richard, that it is kinda weird to be using k AT ALL in the outer loop. It would make more sense to increment i in the inner loop (so that you never print more than 10 times total). This way the bigger the k amount is, the fewer times that the outer loop will be executed.

However this will NOT get you to print "me" exactly the number of i times unless the i amount is an exact multiple of the k amount. Otherwise it will start into the last go round of the inner loop and finish all of the inner loop times.
If you really want to break out of the inner loop when the i amount has been reached, then you need to have a compound expression in the loop control.


[This message has been edited by Cindy Glass (edited May 05, 2001).]
ione walker
Ranch Hand

Joined: Apr 19, 2001
Posts: 31
This was what I wanted.
I was not aware the I could
incriment i in the k loop.
This showed how to get the EXACT
number of "me"'s that I wanted

------------------
=======================
Ione Walker
walkeri@uas.net
========================

[This message has been edited by Cindy Glass (edited May 07, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding values in nested loops
 
Similar Threads
Understanding logic on for loop
There is no main method in class: Times - How do I resolve it?
Multiplication Table
While nested in do-while loop
MyPattern.Java - homework help?