Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 {

private int userId;

private List usersGuesses;

private boolean correctGuess;

public User(int userId) {

this.userId = userId;

this.usersGuesses = new ArrayList();

}

public int getUserId() {

return userId;

}

public List 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;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genomes And Databases On The Internet A Practical Guide To Functions And Applications

Authors: Paul Rangel

1st Edition

189848631X, 978-1898486312

More Books

Students also viewed these Databases questions