Drawing a Christmas tree w/ nested for loops and from JavaScript to Java

I need help in making this code compatible with Java (I use BlueJ):

It's JavaScript...

Thanks in advance!

<HTML>

<BODY>

<CENTER>

<SCRIPT>

var width=1;

for (i=0; i <= 5 ; i++)

{

for (x=0; x<=4; x++)

{

for (y=1; y<=width; y++)

{

var Number=Math.random()*10;

var Ornament=Math.round(Number);

if (Ornament<=1)

{

document.write("O");

}

if (Ornament>=2)

{

document.write(" X");

}

}

document.write("
");

width=width+1;

}

width=width-2;

}

</SCRIPT>

</CENTER>

</BODY>

</HTML>

[768 byte] By [bronze-starDukes] at [2007-11-26 12:13:01]
# 1

What do you mean "compatible with Java"? Java doesn't enter into this at all.

Are you asking how to translate this code to Java?

If so, where you have "
", you'll want a println rather than a print (as in System.out.print).

The second tricky part is centering. The browser handled that for you here.

You'll have to either do it yourself, or find another Java API tool to do it for you.

If I were you I'd look at the API docs for PrintStream.format(), I think it is. It's possible that the %s conversion has a "center" option. Or maybe something else there does.

If there is no textual centering tool, you can build one yourself by using StringBuffer or StringBuilder and padding with spaces, I guess.

Otherwise it should map across pretty easily.

platinumsta at 2007-7-7 14:13:44 > top of Java-index,Archived Forums,Socket Programming...
# 2
I guess if you're using format(), you might find it easier to explicitly print a "\n" rather than use println, maybe.Of course, if you want to do it on anything other than the console, you'll have to use a GUI. Look into AWT or Swing, preferably Swing.
platinumsta at 2007-7-7 14:13:44 > top of Java-index,Archived Forums,Socket Programming...