Java Reference
In-Depth Information
Lab 3.5: The Powerball Lottery
Powerball is a lottery played in many of the United States. The lottery
numbers are chosen randomly from two containers of numbered balls.
Five white balls are chosen from a container of 49 balls, and one red ball
is chosen from a container of 42 balls.
Write a program that simulates the selection of the Powerball lottery
numbers. Use the Math.random() function to simulate the random selec-
tion of a numbered ball. Keep in mind that you cannot just randomly
generate six numbers. The five white numbers must be unique and
between 1 and 49. (After a ball is removed from the container, it can't be
selected again.) The one red number must be between 1 and 42. Note that
the red number can possibly be the same number as one of the selected
white balls.
Summary
The && and || operators are the short-circuit
and
and
or
operators,
respectively.
■■
The if and if/else statements are used for decision making.
■■
A switch statement consists of one or more case statements. The value
in a switch statement can be of type byte, short, int, or char. The value
of a case must be either a literal or a constant expression.
■■
A while loop is used for repeating tasks. The statements in the body of
a while loop execute until the Boolean expression of the while loop is
false. It is possible to write an infinite while loop and also a while loop
that never executes.
■■
A do/while loop is similar to a while loop, except that a do/while loop
is guaranteed to execute at least once.
■■
A for loop is useful when you know how many times a task is to be
repeated.
■■
The break keyword terminates the execution of a loop. The continue
keyword causes the loop to jump to the next iteration of the loop.
■■