I need to build on this previous code. I need to store the numbers guessed from
ID: 3679743 • Letter: I
Question
I need to build on this previous code. I need to store the numbers guessed from the user, and print them out later when the game is finished. I also need to add multiple users to the game, right now only one player can play it. Its the random number game with multiple players. It goes like this: player 1 picks number, java prompts if too low/ too high/ or incorrect. Then player 2 picks number, java prompts if too low/ too high/ or incorrect. It goes subsequently, (each player chooses one number each per round) until each player guesses the right number. I will attach prompt and previous code and directions.MUST USE ARRAYS. Thank you!!
Code -
package firstClass;
import java.util.*;
public class tryArrays {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("how many players are there?");
int numberArray = input.nextInt();
int [] numOfPlayers = new int[numberArray];
for (int i = 0; i<=numberArray; i++){
System.out.println("Enter number for person " + (i+1));
numOfPlayers [i] = input.nextInt();
int n = figureHighLow(numOfPlayers[i]);
if (numOfPlayers[i] == numberArray)
{
break;
}
}
System.out.println("Your previous scores were" + Arrays.toString(numOfPlayers));
} static int figureHighLow (int num){
int randNum = 3; //need to add random number generator here
if (num == randNum){
System.out.println("You got it. well done.");
}else if ( num < randNum){
System.out.println("Little higher");
}else if (num > randNum){
System.out.println("little lower");
}
return num;
}
}
Explanation / Answer
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.