Click to See Complete Forum and Search --> : Simple Java


zacaritex
02-18-2003, 02:09 PM
Hi
I have a simple Java question.

I want to create a Java Application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the Java application using the following methods:

Using one (1) System.out statement.
This I got with no problem

Using four (4) System.out statements
This one I cant figure out....and I read the chapter 4 times.
no clue.
Help
Thanks.

spufi
02-18-2003, 08:12 PM
What you need to do is not include println which drops the next statement down to the next line. Use print instead and it will remain on the same line. See code below for having it in one System.out and for 4 System.out statements. I'm also curious as to what book you are using.

public class Numbers
{
public static void main(String[] args)
{
int numOne = 1;
int numTwo = 2;
int numThree = 3;
int numFour = 4;

System.out.println(numOne + " " + numTwo + " " + numThree + " " + numFour);
System.out.print(numOne + " ");
System.out.print(numTwo + " ");
System.out.print(numThree + " ");
System.out.print(numFour);
}
}