This program is written in java and requires a template code that i have put at
ID: 672452 • Letter: T
Question
This program is written in java and requires a template code that i have put at the bottom. I am also using eclipse to write it.
Write a program in java that plays a guessing game with the user. Specifically, your program should randomly pick a number between 1 and 100. Then, ask the user for a guess. You should detect and tell the user if the guess is not a valid guess. Otherwise, tell the user their guess was too high or too low. The program should continue to prompt the user for new guesses until they get the correct number, telling them each time if the guess was too high or too low or invalid.
You have been supplied code to pick a random number between 1 and 100 each time you run your program. Here are a couple development/debugging strategies for this “target” variable:
Print out the random number, to make sure your program is acting correctly
Temporarily set the random “seed” to a value, which will have the effect of always choosing the same random number
Temporarily set the “target” variable to a fixed number, so you can test to see how your program responds in different testing situations.
Here’s a sample run of a working version of the program:
Enter your guess (between 1 and 100): 50
Too high!
Enter your guess (between 1 and 100): 0
Invalid guess, try again!
Enter your guess (between 1 and 100): 101
Invalid guess, try again!
Enter your guess (between 1 and 100): 25
Too high!
Enter your guess (between 1 and 100): 12
Too high!
Enter your guess (between 1 and 100): 6
Too high!
Enter your guess (between 1 and 100): 3
Too low!
Enter your guess (between 1 and 100): 4
Too low!
Enter your guess (between 1 and 100): 5
You win!
Please test for a set of lucky guesses (i.e. the user immediately guesses the right answer), lucky guesses starting with invalid guesses, and a full game, including both invalid and valid guesses.
I also have to start with this template code:
package lec5;
import java.util.Scanner;
import java.util.Random;
public class lec5 {
public static void main(String[] args) {
Random random;
Long seed;
if (args.length != 1) {
seed = null;
} else {
seed = Long.valueOf(args[0]);
}
if (seed == null) {
random = new Random();
} else {
random = new Random(seed);
}
int target = (Math.abs(random.nextInt()) % 100) + 1;
System.out.println(target);
//Write your code here
System.out.println("You win!");
}
}
Explanation / Answer
package mani;
import java.util.Scanner;
import java.util.Random;
public class lec5 {
public static void main(String[] args) {
Random random;
Long seed;
if (args.length != 1) {
seed = null;
} else {
seed = Long.valueOf(args[0]);
}
if (seed == null) {
random = new Random();
} else {
random = new Random(seed);
}
int target = (Math.abs(random.nextInt()) % 100) + 1;
System.out.println(target);
int guess=0;
Scanner s=new Scanner(System.in);
do{
System.out.println("Enter your guess (between 1 and 100):");
guess=s.nextInt();
if(guess>100||guess<1){
System.out.println("Invalid Number!!!");
}else{
if(target==guess){
System.out.println("You Win!!");
}else if(target>guess){
System.out.println("Too Low!!");
}else if(target<guess){
System.out.println("Too High!!");
}
}
}while(target!=guess);
System.out.println("You win!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.