Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could someone please explain what is happening is this problem?

 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm trying to explain why over 20 lines of output gets printed to the console from this code but I really have no idea how

I'd really appreciate if someone could tell what is happening and why there are so many lines of output

The code:

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output count1 and count2 and you should see what's happening.
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? just delete count1 and count2? but I then get error messages
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Output" does not mean "delete". It means to print out the values.
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've outputed count1 like this:

Sytem.out.println(count1)

and all 5 digits show up in the console but each number displays nearly as many times as the original i.e. System.out.println("How many times do we need to trace?");
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try another tactic. Change your output line to:
What can you gather about the results?
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The console now displays from count1 which is 5

and counts up to 20 on each number from count2

 
Ankit Dan
Ranch Hand
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first,
count1 loop from 1 to 5
inside it count2 loop from 1 to 20
System.out.println("How many times do we need to trace?");
count2 loop ends
count1 loop ends


so for every count1 loop, there are 20 count2 loop.thats it!
Hope it helps!

Ankit
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it did! Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic