I'm being asked to represent an integer score value in asterisks
instead of just as a digit. I figured (as a newbie) I could just define
a string 'star' (as "*") and just call it like score * star..
apparently not. How can I do this? My other idea is using a while loop,
but I think this could be a bit unwieldy, for the scale of the program
it's for.
Matt
Oliver Wong - 31 Oct 2005 20:27 GMT
> I'm being asked to represent an integer score value in asterisks
> instead of just as a digit. I figured (as a newbie) I could just define
> a string 'star' (as "*") and just call it like score * star..
> apparently not. How can I do this? My other idea is using a while loop,
> but I think this could be a bit unwieldy, for the scale of the program
> it's for.
I'd use a for-loop if I were you. But a while-loop would probably do
just as well (subtle pun).
- Oliver
guitarromantic@gmail.com - 31 Oct 2005 20:59 GMT
Thankyou, you too Carl.. I actually meant a 'for' loop, not while, I
just got mixed up. I've got this working exactly as I needed it to now,
anyway, so thanks again!
Carl - 31 Oct 2005 20:28 GMT
> I'm being asked to represent an integer score value in asterisks
> instead of just as a digit. I figured (as a newbie) I could just define
[quoted text clipped - 4 lines]
>
> Matt
I would use a 'for' loop.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
Carl.
Thomas Schodt - 01 Nov 2005 10:33 GMT
> I'm being asked to represent an integer score value in asterisks
> instead of just as a digit. I figured (as a newbie) I could just define
> a string 'star' (as "*") and just call it like score * star..
> apparently not. How can I do this? My other idea is using a while loop,
> but I think this could be a bit unwieldy, for the scale of the program
> it's for.
As you say 'digit' I assume the range is [0;9], so
public static final String STAR9 = "*********";
...
STAR9.substring( /*for you to figure out*/ );
As there are only 10 different scores, you could cache them in an array
so you don't create a new String object every time.
If you have larger numbers to deal with
use a StringBuffer to "double up"
public static final StringBuffer STARS = new StringBuffer("****");
...
while (STARS.length()<score) STARS.append(STARS);
STARS.substring( /*for you to figure out*/ );