Question

In: Computer Science

You've devised a new twist on the traditional 'What number am I thinking of?' game to...

You've devised a new twist on the traditional 'What number am I thinking of?' game to help your cousins learn their 7 times tables! Write a game that asks the user to guess the number you are thinking of. (For this game, the number will always be 42.)

The user is allowed 10 guesses, and makes a 'Mistake!' if they guess a number that isn't a multiple of 7. A user can make a maximum of one mistake, otherwise they lose the game. When the game is over, you should always print out That was fun.

Here is an example:

Guess a multiple of 7: 14
Nope!
Guess a multiple of 7: 32
Mistake! That number isn't a multiple of 7.
Guess a multiple of 7: 28
Nope!
Guess a multiple of 7: 86
Another mistake. Too bad.
That was fun.

Here is another example:

Guess a multiple of 7: 7
Nope!
Guess a multiple of 7: 14
Nope!
Guess a multiple of 7: 126
Nope!
Guess a multiple of 7: 133
Nope!
Guess a multiple of 7: 70
Nope!
Guess a multiple of 7: 77
Nope!
Guess a multiple of 7: 63
Nope!
Guess a multiple of 7: 35
Nope!
Guess a multiple of 7: 126
Nope!
Guess a multiple of 7: 77
Nope!
No guesses left!
That was fun.

If the user correctly enters 42, your program should print out You won! instead of Nope!, and then not ask for any more guesses. For example:

Guess a multiple of 7: 42
You won!
That was fun.

Solutions

Expert Solution

import java.util.Scanner;

public class GuessMultiple {

   public static void main(String[] args) {
       // Scanner class object to take user input
       Scanner scan= new Scanner(System.in);
      
       // Target value to be matched
       int targetNumber = 42;
       // Total guesses user get
       int totalGuesses = 10;
       // user input
       int guessedNumber = -1;
       // Total mistakes made by user
       int totalMistakes = 1;
      
       // Iterate loop till totalGuesses>0
       while(totalGuesses>0) {
           // Take user input
           // Decrement totalGuesses by 1
           System.out.print("Guess a multiple of 7: ");
           guessedNumber = scan.nextInt();
           totalGuesses--;
          
           // If number matched, print message and exit program
           if(guessedNumber==targetNumber) {
               System.out.println("You won!\nThat was fun.");
               System.exit(0);
           }
           // Else if, multiple of 7, display message
           else if(guessedNumber%7==0) {
               System.out.println("Nope!");
           }
           // If not multiple of 7 means first mistake
           // increment totalMistakes by 1 and print message
           else if(totalMistakes==1){
               System.out.println("Mistake! That number isn't a multiple of 7.");
               totalMistakes+=1;
           }
           // If totalMistakes is 2 means another mistake is made
           // Print message and exit
           else if(totalMistakes==2){
               System.out.println("Another mistake. Too bad.\nThat was fun.");
               System.exit(0);
           }
       }
       // After loop, means user lost and guesses are complete
       System.out.println("No guesses left!\nThat was fun.");
   }
}

OUTPUT-


Related Solutions

I am working on bomb lab phase 4, but I was thinking the first number may...
I am working on bomb lab phase 4, but I was thinking the first number may be equal to zero. but I have no idea how to figure out the second number, hope someone could help me out Dump of assembler code for function phase_4: 0x00000000004010e8 <+0>:   sub $0x18,%rsp    0x00000000004010ec <+4>:   lea 0x8(%rsp),%rcx    0x00000000004010f1 <+9>:   lea 0xc(%rsp),%rdx    0x00000000004010f6 <+14>:   mov $0x40256a,%esi    0x00000000004010fb <+19>:   mov $0x0,%eax    0x0000000000401100 <+24>:   callq 0x400ac8 <__isoc99_sscanf@plt>    0x0000000000401105 <+29>:   cmp $0x2,%eax...
how am i not thinking like an economist? i bought a new house this year, but...
how am i not thinking like an economist? i bought a new house this year, but that house (which was new to me) was originally built in the year 1908. i would classify this as a consumption expenditure for this years GDP. Explain why this is not correct by describing what is and what is not included in this years GDP
You've just unboxed a new board game by legendary creator Stefan Feld. In the game, you...
You've just unboxed a new board game by legendary creator Stefan Feld. In the game, you roll a seven-sided, fair die with 4 red faces, 2 green faces, and 1 blue face. Your roll dictates how much gold you receive for rolling: red gives 1 gold, green gives 2 gold, and blue gives 4 gold. a. Define a random variable for this setting, state the support, and find its probability model. b. Find the expected value and variance of your...
I am doing a kids fishing game. right now I am doing it with 15 fish...
I am doing a kids fishing game. right now I am doing it with 15 fish and number them 1-5. prize one the lowest value and prize 5 the highest value. What do I need to do for this? You can work with a partner or by yourself. I want you to invent a game with at least 12 different possible monetary outcomes. These outcomes need to include prizes other than money. You are going to charge people money (you...
I am trying to create bit masks to create a new binary number with the original...
I am trying to create bit masks to create a new binary number with the original bits' orders turned around, and concataneted. For example, int test=0b01100010001010001001(20 bits) now, I need bit masking to make it: int result = 20th bit, 10-1 bits, 11th bit, 19-12 bits, in this order. So in this case, answer would be: 0(20th bit)10100010(10-1 bits)0(11th bit)1011000100(19-12 bits). Im trying to do something like int res |= test & 0x7F800; res |= test& 0x400; Can't seem to...
Hypothetical: I am an administrator for a hospital. I am in charge of developing a new...
Hypothetical: I am an administrator for a hospital. I am in charge of developing a new project/department specializing in outpatient corneal transplant surgery. What would be the best way to finance this project? Debt financing, equity financing, etc? And how do I go about this?
I am convinced that my friend is cheating in our board game. I think that their...
I am convinced that my friend is cheating in our board game. I think that their die is not a normal six sided die because it rolls six far too often. Let ? be the proportion of rolls that are sixes. I plan on recording their result on their next 80 turns. (I will keep track of the number of sixes.) Draw the sampling distribution.
Please show all the steps you've done to reach the final answer. I am trying to...
Please show all the steps you've done to reach the final answer. I am trying to learn, so please show your work. Typing your answer is important. 1) Find the present worth of earthmoving equipment that has a first cost today of $149,000, an annual operating cost of $65,000, and a salvage value of 20% of the first cost after 5 years, these estimates being in future dollars. Assume that the real interest rate is 8% per year and that...
Please show all the steps you've done to reach the final answer. I am trying to...
Please show all the steps you've done to reach the final answer. I am trying to learn, so please show your work. Typing your answer is important. Parker Hannifin of Cleveland, Ohio, manufactures CNG fuel dispensers. It needs replacement equipment to streamline one of its production lines for a new contract, but it plans to sell the equipment at or before its expected life is reached at an estimated market value for used equipment. Select between the two options using...
Please show all the steps you've done to reach the final answer. I am trying to...
Please show all the steps you've done to reach the final answer. I am trying to learn, so please show your work. Typing your answer is important. 1) There are two potential locations to construct an urgent care walk-in clinic to serve rural residents. Use B/C analysis to determine which location, if any, is better at an interest rate of 8% per year. Location 1 2 Initial Cost, $ 1,200,000 2,000,000 Annual M&O Cost, $/Year 80,000 75,000 Annual Benefits, $/Year...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT