/* RandomExercise.java by Craig Persiko
Solution to in-class exercise in CS 111A:
Write a program to generate random integers
between 5 and 10 (inclusive), showing them
on the screen, and stopping when the same number
is generated twice in a row. At the end, output
the largest number that was generated.
*/
class RandomExercise
{
public static void main(String[] args)
{
final int SMALLEST = 5, LARGEST = 10;
int rand=SMALLEST-1, lastRand, max=SMALLEST;
do
{
lastRand = rand;
rand = SMALLEST + (int)( (LARGEST-SMALLEST+1) * Math.random());
System.out.print(rand + " ");
if (rand > max)
max = rand;
}while(rand != lastRand);
System.out.println("\nThe largest number was " + max);
}
}
/* Sample Output:
[cpersiko@milhouse ~]$ java RandomExercise
9 10 8 6 8 6 6
The largest number was 10
[cpersiko@milhouse ~]$ java RandomExercise
5 5
The largest number was 5
[cpersiko@milhouse ~]$ java RandomExercise
7 8 7 6 9 7 9 8 7 7
The largest number was 9
[cpersiko@milhouse ~]$
*/
syntax highlighted by Code2HTML, v. 0.9