• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a certain amount of variables  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is supposed to print out something like this:

(* * * * * * * * *) --basically 12 *'s. But I need it to print out whatever the numStars value is

However it only prints out one *.  Pretty sure something is wrong with my while statement.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (that's a link) when you're posting code.

I don't see how that program will ever halt (until it runs out of memory).  Nothing happens that will change the condition in the while loop.
 
Ranch Hand
Posts: 449
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Georgevich wrote:The output is supposed to print out something like this:

(* * * * * * * * *) --basically 12 *'s. But I need it to print out whatever the numStars value is

Then you should write instead of 
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, John Joe, that's not what OP wants to do.
 
John Joe
Ranch Hand
Posts: 449
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:No, John Joe, that's not what OP wants to do.


Oh No..I misunderstood it 
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere in the loop you need to increment numPrinted, otherwise your comparison to numStars will always be true.

A for() loop might serve you better than a while() loop in this case.
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always be very careful how you ask questions here, and be very explicit in what you want.
If you use the print statement

then (with the proper loop) you will get
************
instead of what you said was the expected result:
* * * * * * * * * * * *
i.e., your sample output included spaces between the asterisks.

And as Carey mentioned, it is more common do use a for-loop instead of a while-loop for this type of thing:

Also, on many systems you might get no output (due to buffering) unless you print a newline at the end.
 
Mark Georgevich
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the replies. It helps and I understand now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!