Control - While Loops
Control - While Loops
- while loops
- infinite loops
- no loops
WhileExamples.java
Deitel 4
Classwork
Create a project, and in it a class with a main.
We will be using the Scanner class to read from the user. At the top of your
main class, after the package statement, paste
import java.util.Scanner;
As the first line inside your main method, paste
Scanner scan = new Scanner(System.in);
In order to read from the user, we use the Scanner like this:
int x = scan.nextInt(); // read an int
double d = scan.nextDouble(); // read a double
String s = scan.next(); // read a String
Overall hint for this (and most) assignments: local variables are free!
If you need another variable to hold a value in order to do a task, add it!
(Instance variables are different, you can't just keep adding them.)
Part A
☑ In your main,
create a String variable secretPet and set it to the name of some
type of animal.
Ask the user to guess what type the pet is, and read in
their response (you need a variable for this). Put this code in a loop
that repeats until they guess the right string. (hint: we need to read the
value from the user inside the loop, but the loop needs to check the
user's value to start off with, so you will need to give it some dummy value to
start the loop)
After the loop, congratulate them on figuring it out.
Part
B
☑ In your main (or you may add
another class with main if you prefer; if so, give them names so it is clear
which is which),
create a double variable balance, that starts as 10.
We want to read in a deposit amount from the user and if it is a positive
number, add it to the balance, but as long as they give us negative numbers, we
want to keep asking them to try again. To do this:
☑ Ask the user for how much to add
to balance (you will need another variable), and read in the number.
☑ Using a loop, as long as the
number they entered was invalid, make them keep entering, remembering that you need
to print to ask every time
(If you don't need to say something different (like be rude or say explicitly it is invalid)
you can just read in within the loop. If you want to
be polite the first time but rude later, you'll need to read in once before the
loop and again inside the loop.)
☑ Once you finally have a valid
value, add that to balance and print the result.
Part C
Continuing in the same main, we will use loops to do a simple menu.
The options are:
0. Quit
1. Show Balance
2. Clear Balance
3. Deposit
We want to repeatedly show these options, get input from the user, and act on
their choice, until they choose 0 to quit.
To do this:
☑ Create an int variable userInput
and set it to any number but 0 (since 0 is the quit value)
☑ Next, set up the loop so we repeat until the
user input is the choice for
quit (0).
☑ Then, inside the loop, print the
options, as shown above, and read in the user's input
Later inside the same loop, use conditionals to decide what to do based on
user input:
☑ If they enter 1, print out the current value of balance.
☑ If they enter 2, set the balance to 0.
☑ If they enter 3, we want to get
a deposit amount. Move the code your wrote for depositing in part B so that
it happens here instead of before the loop.
(If they give an invalid choice, you can just ignore it. )
[EC] Add another option 4. Withdraw, which does the same as Deposit but gets
an amount to subtract from the user. The number must be positive but also
not larger than the current balance (loop until the value is acceptable)