vendredi 13 août 2021

Arrays with if Statments and loops

I have created a basic array and loop as shown below but I am trying to work out how I would go about coding the end results in an If statement so that it shows up as follows....

I.e . Player 1 won 5 rounds Player 2 won 1 rounds "player 1 wins as he won 5 rounds and player 2 won 1"

Any help would be greatly appreciated. Regards,

import java.sql.SQLOutput; import java.util.Random;

public class Main {

public static void main(String[] args) {

    int[] array1 = {10, 6, 8, 9, 7, 12, 7};
    int[] array2 = {7, 6, 9, 5, 2, 8, 11,};

    Random rand = new Random();

    for (int i = 1; i < array1.length; i++) {
        int randomIndexToSwap = rand.nextInt(array1.length);
        int temp = array1[randomIndexToSwap];
        array1[randomIndexToSwap] = array1[i];
        array1[i] = temp;
    }

    for (int j = 0; j < array2.length; j++) {
        int randomIndexToSwap = rand.nextInt(array2.length);
        int temp = array2[randomIndexToSwap];
        array2[randomIndexToSwap] = array2[j];
        array2[j] = temp;

    }
    for (int k = 1; k < 7; k++) {
        System.out.println();
        System.out.println();
        System.out.println("ROUND " + k);
        System.out.println("Player 1 Drew " + ":" + array1[k]);
        System.out.println("Player 2 Drew " + ":" + array2[k]);

        {

        }


        {
            int cardValue_player1 = array1[k];
            int carValue_player2 = array2[k];


            if (cardValue_player1 > carValue_player2) {
                System.out.println();
                System.out.println("Player 1 Wins this Round!");

            }
            if (cardValue_player1 < carValue_player2) {
                System.out.println();
                System.out.println("Player 2 Wins this Round!");
                System.out.println();
            }
            if (cardValue_player1 == carValue_player2) {
                System.out.println();
                System.out.println("This is a Tie!");
                System.out.println();

            }
            

            }
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("                                                         Thank You For Playing !!!!");
    }
}

Aucun commentaire:

Enregistrer un commentaire