Print Numbers on Screen Using Java
The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 2 Exercise 2.14.
Question: Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Use the following techniques:
a) Use one System.out.println statement.
b) Use four System.out.print statements.
c) Use one System.out.printf statement.
I hope this answer will help you to get the solution.public
class
DisplaynumbersOneToFour {
public
static
void
main (String [] args) {
System.out.println (
"This Application Displays The Numbers 1 To 4 Using "
+
"The System.out.println, System.out.print And System.out.prinf"
+
" Methods"
);
System.out.println ();
//displays a blank line
System.out.println (
"1 2 3 4"
);
//Print numbers 1 to 4
System.out.print (
"1 2 3 4\n"
);
//Print numbers 1 to 4
System.out.printf (
"%d%c%d%c%d%c%d\n"
,
1
,
' '
,
2
,
' '
,
3
,
' '
,
4
);
//Print numbers 1 to 4
}
}
0 komentar:
Posting Komentar