Reputation: 3
So I have to make a game that compares two int arrays elements separately and see if one is greater than the other or if they equal and print a message declaring which player won or if it was a draw. Both players are given 7 cards so they have to be compared 7 times each in a different round. I'm stuck when it comes to comparing each number I cant get seem to get it to work and I don't really know how to make it so every iteration declares a new round would that have to be done separately 7 times? This is what I currently have after many attempts. Any help would be greatly appreciated!
public class Exercise1 {
public static void main(String[] args) {
int[] playerOne = new int[6];
int[] playerTwo = new int [6];
playerOne[0] = 10;
playerOne[1] = 6;
playerOne[2] = 8;
playerOne[3] = 9;
playerOne[4] = 7;
playerOne[5] = 12;
playerOne[6] = 7;
playerTwo[0] = 7;
playerTwo[1] = 6;
playerTwo[2] = 9;
playerTwo[3] = 5;
playerTwo[4] = 2;
playerTwo[5] = 8;
playerTwo[6] = 11;
for (int i = 0; i <= playerOne.length - 1; i++) {
if (playerOne[i] < playerTwo) {
}
Upvotes: 0
Views: 991
Reputation: 88707
It looks like you want to compare the elements at the same indices to each other so your code is almost good:
playerOne[i]
to playerTwo[i]
and do whatever is appropriate (e.g. count the score)i < Math.min(playerOne.length, playerTwo.length)
.However, it might be better to rethink your approach (which might be a later exercise though): instead of maintaining 2 separate arrays of "round" scores try to put it into one by introducing a RoundResults
class (or similar) and ideally using a list (which has dynamic length).
Example (simplified):
class RoundResult {
private int playerOneScore;
private int playerTwoScore;
//constructor, getters and setters
}
List<RoundResult> rounds = new ArrayList<>();
rounds.add(new RoundResult(10, 7));
rounds.add(new RoundResult(6, 6));
...
for(RoundResult round : rounds) {
if( round.getPlayerOneScore() < round.getPlayerTwoScore() ) {
//player 2 won that round
} else if( round.getPlayerOneScore() > round.getPlayerTwoScore() ) {
//player 1 won that round
} else {
//it was a draw
}
}
Doing it like this has several advantages:
RoundResult
like boolean isDraw() { ... }
Upvotes: 3
Reputation: 65
int[] playerOne = {10, 6, 8, 9, 7, 12, 7};
int[] playerTwo = {7, 6, 9, 5, 2, 8, 12};
int position = 0;
for(int i = 0; i< playerOne.length; i++)
position += Integer.compare(playerOne[i], playerTwo[i]);
String result = (position < 0)? "Player One": (position > 0)? "Player Two" : "Equal";
System.out.println(result);
The For loop iterates through one element per array per iteration, and it increments or decrements the 'position' variable by comparing the elements in an iteration.
If Player One is greater then the final value of 'position' would be negative as there were more decrements and vice versa.
Upvotes: 0
Reputation: 54148
You need 2 variables that holds each ones score, increment the one that has the bigger card for each round, at the end find the biggest score
int[] playerOne = {10, 6, 8, 9, 7, 12, 7};
int[] playerTwo = {7, 6, 9, 5, 2, 8, 12};
int scorePlayerOne = 0, scorePlayerTwo = 0;
for (int i = 0; i <= playerOne.length - 1; i++) {
if (playerOne[i] < playerTwo[i]) {
scorePlayerTwo++;
} else if (playerTwo[i] < playerOne[i]) {
scorePlayerOne++;
}
}
if (scorePlayerOne < scorePlayerTwo) {
System.out.println("Player Two wins");
} else if (scorePlayerTwo < scorePlayerOne) {
System.out.println("Player One wins");
} else {
System.out.println("Draw");
}
Upvotes: 2