PDA

View Full Version : A simple Java program - how to change the possition display?


blankibaby
09-21-2005, 07:54 AM
hi i was trying out a simple program using loops displaying the "*" sign
line by line with space after each line.
however, i would like to try displaying the output in the other way instead
but not sure how :confused: ... Can anyone guide me and pin point it to me?
And i wonder why my blank space line some are so wide apart and some
are not?? :confused:


My current output looks like this:

*

**

***

****
*****

notice there is such big gabs but not the last 2 lines....did i wrote
something wrong?

I would like to display the asteric sign "*"
to the right side instead left side like above.

This is my coding

public class exercise2
{
public static void main(String args[])
{
int currwidth;

for (int i=0; i<6; i++)
{
currwidth = 6 - i;


for (int x=0; x<currwidth; x++)
{
System.out.println(" ");
}


for (int y=0; y<i; y++)
{
System.out.print("*");
}
}
}
}

Roelf
09-21-2005, 09:21 AM
the difference between System.out.println() and System.out.print() is obvious, println() prints the given string and then a carriage return/linefeed, so the next print appears on the first position of the next line. print() just prints the character and the next print appears just after the given string.

Now you need to decide when you want to appear the next output on the next line, before that, you need to execute the System.out.println() method. in other occasions, use the System.out.print() method

<cough>homework?</cough>

KeZZeR
09-21-2005, 10:20 AM
for(int i = 1; i <= 6; i++) {
for(int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println(" ");
}


Something like that? I can't remember, they were simple excercises at the start though, just requires a little thinking and just doing a trace route of the program

blankibaby
09-21-2005, 07:25 PM
thanks guys! :)

i finally manage to do it after re-doing it.
LOL nope is not a homework, just an exercise from a book.