Java Reference
In-Depth Information
{
case
0:
evens++;
if
(number == 0)
zeros++;
break
;
case
1:
case
-1:
odds++;
}
//end switch
}
//end for loop
System.out.println();
//Step 4
System.out.println("There are " + evens + " evens, "
+ "which also includes "
+ zeros + " zeros");
System.out.println("Total number of odds is: " + odds);
}
}
Sample Run: (In this sample run, the user input is shaded.)
Please enter 20 integers, positive, negative, or zeros.
0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54
0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54
There are 13 evens, which also includes 6 zeros
Total number of odds is: 7
We recommend that you do a walk-through of this program using the above sample
input.
Note that the
switch
statement in Step 3c can also be written as an
if
...
else
statement as follows:
if
(number % 2 == 0)
{
evens++;
if
(number == 0)
zeros++;
}
else
odds++;
This section describes the third type of looping or repetition structure—a
do
...
while
loop. The general form of a
do
...
while
statement is:
Search WWH ::
Custom Search