Skip to main content
Type in the following code, and get it to compile. This assignment will help you learn how to make a loop, so that you can repeat a section of code over and over again!Sample Output
WELCOME TO THE BANK OF MITCHELL.
ENTER YOUR PIN: 90210

INCORRECT PIN. TRY AGAIN.
ENTER YOUR PIN: 11111

INCORRECT PIN. TRY AGAIN.
ENTER YOUR PIN: 12345

PIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.
Notice what happens when we type the correct PIN on the first try:
WELCOME TO THE BANK OF MITCHELL.
ENTER YOUR PIN: 12345

PIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.
Modify your previous number-guessing game so that they can guess until they get it right. That means it will keep looping as long as the guess is different from the secret number. Use a while loop.Sample Output
I have chosen a number between 1 and 10. Try to guess it.
Your guess: 5
That is incorrect. Guess again.
Your guess: 4
That is incorrect. Guess again.
Your guess: 8
That is incorrect. Guess again.
Your guess: 6
That's right! You're a good guesser.
Modify your dice game from last time so that it keeps rolling until they get doubles (the same number on both dice).Notice that since there’s no user input, this will happen very quickly (all the rolls will happen one right after the other).Sample Output
HERE COMES THE DICE!

Roll #1: 3
Roll #2: 5
The total is 8!

Roll #1: 6
Roll #2: 1
The total is 7!

Roll #1: 2
Roll #2: 5
The total is 7!

Roll #1: 1
Roll #2: 1
The total is 2!
Type in the following code, and get it to compile. This assignment shows you how we can abuse a while loop to make something repeat an exact number of times.Normally, while loops are best for repeating as long as something is true:
  • Keep going as long as they haven’t guessed it.
  • Keep going as long as you haven’t got doubles.
  • Keep going as long as they keep typing in a negative number.
  • Keep going as long as they haven’t typed in a zero.
But sometimes, we know in advance how many times we want to do something.
  • Do this ten times.
  • Do this five times.
  • Pick a random number, and do it that many times.
  • Take this list of items, and do it one time for each item in the list.
We can do that sort of thing with a while loop, but we have to use a counter. A counter is a number variable (int or double) that starts with a value of 0, and then we add 1 to it whenever something happens. So, here, we’re going to be adding 1 to the counter everytime we repeat the loop. And when the counter reaches a predetermined value, we’ll stop looping.Sample Output
Type in a message, and I'll display it five times.
Message: All work and no play makes Jack a dull boy.
1. All work and no play makes Jack a dull boy.
2. All work and no play makes Jack a dull boy.
3. All work and no play makes Jack a dull boy.
4. All work and no play makes Jack a dull boy.
5. All work and no play makes Jack a dull boy.
This assignment will help you learn how to make a loop, so that you can repeat a section of code over and over again!Sample Output
WELCOME TO THE BANK OF MITCHELL.
ENTER YOUR PIN: 10101

INCORRECT PIN. TRY AGAIN.
ENTER YOUR PIN: 23232

INCORRECT PIN. TRY AGAIN.
ENTER YOUR PIN: 99999

YOU HAVE RUN OUT OF TRIES. ACCOUNT LOCKED.
Modify your previous number-guessing game so that they can guess until they get it right AND count the number of tries it takes them to guess it.Sample Output
I have chosen a number between 1 and 10.  Try to guess it.
Your guess: 5
That is incorrect.  Guess again.
Your guess: 4
That is incorrect.  Guess again.
Your guess: 8
That is incorrect.  Guess again.
Your guess: 6
That's right!  You're a good guesser.
It only took you 4 tries.
Write a program that picks a random number from 1-100. The user keeps guessing as long as their guess is wrong, and they’ve guessed less than 7 times. If their guess is higher than the number, say “Too high.” If their guess is lower than the number, say “Too low.” When they get it right, the game stops. Or, if they hit seven guesses, the game stops even if they never got it right.This means your while loop will have a compound condition using &&.Sample Output
I'm thinking of a number between 1-100.  You have 7 guesses.
First guess: 50
Sorry, you are too low.
Guess # 2: 75
Sorry, you are too low.
Guess # 3: 87
Sorry, that guess is too high.
Guess # 4: 82
Sorry, you are too low.
Guess # 5: 84
You guessed it!  What are the odds?!?
I'm thinking of a number between 1-100.  You have 7 guesses.
First guess: 1
Sorry, you are too low.
Guess # 2: 2
Sorry, you are too low.
Guess # 3: -8
Sorry, you are too low.
Guess # 4: 0
Sorry, you are too low.
Guess # 5: 7
Sorry, you are too low.
Guess # 6: 612
Sorry, that guess is too high.
Guess # 7: -523
Sorry, you didn't guess it in 7 tries.  You lose.
Write a program that gets several integers from the user. Sum up all the integers they give you. Stop looping when they enter a 0. Display the total at the end.You must use a while loop.Sample Output
I will add up the numbers you give me.
Number: 6
The total so far is 6
Number: 9
The total so far is 15
Number: -3
The total so far is 12
Number: 2
The total so far is 14
Number: 0

The total is 14.
I will add up the numbers you give me.
Number: 1
The total so far is 1
Number: 2
The total so far is 3
Number: 3
The total so far is 6
Number: 4
The total so far is 10
Number: 5
The total so far is 15
Number: 0

The total is 15
Write a program to take the square root of a number typed in by the user. Your program should use a loop to ensure that the number they typed in is positive. If the number is negative, you should print out some sort of warning and make them type it in again.Note that it is possible to do this program with either a while loop or a do-while loop. (Though personally, I think this one is easier with a while loop.)You can get the square root of a number n with Math.sqrt(n). Make sure you don’t do this until the loop is done and you know for sure you’ve got a positive number.Sample Output
SQUARE ROOT!
Enter a number: 9
The square root of 9 is 3.0.
SQUARE ROOT!
Enter a number: 2
The square root of 2 is 1.4142135623730951.
SQUARE ROOT!
Enter a number: -9
You can't take the square root of a negative number, silly.
Try again: -10
You can't take the square root of a negative number, silly.
Try again: 10
The square root of 10 is 3.1622776601683795.
Write a program to allow the user to enter three integers. You must use do-while or while loops to enforce that these integers are in ascending order, though duplicate numbers are allowed.Tell the user whether or not these integers would represent the sides of a right triangle.Sample Output
Enter three integers:
Side 1: 4
Side 2: 3
3 is smaller than 4.  Try again.
Side 2: -9
-9 is smaller than 4.  Try again.
Side 2: 5
Side 3: 1
1 is smaller than 5.  Try again.
Side 3: 5

Your three sides are 4 5 5
NO!  These sides do not make a right triangle!
Enter three integers:
Side 1: 6
Side 2: 8
Side 3: 10

Your three sides are 6 8 10
These sides *do* make a right triangle.  Yippy-skippy!
Take any natural number n.
  • If n is even, divide it by 2 to get n / 2.
  • If n is odd, multiply it by 3 and add 1 to get 3n + 1.
  • Repeat the process indefinitely.
In 1937, Lothar Collatz proposed that no matter what number you begin with, the sequence eventually reaches 1. This is widely believed to be true, but has never been formally proved.Write a program that inputs a number from the user, and then displays the Collatz Sequence starting from that number. Stop when you reach 1.Sample Output Here’s an example of the expected output, assuming I start with 6 and print tabs between each number.
Starting number: 6
6     3      10     5      16     8      4      2     1
Or, starting with a different number:
Starting number: 11
11    34    17    52    26    13    40    20    10    5
16    8     4     2     1
Some numbers take quite a while to reach 1:
Starting number: 27
27    82    41    124   62    31    94    47    142   71
214   107   322   161   484   242   121   364   182   91
274   137   412   206   103   310   155   466   233   700
350   175   526   263   790   395   1186  593   1780  890
445   1336  668   334   167   502   251   754   377   1132
566   283   850   425   1276  638   319   958   479   1438
719   2158  1079  3238  1619  4858  2429  7288  3644  1822
911   2734  1367  4102  2051  6154  3077  9232  4616  2308
1154  577   1732  866   433   1300  650   325   976   488
244   122   61    184   92    46    23    70    35    106
53    160   80    40    20    10    5     16    8     4
2     1
Make another short “Choose Your Own Adventure” game. However, this time you need to use a loop so that they can freely move from room to room and back again.There need to be at least six rooms or destinations, and at least two different ways for the game to end.Files NeededSample Output
MITCHELL'S TINY ADVENTURE 2!

You are in a creepy house!  Would you like to go "upstairs" or into the
"kitchen"?
> kitchen

There is a long countertop with dirty dishes everywhere.  Off to one side
there is, as you'd expect, a refrigerator.  You may open the "refrigerator"
or go "back".
> back

You are in a creepy house!  Would you like to go "upstairs" or into the
"kitchen"?
> upstairs

Upstairs you see a hallway.  At the end of the hallway is the master
"bedroom".  There is also a "bathroom" off the hallway.  Or, you can
go back "downstairs". Where would you like to go?
> downstairs

You are in a creepy house!  Would you like to go "upstairs" or into the
"kitchen"?
> kitchen

There is a long countertop with dirty dishes everywhere.  Off to one side
there is, as you'd expect, a refrigerator.  You may open the "refrigerator"
or go "back".
> refrigerator

Inside the refrigerator you see food and stuff.  It looks pretty nasty.
Would you like to eat some of the food? ("yes" or "no")
> yes

The food is slimy and foul, but you manage to choke it down. Your stomach
starts jumping like a frog in hot water.  You feel faint. Sliding to the
floor, the darkness closes in.

You have died.
Write a program that starts with three “piles” of 3 counters each. Let the player choose piles and remove counters until all the piles are empty.
  1. Start by placing counters (coins or toothpicks or something) into 3 piles.
  2. The player picks a pile, then removes one or more counters from that pile. (It’s okay to take the whole pile.)
  3. The player picks a new pile, then removes one or more counters from that pile. (It’s okay to pick the same pile as before.)
  4. Once all piles are empty, the game stops.
You do not need to check for errors like a wrong pile name, or if someone tries to take more counters from the pile than the pile has.Sample OutputHere is an example game, with starting piles of 3 counters.
A: 3	B: 3	C: 3

Choose a pile: A
How many to remove from pile A: 2

A: 1	B: 3	C: 3

Choose a pile: C
How many to remove from pile C: 3

A: 1	B: 3	C: 0

Choose a pile: B
How many to remove from pile B: 1

A: 1	B: 2	C: 0

Choose a pile: A
How many to remove from pile A: 1

A: 0	B: 2	C: 0

Choose a pile: B
How many to remove from pile B: 1

A: 0	B: 1	C: 0

Choose a pile: C
How many to remove from pile C: 2

A: 0	B: 1	C: -2

Choose a pile: B
How many to remove from pile B: 1

A: 0	B: 0	C: -2

All piles are empty. Good job!
Nim is a strategy game between two players.
  1. Start by placing counters (coins or toothpicks or something) into 3 piles.
  2. Player #1 picks a pile, then removes one or more counters from that pile. (It’s okay to take the whole pile.)
  3. Player #2 picks a pile, then removes one or more counters from that pile.
  4. Player #1 plays again. (It’s okay to choose a different pile this time.)
  5. Whichever player is forced to take the last counter is the LOSER.
Write a program that allows two human players to play Nim against each other. The program should detect when the last counter has been taken and declare a winner.At first, don’t worry about detecting cheating. That is one of the bonus options.Sample OutputHere is an example game, with starting piles of 3, 4, and 5 counters.
Player 1, enter your name: Alice
Player 2, enter your name: Bob

A: 3	B: 4	C: 5

Alice, choose a pile: A
How many to remove from pile A: 2

A: 1	B: 4	C: 5

Bob, choose a pile: C
How many to remove from pile C: 3

A: 1	B: 4	C: 2

Alice, choose a pile: B
How many to remove from pile B: 1

A: 1	B: 3	C: 2

Bob, choose a pile: B
How many to remove from pile B: 1

A: 1	B: 2	C: 2

Alice, choose a pile: A
How many to remove from pile A: 1

A: 0	B: 2	C: 2

Bob, choose a pile: B
How many to remove from pile B: 1

A: 0	B: 1	C: 2

Alice, choose a pile: C
How many to remove from pile C: 2

A: 0	B: 1	C: 0

Bob, choose a pile: B
How many to remove from pile B: 1

A: 0	B: 0	C: 0

Alice, there are no counters left, so you WIN!