You will write a very (very) simple game, in which the user will select an anima
ID: 3541586 • Letter: Y
Question
You will write a very (very) simple game, in which the user will select an animal and
then guess how many legs that animal should have. (It is not a very challenging or
interesting game, is it?)
Specifications:
Your program knows three things: A dog has 4 legs, a chicken has 2 legs, and a fish
has 0 legs. That's it.
The program execution consists of two phases:
1. The user specifies what kind of animal he/she is playing with.
2. The user tries to guess how many legs that animal has.
The program begins by prompting the user with:
Choose an animal:
The user then enters the name of an animal. At this point one of two things could
occur: Either the animal selected is one of the three animals that the program knows
about, or the user has selected an animal that is not on this list.
When the Animal Selected is NOT One of the Three Known Animals
If the animal is not one of the three animals that the program knows about, the program
will print:
I don't know that animal. Do you want to try again? (y/n)
1If the user answers "y", then the program will again ask for the user to enter an animal,
and continue from there. If the user answers "n", then the program should terminate
with no further output.
When the Animal Selected is One of the Three Known Animals
The program then asks the user:
How many legs does a <AnimalSelected> have?
(In the line above, <AnimalSelected> represents the animal chosen by the user: either
"dog", "chicken", or "fish".)
The user now enters his/her choice, and the program will check whether or not the
guess is correct. If the user guesses correctly, the program will print out:
You win!
But if the user guesses incorrectly, then the program will print out:
You lose!
Either way, the program terminates after reporting whether or not the user wins or
loses.
Requirements:
* Your program must use the single class AnimalGame.
* You must use "named constants" for any literal values that will not change during
program execution.
* You do not need to worry about incorrect input (other than the user selecting an
invalid animal). You may assume that the responses the user enters are the right
types, and that he/she follows instructions.
* You should not modify the project specifications. For example, do not add extra
behavior or output.
* Just in case you know what an array is -- you may not use arrays (or any Java
collections of any kind) while implementing this project.
* Your program must terminate without using anything like System.exit(). (Don't
worry if you don't know what that is.)
* Document your source code and you program output with your name and Section.
2Sample Run:
The following provides a sample run of the program you are expected to write. Keep in
mind this is just an example and not the only scenario your program is expected to
handle.
Choose an animal: frog
I don't know that animal. Do you want to try again? (y/n) y
Choose an animal: chicken
How many legs does a chicken have? 100
You lose!
(Note that the portions above in italics represent the input entered by the user.)
Explanation / Answer
import java.util.Scanner;
public class AnimalGame
{
public static void main(String args[])
{
String input=" ";
Scanner u_input = new Scanner( System.in );
String flag="y";
int ck;
while(flag.equals("y"))
{
System.out.println("Choose an animal: ");
input = u_input.next();
if(input.equals("dog")) {
System.out.println("How many legs does a dog have?");
ck = u_input.nextInt();
if(ck==4)
System.out.println("You Win!!");
else
System.out.println("You Lose!!");
flag = "n";
}
else if(input.equals("chicken")) {
System.out.println("How many legs does a chicken have?");
ck = u_input.nextInt();
if(ck==2)
System.out.println("You Win!!");
else
System.out.println("You Lose!!");
flag = "n";
}
else if (input.equals("fish")) {
System.out.println("How many legs does a fish have?");
ck = u_input.nextInt();
if(ck==0)
System.out.println("You Win!!");
else
System.out.println("You Lose!!");
flag = "n";
}
else
{
System.out.println("I don't know that animal. Do you want to try again? (y/n)");
flag = u_input.next();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.