Code - package firstClass; import java.util.*; public class homework3 { public s
ID: 3679349 • Letter: C
Question
Code -
package firstClass;
import java.util.*;
public class homework3 {
public static void main(String[] args) {
inputGuess();
}
public static void inputGuess(){
Scanner input = new Scanner(System.in);
int x=1;
int min = 0;
min = min(min);
int max=0;
max = max(max);
if (min>max){
System.out.println("Your min is greater than your max, please try again.");
inputGuess();
}
Random r = new Random();
String playAgain = "Y";
int randNum = r.nextInt(max-min) + min;
System.out.println("Great, now pick a number from " + min +"-" + max + ".");
do{
try{
int number = input.nextInt();
// int [] testscores = new int[randNum];
if (number ==randNum){
System.out.println("Congratulations, You guessed the number!");
// System.out.println("Your previous guesses were " + testscores[randNum]++);
System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");
input.nextLine();
playAgain = input.nextLine();
if( playAgain.equalsIgnoreCase("Y") )
inputGuess();
else
printGoodbye();
break;
}
else if( number < randNum)
System.out.println("Go a little higher.");
else if( number > randNum)
System.out.println("Go a little lower.");
}
catch(Exception e){
printinvalid();
inputGuess();
x=2;
}
}while (x==1);
}
private static void printGoodbye() {
System.out.println("Game over, have a nice day.");
}
private static void printinvalid() {
System.out.println("Invalid entry. Try again.");
}
public static int min(int min) {
boolean numberError = true;
String enteredString = "";
do {
try {
System.out.print("Please enter an integer for the minimum of the range: ");
Scanner myScanner = new Scanner(System.in);
enteredString = myScanner.next();
min = Integer.parseInt(enteredString.trim());
numberError = false;
} catch(Exception e) {
System.out.println("Your entry: "" + enteredString + "" is invalid. Try again.");
numberError = false;
inputGuess();
}
} while (numberError == true );
System.out.println("Your range starts at " + min );
return min;
}
public static int max(int max) {
boolean numberError = true;
String enteredString = "";
do {
try {
System.out.print("Please enter an integer for your maximum of the range: ");
Scanner myScanner = new Scanner(System.in);
enteredString = myScanner .next();
max = Integer.parseInt(enteredString.trim());
numberError = false;
} catch(Exception e) {
System.out.println("Your entry: "" + enteredString + "" is invalid. Try again.");
numberError = false;
inputGuess();
}
} while (numberError == true );
System.out.println("Your range ends at: " + max );
return max;
}
}
Explanation / Answer
Note:-place this code in the above program import java.util. * ; public class randomNumber { public static void hint(int answer, int guess) { if(answer guess) { System.out.println("Incorrect! TOO LOW!."); } else if(answer == guess) { System.out.println("YOU GOT IT!"); } } public static void main(String[] args) { System.out.println("How many Players will be playing?"); int players = players(); int player = 0; player++; System.out.println("Player " + player + " Enter a number"); int guess = guess(); int answer = random(); int count = 0; while (guess != answer) { hint(answer, guess); count = count(count); guess = guess(); } hint(answer, guess); System.out.println("It took " + count(count) + " tries!"); } public static int random() { return(int)(Math.random() * 100) + 1; } public static int players() { Scanner my_input = new Scanner(System. in ); return my_input.nextInt(); } public static int guess() { Scanner my_input = new Scanner(System. in ); return my_input.nextInt(); } public static int count(int c){ c++; return c; } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.