48. Enter Your PIN - Link
- Task
- Answer
- Question
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 OutputNotice what happens when we type the correct PIN on the first try:
49. Keep Guessing - Link
- Task
- Answer
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 Output50. Dice Doubles - Link
- Task
- Answer
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
51. Counting with a While Loop - Link
- Task
- Answer
- Question
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.
- 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.
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 Output52. PIN Lockout - Link
- Task
- Answer
- Question
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
53. Number-Guessing with a Counter - Link
- Task
- Answer
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
54. Hi-Lo with Limited Tries - Link
- Task
- Answer
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 Output55. Adding Values in a Loop - Link
- Task
- Answer
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 Output60. Safe Square Root - Link
- Task
- Answer
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 Output61. Right Triangle Checker - Link
- Task
- Answer
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 Output62. Collatz Sequence - Link
- Task
- Answer
- Bonus
Take any natural number n.Or, starting with a different number:Some numbers take quite a while to reach 1:
- 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.
63. Short Adventure 2: With a Loop - Link
- Task
- Answer
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 Needed
- Adventure2.java - empty shell
- Adventure2Example.java - tiny sample game
63b. Baby Nim - Link
- Task
- Answer
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.
- Start by placing counters (coins or toothpicks or something) into 3 piles.
- The player picks a pile, then removes one or more counters from that pile. (It’s okay to take the whole pile.)
- 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.)
- Once all piles are empty, the game stops.
63c. Nim - Link
- Task
- Answer
Nim is a strategy game between two players.
- Start by placing counters (coins or toothpicks or something) into 3 piles.
- Player #1 picks a pile, then removes one or more counters from that pile. (It’s okay to take the whole pile.)
- Player #2 picks a pile, then removes one or more counters from that pile.
- Player #1 plays again. (It’s okay to choose a different pile this time.)
- Whichever player is forced to take the last counter is the LOSER.