This program is supposed to compute the sum of the first 5 squares, but it has multiple issues.
int n = 0; int i = 0; while (true); { n = i * i; i++; if (i == 5) { break; } } System.out.println(n);
Identify the bugs and suggest a fix.
Recall that we can use indefinite loops (while
or do { ... } while
loops)
when the number of iterations that we need to make is not obvious or
impossible to determine before the loop begins. Often this is used in a
program that takes input from the keyboard, file or the network, when the
data being recieved by the program could be of any length.
Let’s trace through an example of a while
loop.
int a = 4; int b = 12; while (b > a && a > 0) { System.out.println(b); b = b - a; a--; }
Using a piece of paper or the chalkboard, create a table that
resembles the following to trace the while
loop above. 1
a | b | b > a | a > 0 | b > a && a > 0 |
---|---|---|---|---|
4 | 12 | ... | ... | ... |
... | ... | ... | ... | ... |
Convert the following for
loop into an equivalent while
loop. 2
for (int i = 10; i < 24; i += 2) { System.out.println("i = " + i); }
Convert the following while
loop into an equivalent
do { ... } while
loop. 3
int num = 5; System.out.println(num); num++; while(num > 5 && num < 20){ System.out.println(num); num++; }
To exercise your skills in procedural decomposition, conditional execution,
indefinite loops and the Scanner
class, we will construct a number
guessing game. Write your class in a file named NumberGame.java
. The
program should have the following properties:
When the game begins, the program should ask the player for their preferred level of difficulty (a number between 1 and 3, where 1 is the easiest difficulty and 3 is the most difficult).
Then the program should pick a random number in the range specified by the level of difficulty, and begin allowing the player to make guesses.
Your guess is too high!
.Your guess is too low!
.The number of guesses that are given to the player is determined by the level of difficulty.
If the player does not guess the secret number before the number
of guesses has been exhausted, the program exits immediately with
the message Game over! The number was <x>!
, where <x>
is the
secret number.
If the player guesses the secret number before the number of
guesses has been exhausted, the program should exit after
printing You win! Good guessing!
.
If the player gets the number right on their first guess, the
program should print Wow! Are you cheating?
, before printing
the You win!
message.
Here are some additional hints.
random()
method
from the Math
class can be used. (Recall that the Math
class is
always available to you.) Math.random()
will return a double
between 0.0 and 1.0. You will need to write an expression that
converts this floating-point number into an integer in the correct
range as dictated by the level of difficulty of the game.You should copy this template code into a Java file and start by writing an unstructured solution, with the logic in
the main()
method. Then, test your program until you are convinced
it works, and break each piece into methods and combine repeated
blocks in conditionals, as we did yesterday with the buffet
exercise. 4
The code produces the following output:
12 8 5 3
The trace can be found below.
a | b | b > a | a > 0 | b > a && a > 0 |
---|---|---|---|---|
4 | 12 | true | true | true |
3 | 8 | true | true | true |
2 | 5 | true | true | true |
1 | 3 | true | true | true |
0 | 2 | true | false | false |
Here’s an equivalent while
loop.
int i = 10; while (i < 24) { System.out.println("i = " + i); i += 2; }
Here’s an equivalent do { ... } while
loop.
int num = 5; do { System.out.println(num); num++; } while (num > 5 && num < 20);
Here’s one possible
solution: NumberGame2.java
↩
Last updated on July 5, 2025.