OK, so in the last application, you created a Java program that output one line of text to a screen. In this program, you learn to create multiple lines by using the newline character --> "\n"
The newline character forces the cursor inside the system console to go down to the next line. This can be useful if you want to display a column of number values, rather than have them all strung together on the same line.
You will use the newline character in conjunction with a little device called a concatenator, which looks like a "+" sign. This concatenator can be used to glue together several different strings into one. It's also a cheap knock off way of converting a number in Java into text: anytime you have a String on either side of a concatenator, the whole thing automatically becomes a String.
Anyway, here is the program:
public class HelloPrime
{
public static void main( String args[] )
{
System.out.println( "1"
+ "\n" + "2"
+ "\n" + "3"
+ "\n" + "5"
+ "\n" + "7"
+ "\n" + "11"
+ "\n" + "13"
+ "\n" + "17"
);
}
}
Monday, January 26, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment