Java Reference
In-Depth Information
System.out.println("Please enter a number between 1 and 10 and "
+ "then press Enter");
num = console.nextInt();
When these two statements execute in the order given, first the output statement causes
the following line of text to appear on the screen:
Please enter a number between 1 and 10 and then press Enter
After seeing this line, an example of a prompt line, users know that they must enter a number
and press the Enter key. If the program contained only the second statement, users would
not know that they must enter a number, and the computer would wait indefinitely for the
input. The preceding output statement is an example of a prompt line.
In a program, whenever users must provide input, you should include the necessary
prompt lines. The prompt lines should include sufficient information about what input is
acceptable. For example, the preceding prompt line not only tells the user to input a
number, but also informs the user that the number should be between 1 and 10 .
FORM AND STYLE
You might think that Java has too many rules. However, in practice, the rules give Java a
great degree of freedom. For example, consider the following two ways of declaring
variables:
int feet;
int inch;
double x;
double y;
and:
int feet; int inch; double x; double y;
The computer has no difficulty understanding either of these formats, but the first form is
easier for a person to read and follow.
What about blank spaces? Where are they significant and where are they meaningless?
Consider the following two statements:
int a;
and:
int
a;
Both of these declarations mean the same thing. Here, the extra blanks between the
identifiers in the second statement are meaningless. On the other hand, consider the
following statement:
inta;
Search WWH ::




Custom Search