While Loop programs

Read the class notes on while loops. Read the section on break; continue;
Copy the program at the bottom of the while loop class note page and run it.

There are three (plus one) programs to do:

While50.java (everybody do this)

Write a while loop that will print out the numbers 1-50 (i.e. it loops 50 times printing out a number)

This is just marked for completion

GuessNumber.java (everybody do this)

Write a guess a number game.
a) make a random number between 1 and 100 using this code: int secret = (int) (Math.random() * 100) +1;
b) when the user enteres a guess, say if it is too high, too low or just right.
c) print out the number of guesses once the user wins.

This will be marked more carefully for formatting, variable names, etc.

While99.java (everybody do this)

Start learning how to use break; and continue

 

Here's the outline of how to do it (but there is one problem you'll have to fix to get it to work)

Example:

n	total
---------------
2	2
4	6
20	6	(multiples of 5 are ignored)
11	17
6	17	(multiples of 3 are ignored)
-99	17	(exit the loop)

Total = 17

PlusMinus.java (enrichment)

Write a program that gets numbers (positive integers) from the user. Alternate between adding and subtracting each successive number. Print out the total each time. Quit when the total equals 100.

While_q.java (Try and do this, but not marked)