*
**
***
****
*****
****
***
**
*
But I have not been able to do it using only one nested loop. I have
done it with two nested for loops, but need help in using only a
single for loop. Any suggestions?
Needed to go back to my highschool algerbra but it works...
If the OP ever responds, I'd be curious as to what the group thinks of
my solution. I won't post it before the OP responds..
--
There are 10 types of people in this world. Those who understand binary
and those who don't.
I'd be curious to see what how you've done it... (but yeah, perhaps wait
until OP gets the answer themselves).
lex
> I'd be curious to see what how you've done it... (but yeah, perhaps wait
> until OP gets the answer themselves).
> lex
One question, how did you think to transform the *'s into numbers? That
would never have crossed my mind.
If your email address is correct, I could send you a link where you can
find a zip file of my source.
Just a classic sort of problem simplification: output something that
requires the same information (i.e. number of stars that would be
output), but is simpler to do - helps you concentrate on the problem at
hand...
> If your email address is correct, I could send you a link where you can
> find a zip file of my source.
Feel free. You'll have to reply to the anti-spam confirm message you get
sent though.
lex
Did your solution involve Math.abs()?
--
Lew
> But I have not been able to do it using only one nested loop. I have
> done it with two nested for loops, but need help in using only a
> single for loop. Any suggestions?
Recursion.
--
---------------------------------------------------------------------
Greg R. Broderick gregb+use...@blackholio.dyndns.org
A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
public static void drawDiamond(int n) {
for(int i = 0; i < n * n; i++)
System.out.print((Math.abs(i%n - n/2) + Math.abs(i/n - n/2) > n/
2?"":"*") + (i%n == n-1?"\n":""));
}
Reference:
http://www.velocityreviews.com/forums/showthread.php?s=3c9a5c0ae8f73e70acef59996dbe836a&t=136340&page=2&pp=10
by Larry Barowski
Have a good one Emre Simtay
public static void drawDiamond(int n) {
for(int i = 0; i < n * n; i++)
System.out.print((Math.abs(i%n - n/2) + Math.abs(i/n - n/2) > n/
2?"":"*") + (i%n == n-1?"\n":""));
}
Reference:
http://www.codecomments.com/archive245-2004-8-265254.html
by
Larry Barowski
Have a good day
Emre SIMTAY
public class ShowMe {
public static void main(String [] args) {
String allStars = "*****";
int count = 0;
for (int x=1;x<10;x++) {
count = x;
if (x > 5)
count = x + (( -2 * x) + 10);
StringWriter sb = new StringWriter(count);
sb.write(allStars, 0, count);
String correctNumberOfStars = sb.toString();
System.out.println(correctNumberOfStars);
}
}
}
Lex pointed out to me that my expression was a bit more complex than it
needed to be, but I didn't think my effort was too bad for about 45
minutes of work. Most of that time was working out the formula. Once I
had that figured out, it wasn't too difficult to search the java docs
for a "string" type class that had a method I needed.
Hey, there. How's the homework coming along?
It's the abs() method of the java.lang.Math class.
Hint: The Java API docs are filled with useful information.
How is your maths skill? What sort of background do you have with respect to
maths?
--
Lew
No, it's a free Bluebottle account - website based anti-spam email
service. You can sign up yourself for nothing if you like it.
lex
This solution crossed my mind too! It's quite nice for the avoidance of
an if.
lex
>
Why not do something like this?
System.out.println("* ");
System.out.println("** ");
System.out.println("*** ");
System.out.println("**** ");
System.out.println("*****");
System.out.println("**** ");
System.out.println("*** ");
System.out.println("** ");
System.out.println("* ");
Sorry; I couldn't resist. ;-)
Iterate i from 0 to 9, write the number of asterisks min(i, 10-i) in each iteration?
That's two for-loops: the 1 that you explicitly specified, and the one
to "write the NUMBER of asterisks", unless, of course, you have
another way, which is plausible, but you didn't specify; previous
suggestions have been aimed at eliminating that latter for-loop.
Dustan wrote:
> That's two for-loops: the 1 that you explicitly specified, and the one
> to "write the NUMBER of asterisks", unless, of course, you have
> another way, which is plausible, but you didn't specify; previous
> suggestions have been aimed at eliminating that latter for-loop.
Number of asterisks may be a loop, but it isn't a for-loop, it's a
String.substring() loop.
--
Lew
Also, you specified a *nested* for loop, which means a loop contained within a loop.
Of course, you could always eliminate the inner loop with:
String outText = "*****".substring(0, min(i, 10-i));
If you could work out how to print out:
I was thinking of a slightly different approach (untested):
public static void main(String [] args)
{
String allStars = "*****";
for ( int ix = -4; ix < 5; ++ix )
{
System.out.println( allStars.substring( 0, 5 - Math.abs(ix) );
}
}
--
Lew