Need help with my java project. the code is below 1. Program prompts users to pl
ID: 3801617 • Letter: N
Question
Need help with my java project. the code is below
1. Program prompts users to play again, and resets values
2. Ask the user for both a minimum and maximum possible value for the chosen random number.
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = userInput(scanner, "Enter number of users");
System.out.println(count);
User[] users = new User[count];
for (int indx = 0; indx < count; indx++) {
User user = new User(indx);
users[indx] = user;
}
int max = 10;
while (true) {
int allCorrect = 0;
for (int bindx = 0; bindx < count; bindx++) {
if (users[bindx].isCorrected()) {
allCorrect++;
}
}
if (allCorrect == count) {
for (int bindx = 0; bindx < count; bindx++) {
System.out.println(users[bindx].toString());
}
System.out
.println("Game finished ! Got all the correct number ");
break;
}
for (int indx = 0; indx < count; indx++) {
int randomNum = new Random().nextInt(10);
System.err
.println(" --------------------------------------------Random number "
+ randomNum);
if (!users[indx].isCorrected()) {
int userGuessValue = userInput(scanner, "Guess any number");
users[indx].getUserGuessValue().add(userGuessValue);
if (userGuessValue == randomNum) {
System.out.println("CORRECT !!");
users[indx].setCorrected(true);
} else {
System.out.println(" Oops !! Number is incorrect ");
int difference = userGuessValue - randomNum;
if (difference < 0) {
difference = -(difference);
}
if (difference >= max) {
System.err.println(" Too High !!");
} else {
System.err.println(" Too Low !!");
}
}
}
}
}
}
public static int userInput(Scanner scanner, String inputMessage) {
boolean isValidCount = false;
int number = -1;
do {
System.err.println(inputMessage);
while (!scanner.hasNextInt()) {
System.err.println("Input should be an integer value");
scanner.next();
}
try {
number = scanner.nextInt();
isValidCount = true;
break;
} catch (InputMismatchException e) {
System.err.print("input should be integer value");
} catch (NoSuchElementException e1) {
System.err.print("input should be integer value");
}
} while (!isValidCount);
return number;
}
}
class User implements Comparable<User> {
private int userId;
private List<Integer> usersGuesses;
private boolean correctGuess;
public User(int userId) {
this.userId = userId;
this.usersGuesses = new ArrayList<Integer>();
}
public int getUserId() {
return userId;
}
public List<Integer> getUserGuessValue() {
return usersGuesses;
}
public boolean isCorrected() {
return correctGuess;
}
public void setCorrected(boolean correctGuess) {
this.correctGuess = correctGuess;
}
@Override
public String toString() {
return "User [userId=" + userId + ", usersGuesses="
+ usersGuesses + ", correctGuess=" + correctGuess
+ "]";
}
@Override
public int compareTo(User o) {
int i = -9999;
if (o != null)
i = -1;
if (this.getUserGuessValue() == o.getUserGuessValue()) {
i = 0;
} else {
i = 1;
}
return i;
}
}
Explanation / Answer
package rockPaperScissors;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean quit = false;
while(!quit){
int count = userInput(scanner, "Enter number of users");
System.out.println(count);
User[] users = new User[count];
for (int indx = 0; indx < count; indx++) {
User user = new User(indx);
users[indx] = user;
}
int minRand = userInput(scanner, "Enter Minimum Randorm Number:");
int maxRand = userInput(scanner, "Enter Maximum Randorm Number:");
int range = maxRand - minRand;
int indx = 0;
while (true) {
int allCorrect = 0;
for (int bindx = 0; bindx < count; bindx++) {
if (users[bindx].isCorrected()) {
allCorrect++;
}
}
if (allCorrect == count) {
for (int bindx = 0; bindx < count; bindx++) {
System.out.println(users[bindx].toString());
}
System.out.println("Game finished ! Got all the correct number ");
break;
}
int randomNum = new Random().nextInt(range)+minRand;
while(!users[indx].isCorrected()) {
System.err.println(" --------------------------------------------Random number " + randomNum);
if (!users[indx].isCorrected()) {
int userGuessValue = userInput(scanner, "User "+(indx+1)+" Guess any number");
users[indx].getUserGuessValue().add(userGuessValue);
if (userGuessValue == randomNum) {
System.out.println("CORRECT !!");
users[indx].setCorrected(true);
indx++;
break;
} else {
System.out.println("Oops !! Number is incorrect ");
int difference = userGuessValue - randomNum;
if (difference < 0) {
difference = -(difference);
}
if (difference >= maxRand) {
System.out.println(" Too High !!");
} else {
System.out.println(" Too Low !!");
}
}
}
}
}
System.out.println("Do you want to play again(Y/N):");
String ch = scanner.next();
if(ch.charAt(0)=='Y' || ch.charAt(0)=='y'){
continue;
}
else{
quit = true;
}
}
}
public static int userInput(Scanner scanner, String inputMessage) {
boolean isValidCount = false;
int number = -1;
do {
System.out.println(inputMessage);
while (!scanner.hasNextInt()) {
System.out.println("Input should be an integer value");
scanner.next();
}
try {
number = scanner.nextInt();
isValidCount = true;
break;
} catch (InputMismatchException e) {
System.out.print("input should be integer value");
} catch (NoSuchElementException e1) {
System.out.print("input should be integer value");
}
} while (!isValidCount);
return number;
}
}
class User implements Comparable<User> {
private int userId;
private List<Integer> usersGuesses;
private boolean correctGuess;
public User(int userId) {
this.userId = userId;
this.usersGuesses = new ArrayList<Integer>();
}
public int getUserId() {
return userId;
}
public List<Integer> getUserGuessValue() {
return usersGuesses;
}
public boolean isCorrected() {
return correctGuess;
}
public void setCorrected(boolean correctGuess) {
this.correctGuess = correctGuess;
}
@Override
public String toString() {
return "User [userId=" + userId + ", usersGuesses="
+ usersGuesses + ", correctGuess=" + correctGuess
+ "]";
}
@Override
public int compareTo(User o) {
int i = -9999;
if (o != null)
i = -1;
if (this.getUserGuessValue() == o.getUserGuessValue()) {
i = 0;
} else {
i = 1;
}
return i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.