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
Notice what happens when we type the correct PIN on the first try:
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
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
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
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
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
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
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
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
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
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.
Or, starting with a different number:
Some numbers take quite a while to reach 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
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.
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.