Jack Mchugh
Jack Mchugh

Reputation: 3

How do you compare two int arrays elements separately in Java and see which is the greater element?

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

Answers (3)

Thomas
Thomas

Reputation: 88707

It looks like you want to compare the elements at the same indices to each other so your code is almost good:

  • compare playerOne[i] to playerTwo[i] and do whatever is appropriate (e.g. count the score)
  • keep in mind that the maximum index is defined by the shorter array so either make sure they both have the same length or use 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:

  • using a list doesn't require to know the number of rounds at compile time (e.g. when you'd want to go on until one player has won at least 2 rounds more than the other with a minimum of x)
  • you have only one list so you don't have to make sure 2 arrays or lists have the same size and order
  • you could even add methods to RoundResult like boolean isDraw() { ... }

Upvotes: 3

Vineeth Maller
Vineeth Maller

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

azro
azro

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

Related Questions