Java Guessing Game Project (Beginner) - java

I'm currently working on a java project, and im supposed to make a program that generates a number between 1000 and 2000, then gives you the chance to enter the amount of players, takes in all the names and guesses in two different arrays, then create two methods, one to find the closest guess to the actual numbers and another to report or find the winner match it up with their guess and print it out. Im having trouble with my second method, im drawing a blank on the logic part right now this is my code and output:
public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
ArrayList tempList = new ArrayList();
ArrayList tempList1 = new ArrayList();
int counter = 0;
int temp = 0;
for(int index = 0; index < array.length; index++)
{
temp = answer - array[index];
temp = Math.abs(temp);
if(temp == winner)
{
counter++;
tempList.add(array1[index]);
tempList1.add(array[index]);
}
}
for(int index = 0; index < tempList.size(); index++)
{
if(tempList.size() == 1);
{
System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is "
+ winner + " away from the actual number of jelly beans.");
if(tempList.size() > 1)
{
System.out.println("The winners are: ");
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is "
+ winner + " away from the actual number of jelly beans.");
}
}
if(tempList.size() == 1 && winner == 0)
{
System.out.println("The winner is " + tempList.get(0) + ", with a guess of " + tempList1.get(0) + " which is "
+ "the exact number of jelly beans in the jar.");
if(tempList.size() > 1)
{
System.out.println("The winners are: ");
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is "
+ "the exact number of jelly beans in the jar.");
}
}
}
}
This is the output it is producing when there is more than one winner.
There were 1532 jelly beans in the jar.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are:
Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are:
greg, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winner is Stan, with a guess of 1200 which is 332 away from the actual number of jelly beans.
The winners are:
Jim, with a guess of 1200 which is 332 away from the actual number of jelly beans.
This is what it should look like
There were 1500 jelly beans in the jar.
The winners are:
Mike with a guess of 1475, which is 25 away from the actual number of jelly beans.
Tony with a guess of 1525, which is 25 away from the actual number of jelly beans.
Im having trouble with the method portion i know this much but if you need to see the rest of the code let me know any help would be appreciated, thanks.
(I think i need a way to void the first print out if the counter passes one but im not sure how to go about this)

Try this. I have made a lot of changes in your code because I did not understand it. Following changes:
No raw types
Only one loop to find the winner
Only one array with the player and their numbers
A different loop for the output
Here is the code
import java.util.ArrayList;
import java.util.List;
public class GuesNumber {
public static void main(String[] args) {
// The number we want to guess
final int theNumber = 1500;
// A random set of players and their numbers
List<Player> players = new ArrayList<>();
for (int i = 0; i < 100; i++) {
players.add(new Player("Player " + i, (int) (Math.round((Math.random() * 1000)) + 1000)));
}
// Call of your function (with less arguments)
reportWinner(theNumber, players);
}
public static void reportWinner(int theNumber, List<Player> players) {
// Here we store the minimal diff between THE number
// and the number of the potential winner
Integer minDiff = null;
// Here we store the winners
ArrayList<Player> winners = new ArrayList<>();
// Find the winners
for (Player player : players) {
int diff = Math.abs(player.number - theNumber);
// We have a potential winner the player is the first
// we check or his number is less or equal then
// the minimal diff
if (minDiff == null || minDiff <= diff) {
// We have to clear all potential winners,
// if the number of the player is smaller then
// the minimal diff
if (minDiff != null && minDiff > diff) {
winners.clear();
}
// Add a potential winner
winners.add(player);
minDiff = diff;
};
}
// Let's do the output
System.out.println("There were " + theNumber + " jelly beans in the jar\n");
System.out.println("The winner " + (winners.size() == 1 ? "is" : "are") + ":\n");
for (Player player : winners) {
System.out.print(player.name + " with a gues of " + player.number + ", wich is " + minDiff + " away from the actual number of jelly beans. ");
}
System.out.println("");
}
public static class Player {
String name;
int number;
public Player(String name, int number) {
this.name = name;
this.number = number;
}
}
}

This is what you need. Note: it is recommended to use meaningful variable names rather than array or tempList to make the code more readable.
public static void reportWinner (int answer, int winner, int [] array, String [] array1)
{
ArrayList tempList = new ArrayList();
ArrayList tempList1 = new ArrayList();
int temp = 0;
for(int index = 0; index < array.length; index++)
{
temp = answer - array[index];
temp = Math.abs(temp);
if(temp == winner)
{
tempList.add(array1[index]);
tempList1.add(array[index]);
}
}
if(tempList.size() > 1) {
System.out.println("The winners are: ");
} else if(tempList.size() == 1) {
System.out.println("The winner is: ");
}
for(int index = 0; index < tempList.size(); index++)
{
String suffix = "";
if(winner == 0) {
suffix = "the exact number of jelly beans in the jar."
} else {
suffix = winner + " away from the actual number of jelly beans."
}
System.out.println(tempList.get(index) + ", with a guess of " + tempList1.get(index) + " which is " + suffix);
}
}

Related

How do I stop this array from only printing/remembering the latest thing put into the array?

I don't know if the array itself is broken or if the code to print the array is broken
/*This is the main class that prints out everything */.
//Start the problems loop
for (int i = 0; i < session.getNumProb(); i++) {
//Set random values for factors
cards.setFactors();
//Create problems
cards.setProb(session);
//Determine correct/incorrect answer, update running score
cards.setResponse(session);
//Add the problem to the history array
session.setHistory(cards);
}
//Set score percentage
session.setScorePct();
//Print out summary
session.prtSummary();
//Print out history array and outro
session.prtHistoryAndOutro();
______________________________________________________________________________
/* This sets the history based on questions asked */
//setHistory
public void setHistory(Cards c) {
for (int i = 0; i < numProb; i++) {
history[i] = c.getA() + oper + c.getB() + " = " + c.getResponse() + ", " + c.getCorInc() + ", correct answer is " + c.getC();
System.out.println();
}
}
____________________________________________________________________________
/* This prints out the history array */
//prtHistoryAndOutro
public void prtHistoryAndOutro() {
System.out.println("Problems");
for (int i = 0; i < numProb; i++) {
System.out.println(history[i]);
}
System.out.println();
System.out.println();
System.out.println("Thank you for using the 3312 FlashCard System, " + name + ".");
System.out.println("Come back and play again real soon!");
}
I don't know where something is going wrong but it should be within these three pieces of code. Also the bottom two pieces are within the same class

JAVA array pair for student grades [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
how do I pair the student array with the grade array? When I find the highest grade the corresponding student should also show, and same with the lowest graded student. I cant figure out how to make this program perform as such with two separate arrays.
import java.util.Scanner;
public class Asm7 {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
System.out.println("How many students do you have?: ");
int AMOUNT = 0;
AMOUNT = Scan.nextInt();
String[] STUDENT = new String [AMOUNT];
int COUNTER = 0;
int GRADE [] = new int [AMOUNT];
if (AMOUNT <= 0) {
System.out.println("Invalid student amount");
}
else {
for(int i = 0; i < AMOUNT; i++){
System.out.println("Enter student's first name: " + (i+1));
STUDENT[i] = Scan.next();
System.out.println("Enter student's grade in order added: ");
GRADE[i] = Scan.nextInt();
}
for(int i = 0; i < AMOUNT; i++){
System.out.println(STUDENT[i] + " received the final grade of " + GRADE[i]);}
System.out.println();
int [] Results = MinMax(GRADE);
System.out.println("The highest grade in the class was " + Results[1]);
System.out.println("The lowest grade in the class was "+ Results[0]);
}}
public static int[] MinMax(int[] value) {
int[] Result = new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int i : value) {
Result[0] = i < Result[0] ? i : Result[0];
Result[1] = i > Result[1] ? i : Result[1];
}
return Result;
}
}
Your while loop validation for number of students is a little late. You want to do this before you declare and initialize your arrays. However, the fact that the while loop was actually used in an attempt towards some form of validation is a really good sign. It's more than most new programmers tend to do. All input should be validated and provide a User the opportunity to supply a correct solution. This can only lead to a smoother, trouble free application and a much better experience for the User. Take a look at this while loop which is in your code:
while (amount < 0) {
System.out.println("Invalid student amount");
}
What is going to happen if the User supplies -1 (this is a valid integer value as is +1)? That's right...your application will end up in an infinite loop spitting out Invalid student amount to the Console Window. Your validation scheme should encompass the entire prompt and then the means to exit it should be more logically defined. With a while loop the best exit is done through its conditional statement, if the condition is false then exit the loop, for example:
Scanner scan = new Scanner(System.in);
// Number Of Students...
String inputString = "";
while (inputString.isEmpty()) {
System.out.print("How many students do you have?: --> ");
inputString = scan.nextLine().trim();
/* Is the supplied Number Of Students valid and within
range (1 to 50 inclusive)? */
if (!inputString.matches("\\d+") || Integer.valueOf(inputString) < 1
|| Integer.valueOf(inputString) > 50) {
// No...
System.err.println("Invalid entry (" + inputString + ") for Student "
+ "amount! Try again...");
inputString = ""; // Empty inputString so we loop again.
System.out.println();
}
}
// Valid amount provided.
int amount = Integer.valueOf(inputString);
String[] student = new String[amount];
int grade[] = new int[amount];
Right away you will notice some obvious changes here. The entire How many students do you have? prompt is contained within a while loop block. If the User does not supply a valid response then that User is asked to try again. The student and grade parallel arrays are declared and initialized only after a valid response for the number of students is provided.
You will also notice that the while loop condition doesn't rely on a integer value but instead it relies on actual string content (regardless of what it is) instead. If the variable is empty ("") then loop again. This is because the Scanner#nextLine() method is used to collect the Users input instead of the Scanner#nextInt() method. The prompt still expects an integer value to be supplied, just a string representation of an integer value and this is validated using the String#matches() method along with a small Regular Expression (regex).
I personally prefer to use the Scanner#nextLine() method for a number of reasons. I personally find it more flexible especially if you want to accept both Alpha and Numerical input from a single prompt. If the prompt above stated:
How many students do you have? (q to quit)
you would just need to add another if statement above the numerical validation code to see if 'q' or 'Q' was supplied, for example:
// If either q or Q is entered then quit application.
if (amountString.matches("[qQ]")) {
System.out.println("Bye-Bye");
System.exit(0);
}
Also, with a good expression passed to the matches() method, there is no need to trap exceptions in order to carry out validations, not that there is anything wrong with this, many people do it, I especially don't however when I have no need to do so.
Side Note: I'm going to state the obvious here and I'm sure you've heard it a hundred times before and you're sick of hearing it but I'm going to tell you again:
Your class methods should start with a lowercase letter (see Java Naming
Conventions).
I know you don't hear the compiler complaining but it does make it a
little more difficult (at times) to read the code. Everyone that reads
your code will appreciate you for it.
Because the student and grade arrays are parallel arrays you would want the minGrade() and maxGrade() methods to return a specific array index value to either the lowest or highest grade so that a referential relationship can be made toward the student that contains that specific grade determined. So, this would be far more useful:
public static int minGrade(int[] arr, int size) {
// Initialize min to have the highest possible value.
int min = Integer.MAX_VALUE;
int returnableIndex = -1;
// loop to find lowest grade in array
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
public static int maxGrade(int[] arr, int size) {
int max = Integer.MIN_VALUE;
int returnableIndex = -1;
// loop to find highest grade in array
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
With everything in play your code might look like this:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Number Of Students...
String amountString = "";
while (amountString.isEmpty()) {
System.out.print("How many students do you have?: --> ");
amountString = scan.nextLine().trim();
// Is the supplied Number Of Students valid and within
// range (1 to 50 inclusive)?
if (!amountString.matches("\\d+") || Integer.valueOf(amountString) < 1
|| Integer.valueOf(amountString) > 50) {
// No...
System.err.println("Invalid entry (" + amountString + ") for Student "
+ "amount! Try again...");
amountString = ""; // Empty inputString so we loop again.
System.out.println();
}
}
// Valid amount provided.
int amount = Integer.valueOf(amountString);
// Declare and initialize parallel arrays
String[] student = new String[amount];
int grade[] = new int[amount];
// Student Names and Grade...
for (int i = 0; i < amount; i++) {
// Student Name...
String name = "";
while (name.isEmpty()) {
System.out.print("Enter student #" + (i + 1) + " name: --> ");
name = scan.nextLine().trim();
/* Is the name valid (contains upper or lower case letters from
A-Z and a single whitespaces separating first and last name?
Whitespace and last name is optional. */
if (!name.matches("(?i)([a-z]+)(\\s{1})?([a-z]+)?")) {
// No..
System.err.println("Invalid Student #" + (i + 1) + " name ("
+ name + ")! Try Again...");
System.out.println();
name = ""; // Empty name so we loop again.
}
}
// Valid Student name provided...
student[i] = name;
// Student Grade...
String gradeString = "";
while (gradeString.isEmpty()) {
System.out.print("Enter student #" + (i + 1) + " grade: --> ");
gradeString = scan.nextLine().trim();
// Is the supplied grade valid and within range (0 to 100 inclusive)?
if (!gradeString.matches("\\d+")
|| Integer.valueOf(gradeString) < 0
|| Integer.valueOf(gradeString) > 100) {
// No...
System.err.println("Invalid entry (" + gradeString + ") for "
+ "Student #" + (i + 1) + " grade! Try again...");
gradeString = "";
System.out.println();
}
}
// Valid Student grade provided...
grade[i] = Integer.valueOf(gradeString);
}
// Display everyone's grade
System.out.println();
for (int i = 0; i < amount; i++) {
System.out.println(student[i] + " received the final grade of " + grade[i]);
}
System.out.println();
//Display who is highest and lowest...
int index = maxGrade(grade, amount);
System.out.println("The highest grade in the class was by '" + student[index]
+ "' with a grade of: " + grade[index]);
index = minGrade(grade, amount);
System.out.println("The lowest grade in the class was by '" + student[index]
+ "' with a grade of: " + grade[index]);
}
public static int minGrade(int[] arr, int size) {
// Initialize min to have the highest possible value.
int min = Integer.MAX_VALUE;
int returnableIndex = -1;
// loop to find lowest grade in array
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
public static int maxGrade(int[] arr, int size) {
int max = Integer.MIN_VALUE;
int returnableIndex = -1;
// loop to find highest grade in array
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
returnableIndex = i;
}
}
return returnableIndex;
}
If the data are not sorted, it would be better to find both min and max grades in the same loop, after printing the students and their grades.
Then no loop is needed to print min and max grades:
for (int i = 0; i < amount; i++) {
System.out.println(student[i] + " received the final grade of " + grade[i]);
}
int min = grade[0];
int max = grade[0];
for (int i = 1; i < amount; i++) {
if (grade[i] < min) {
min = grade[i];
} else if (grade[i] > max) {
max = grade[i];
}
}
System.out.println("The highest grade in the class was " + max);
System.out.println("The lowest grade in the class was " + min);
If the index of min/max is sought, it would be possible to print the name of the students who get the min and max grades.
public static void main(String[] args) {
int[] grades = new int[]{50, 51, 52, 50, 60, 22, 53, 70, 60, 94, 56, 41};
int[] result = getMinMax(grades);
System.out.println("Min: " + result[0] + ", Max: " + result[1]);
}
public static int[] getMinMax(int[] values) {
int[] result = new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int i : values) {
result[0] = i < result[0] ? i : result[0];
result[1] = i > result[1] ? i : result[1];
}
return result;
}
You'll need to handle the case of int[] values being null or empty. You can decide that (Throw an exception, return null... or whatever)

Stack Overflow on Random Integers

Where the commented section is, it says that there is a StackOverflowError - null. I am trying to get it to make random numbers to match up with an inputted value. The goal of this code is to do the following:
Accept a top number (ie 1000 in order to have a scale of (1-1000)).
Accept an input as the number for the computer to guess.
Computer randomly guesses the first number and checks to see if it is correct.
If it is not correct, it should go through a loop and randomly guess numbers, adding them to an ArrayList, until it guesses the input. It should check to see if the guess is already in the array and will generate another random number until it makes one that isn't in the list.
In the end, it will print out the amount of iterations with the count variable.
Code:
import java.util.*;
public class ArrNumGuess
{
public static Integer top, input, guess, count;
public static ArrayList <Integer> nums;
public static void main ()
{
System.out.println("Please enter the top number");
top = (new Scanner(System.in)).nextInt();
System.out.println("Please enter the number to guess (1 - " + top + ")");
input = Integer.parseInt(((new Scanner(System.in)).nextLine()).trim());
nums = new ArrayList<Integer>(); //use nums.contains(guess);
guess = (new Random()).nextInt(top) + 1;
nums.add(guess);
System.out.println("My first guess is " + guess);
count = 1;
if(guess != input)
{
guesser();
}
System.out.println("It took me " + count + " tries to find " + guess + " and " + input);
}
public static void guesser()
{
boolean check = false;
while(!check)
{
guess = (new Random()).nextInt(top) + 1; //Stack Overflow - null
if(nums.contains(guess) && !(guess.equals(input)))
{
count--;
guesser();
}
else if(guess.equals(input))
{
check = true;
System.out.println("My guess was " + guess);
// nums.add(guess);
count++;
}
else
{
System.out.println("My guess was " + guess);
nums.add(guess);
count++;
}
}
}
}
In guesser() method, you're invoking itself:
if(nums.contains(guess) && !(guess.equals(input)))
{
count--;
guesser();
}
There is quite a possibility it will never end. But all that is in while loop, so why not get rid of recurrence and do this in an iterative style?
OK - a different approach to your guesser for fun. Enumerate a randomized sequence of numbers in specified range (1 to 'top') and find the guess in the list whose index is effectively the number of "attempts" and return.
(BTW - #Andronicus answer is the correct one.)
/** Pass in 'guess' to find and 'top' limit of numbers and return number of guesses. */
public static int guesser(int guess, int top) {
List<Integer> myNums;
Collections.shuffle((myNums = IntStream.rangeClosed(1, top).boxed().collect(Collectors.toList())), new Random(System.currentTimeMillis()));
return myNums.indexOf(guess);
}
You are making it more complicated than it needs to be and introducing recursion unnecessarily. The recursion is the source of your stack overflow as it gets too deep before it "guesses" correctly.
There is a lot of sloppiness in there as well. Here's a cleaned up version:
import java.util.*;
public class Guess {
public static void main(String args[]) {
System.out.println("Please enter the top number");
Scanner scanner = new Scanner(System.in);
int top = scanner.nextInt();
System.out.println("Please enter the number to guess (1 - " + top + ")");
int input = scanner.nextInt();
if (input < 1 || input > top) {
System.out.println("That's not in range. Aborting.");
return;
}
ArrayList <Integer> nums = new ArrayList<>();
Random rng = new Random(System.currentTimeMillis());
while(true) {
int guess = rng.nextInt(top) + 1;
if (!nums.contains(guess)) {
nums.add(guess);
if (nums.size() == 1) {
System.out.println("My first guess is " + guess);
} else {
System.out.println("My guess was " + guess);
}
if (guess == input) {
System.out.println("It took me " + nums.size() + " tries to find " + guess);
break;
}
}
}
}
}

How can I get my for each loop to print once for each number in the array

assignment:
Write a program that reads in integers between 1 and 100 from the user and counts the
occurrences of each number. The user input ends when they enter a 0.
You must use an enhanced for-loop to solve this problem.
If a number occurs more than 1 time use the plural word “times” instead of “time”. Do not display numbers that were not entered.
I know and understand why my code's current output below appears with duplicates. The print logic is inside the for-each loop code block. If I close the code block I am no longer able to use the variables I initialized inside the loop. I have tried everything I can think of. Any suggestions would be appreciated
current output:
- 1 occurs 1 time,
- 1 occurs 2 times
- 2 occurs 1 time
- 2 occurs 2 times
- 3 occurs 1 time
- 3 occurs 2 times
needed output:
- 1 occurs 2 times
- 2 occurs 2 times
- 3 occurs 2 times
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] numbers = new int[10];
System.out.print("Enter Integers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = in.nextInt();
if (numbers[i] == 0) {
break;
}
}
enhancedLoop(numbers);
}
private static void enhancedLoop(int[] numbers) {
int[] counts = new int[101];
for (int value : numbers) {
counts[value]++;
if (value > 0)
if (counts[value]> 1)
System.out.println(value + " occurs " + counts[value]+ " times");
else
System.out.println(value + " occurs " + counts[value] + " time");
}
}
Variables are only available in the block in which they are declared. Move the output after the for loop and iterate over counts to display the values:
for (int i = 0, c = counts.length; i < c; ++i) {
if (counts[i] > 0) {
if (counts[i] > 1) {
System.out.println(value + " occurs " + counts[i]+ " times");
} else {
System.out.println(value + " occurs " + counts[i]+ " time");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] numbers = new int[10];
System.out.print("Enter Integers:");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = in.nextInt();
if (numbers[i] == 0) {
break;
}
}
enhancedLoop(numbers);
}
private static void enhancedLoop(int[] numbers) {
int[] counts = new int[101];
for (int value : numbers) {
counts[value]++;
if (value > 0)
if (counts[value]> 1)
System.out.println(value + " occurs " + counts[value]+ " times");
else
System.out.println(value + " occurs " + counts[value] + " time");
}
}
You can do this effectively using the below steps:
(1) Identify the unique numbers first
(2) Find the number of times each unique number occurs
So, you need to change your enhancedLoop(int[] numbers) method as shown below to achieve the result:
private static void enhancedLoop(int[] numbers) {
//convert the array to a list to make computations easier by using streams
List<Integer> numbersList = Arrays.stream(numbers).boxed().collect(Collectors.toList());
//Get the unique numbers in the list
List<Integer> uniqueNumbers = numbersList.stream().distinct().collect(Collectors.toList());
//Now find out number of times each each unique number occured
for(int number : uniqueNumbers) {
long times = numbersList.stream().filter(num -> num == number).count();
System.out.println(number+" occurs "+times);
}
}
lets try to do this without lamdas, using simply a hashmap instead of arrays
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
int tmp=0;
System.out.print("Enter Integers:");
//read a maximum of 10 int
for (int i = 0; i < 10; i++) {
tmp = in.nextInt();
//if we read a 0 we quit
if (tmp == 0) {
break;
}
//if we already saw the number we up the counter
if(numbers.containsKey(tmp)){
numbers.put(tmp, numbers.get(tmp)+1);
}else{
//otherwise we just add the new int
numbers.put(tmp, 1);
}
}
//call the print loop
enhancedLoop(numbers);
}
private static void enhancedLoop(HashMap<Integer, Integer> numbers) {
//you print what you counted
for(Map.Entry<Integer, Integer> entry : numbers.entrySet()) {
System.out.print(entry.getKey() + " occurs " + entry.getValue() + " time");
if (entry.getValue()>1)
System.out.print("s");
System.out.println("");
}
}

How to display all scores at the end without a loop?

I am trying to display my score at the end of a questionnaire however my way of doing it only displays the last score.
It would work if I were to put it in a loop, the JOptionPane.showMessageDialog however I only want it to display this once, at the end.
Here is my code.
//Main method
JOptionPane.showMessageDialog(null, printPlayerScore(playerName, playerAge, playerScore, playerCount));
//printPlayerScore method
public static String printPlayerScore(String playerName[], int playerAge[], int playerScore[], int playerCount) {
String displayResult = "";
for(int i = 0; i < playerCount; i++) {
displayResult += "\nName: " + playerName[i] + "\nAge: " + playerAge[i] + "\nScore: " + playerScore[i] + "\n";
}
return displayResult;
}
Example run:
Player1: 12
Player2: 12
When it should be
Player1: 10
Player2: 12
I know I need to change the method to something else, but how else could I do it?
Full code: http://pastebin.com/NME8Dh7N
This is a screaming example of the benefits of Object-Oriented Programming. OOP will make this chunk of code easier to debug, read, and write. I'll write some quick code to explain this better (by no means is it perfect). Notice how we can easily create a nice output string using the properties of a Player. Create an array of Player objects, and pass it into your print method.
public class Player
{
public String name;
public int age;
public int score;
public String toString()
{
return String.format("\nName : %s\nAge: %d\nScore: %s\n", name, age, score);
}
}
public static String printPlayerScore(Player[] players)
{
String displayResult = "";
for(Player player : players)
{
displayResult += player.toString();
}
return displayResult;
}
I personally would not code this problem the way you did as it's not OOP and just very confusing. However, here is something you can use so as to not totally break your code but fix your problem.
The main question you are trying to answer is why is the last score showing up. As others have stated, you are using one array for player scores. Here is hack to change your code to get it working:
List<int[]> playerScoresList = new ArrayList<int[]>();
for (int i = 0; i < playerCount; i++)
{
int playerScore[] = new int[1];
JOptionPane.showMessageDialog(null, "It is " + playerName[i] + "'s turn now!");
checkQuestion(question, questionAnswer, userAnswer);
System.out.println("Name: " + playerName[i] + " || Age: " + playerAge[i] + "\n\n ~~~~ Results ~~~~");
System.out.println(printQuestionnaireResults(question, userAnswer) + " ~~~~ End of Results ~~~~\n");
playerScore = calculatePlayerScore(userAnswer, playerScore, playerCount);
// double playerScorePercentage = ((double)playerScore[i] / (double)question.length) * 100;
double playerScorePercentage = ((double)playerScore[0] / (double)question.length) * 100;
System.out.println(playerName[i] + " got " + playerScore[0] + " questions correct out of " + question.length + "! (" +
playerScorePercentage + "%)\n");
playerScoresList.add(playerScore);
}
JOptionPane.showMessageDialog(null, printPlayerScore(playerName, playerAge, playerScoresList, playerCount));
Other methods that need to be changed:
public static int[] calculatePlayerScore(boolean userAnswer[], int playerScore[], int playerCount) {
for (int i = 0; i < 1; i++) {
playerScore[i] = 0;
for (int ii = 0; ii < userAnswer.length; ii++) {
if (userAnswer[ii]) {
playerScore[i] += 1;
}
}
}
return playerScore;
}
And:
public static String printPlayerScore(String playerName[], int playerAge[], List<int[]> playerScore, int playerCount) {
String displayResult = ""; // Maybe use StringBuilder
for(int i = 0; i < playerCount; i++)
{
int[] score = playerScore.get(i);
displayResult += "\nName: " + playerName[i] + "\nAge: " + playerAge[i] + "\nScore: " + score[0] + "\n";
}
return displayResult;
}
Now, you will create a new array with one element each time for each user.
Please keep in mind this hack is provided such that there should be minimal change in your current code. You should seriously consider re-writing the entire program IMO. I've also commented out some other code as well just to get it working.
According to your pastebin, you only have 1 array for storing question answers. Meaning the n+1 player is overwriting n player's answers.
You could either do a matrix (Array of Arrays), or the easier to read alternative, make a class for players and have that class manage player data, with the program having only a list of players. It will reduce the number of parameters in the function calls and allow for easy individualization of answers.
public class Player {
private String name;
private List<boolean> answers;
private int playerId;
}
The matrix would work like this:
boolean answers[][] = new boolean[playerCount][questionCount];
That way, each player has a separate list of answers that independ from each other.
Then, all you need would be to get send each player as a parameter to the functions and read those as necessary.

Categories

Resources