I'm in a 100 level class for Java... very new and this is my first question so bear with me please :).
I have an assignment where two users are to enter their names, then get the number of games they played, then get the scores for each game that they played, put the scores in an array (or two arrays???), then compare the two arrays and scores to see who won each game or if they're tied, then display results. I don't have to sort it or search through it I don't believe.
Ss my teacher says getting the scores has to be a void method, but I'm not sure how I get the values from this void method into arrays for each player so I can compare the score values.
Here's my method for getting the scores into an array:
public static void inputScores(int[] array, String msg) {
String inputScores = "";
for (int i = 1; i < array.length; i++) {
inputScores = JOptionPane.showInputDialog(msg + " enter your score for game " + i);
array[i] = Integer.parseInt(inputScores);
}
}
Here's my main method:
public static void main(String[] args) {
//Get the number of games from the user, this is the array length
int[] numberOfGamesPlayed = new int[getPositiveIntOrQuit("How many games were played?")];
//Get the names of the players (Two)
String input, input2 = "";
input = JOptionPane.showInputDialog("Player 1 - enter your name");
inputScores(numberOfGamesPlayed, input);
input2 = JOptionPane.showInputDialog("Player 2 - enter your name");
inputScores(numberOfGamesPlayed, input2);
//Get two separate arrays, one for Player1, one for Player2
String output = "";
showResults(WINDOW_TITLE, output);
}
}
Yeah, it's a bit messy and such but I'm trying to figure this out on my own kinda. Here's my compareScores method, but I know it's crazyyyyy messed up. I've tried working through it but yeah, it's getting difficult.
public static String compareScores(int[] playerOne, int[] playerTwo, String msg, String msg2) {
String output = "";
int player1Wins = 0;
int player2Wins = 0;
int tie = 0;
for(int i = 0; i < playerOne.length; i++) {
for(int j = 0; j < playerTwo.length; j++); {
if(playerOne[i] > playerTwo[i]) {
output += "Game " + i + " results:" + "\n" + msg + " outscored " + msg2
+ " by " + (playerOne[i] - playerTwo[i]) + "points.";
player1Wins++;
}
else if(playerOne[i] < playerTwo[i]) {
player2Wins++;
output += "Game " + i + " results:" + "\n" + msg2 + " outscored " + msg
+ " by " + (playerTwo[i] - playerOne[i]) + "points.";
}
else {
output += msg + " and " + msg2 + " scored the same number of points.";
tie++;
}
}
output += "Summary: ";
}
return output;
}
Yeah I know it's a lot and I'm not good at explaining but if anyone can help me at all I don't mind trying to explain more of what's going on if it means getting help because I am really stuck like crazy right now.
Thanks for any and all help /)