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

Series of stars  RSS feed

 
Ali Khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want the program to output a series of stars as follows. i have used for loop within for loop but it doesnt work instead

it does something like:

*
*
*
etc...


----
*
**
***
****
*****
******
*****
****
***
**
*

help will be appreciated, thanks in advance...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"hot female", please check your private messages.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please post your code, and tell us where you think the problem is.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using println(), it will create a line break every time you call it. Without seeing your code, that's my best guess as to where your mistake lies.
 
Ali Khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry but anyway thanks, that your small hint worked almost around 50%, thats like going uphill but now i dont know how to go downhill.

basically currently its display as follows:
*
**
***
****
*****
******



thank you for helping me....
[ November 29, 2007: Message edited by: hot female ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"hot female", you have chosen to disregard my private message regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff
 
Ali Khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i didnt read the message, sorry again... I have changed it cheers... please do try to help me out here,

thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code is awful complicated. can you imagine what it would be like if you had to go to 100 stars? it really doesn't need to be that complicated.

let's look at what's happening.

print 1 star.
print 2 stars.
print 3 stars.
print 4 stars.
print 5 stars.
print 6 stars.

Can you write code that outputs exactly those 6 lines?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm impressed that you're able to keep track of all the for loops. Wow. My head would be spinning!

Let me share one possible approach to the problem...

First, try to write a program that can print this and don't worry about the '*' characters yet:
1
2
3
4
5
4
3
2
1
If you can get the numbers down, converting them each to a line of * characters will be a piece of cake!

I'll admit this is not terribly simple if you're using primitives in a nesting of for loops. But imagine if you had a class that had the following constructor and methods:
--------------
public UpDownCounter(int maxValue) // constructor
public int nextValue() // return the next number 1,2,3,...,maxValue,...,3,2,1
public boolean isFinished() // return true if done counting
--------------

You now have a nice interface to work with to get the numbers you need:


This might seem more intimidating than a stack of for loops at first, but it will really make your code easier to work with. Not to mention there's a chance an UpDownCounter might come in handy for another assignment.
 
Ali Khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help, can you please tell me the method that currently i am using which is for loops within loops, is it okay and sensible?

secondly i dont understand the following code,



basically counter.nextValue(); i just get lost in this point becuase i dont understand how do input the "*".

thanks
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really shouldn't need to nest so many loops.

When faced with a complicated problem, it's a good idea to break it up into smaller problems. I recommend breaking it up into two main problems:
Step 1) How can I count from 1 to some number (5) and then back down to 1 again?
Step 2) How do I print x number of characters in a single line?

Step 1 you need to figure out how to print:
1
2
3
4
5
4
3
2
1
on your console.

Step two, you should figure out how to turn a number into a series of '*' characters. If you have the answer to step 1 and step 2, you can put them together easily for the final solution. I think right now you are having difficulty because you are trying to solve both steps at once, which is making it hard to focus.

So, step 1. You need to figure out how to count up and then back down. There are numerous ways of doing this. You don't have to do it the way I suggested (if you think it's too confusing, forget for now that you ever saw it), but you do need to figure out how to count to a number and then back down. Can you do that and print the numbers to your System.out?
[ November 30, 2007: Message edited by: Marc Peabody ]
 
Ali Khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, thanks for your help, but i've tried the step1 which is counting number from 1 to 5 to 1... but here is the code and it display bit differently...

for e.g.

1
2
3
4
5
5
1
2
3
4
5
4

etc.....



please help me, seriously i am very new too java programming, so i tried what i learnt so far but its not coming in use...
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of stars depends on the row, and can be calculated as follows (given that row starts at 0):
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali,
The suggested problem of printing 1 2 3 4 5 5 4 3 2 1 doesn't need nested for loops. Since it currently has them, it outputs 30 numbers (5*5 for the inner nested println and other 5 for the outer one.)

Breaking it down even further. Can you write a loop that outputs:
1
2
3
4
5

The can you write another loop that outputs:
5
4
3
2
1

Once you have this, it's just a matter of putting one after the other to output
1
2
3
4
5
5
4
3
2
1
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint: take a look at the difference between System.out.println() and System.out.print().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!