Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Count Help

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Count Help

    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, for example….

    After 6 loops array1 “Won” 4 times and array2 “Won” 2???

    Any help would be greatly appreciated.
    Regards,
    J.



    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;

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Count Help

    Use an if statement to detect who won. Inside the if statement increment the count when it is true.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Count

    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, for example….

    How does one code the number of random integers displayed after the loop?

    I.E. "player 1 won 0 Rounds".
    I.E. "player 2 won 6 Rounds."


    I.E. "player 1 won 2 Rounds".
    I.E. "player 2 won 4 Rounds."

    I.E. "player 1 won 1 Rounds".
    I.E. "player 2 won 5 Rounds."


    etc........

    Any help would be greatly appreciated.

    Regards,
    J.



    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;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Count Help

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    How does one code the number of random integers displayed after the loop?
    Where in the code do you want these features added? I don't see any comments anywhere in the code that say what the code needs to do at that location.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Java Count Help

    To track how many times each array "won" after 6 loops, you can use counters and compare the elements of each array after shuffling. Here's how you can modify your code to achieve that:

    ```java
    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();

    int array1Wins = 0;
    int array2Wins = 0;

    for (int loop = 0; loop < 6; loop++) {
    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;
    }

    int sumArray1 = 0;
    int sumArray2 = 0;

    for (int num : array1) {
    sumArray1 += num;
    }

    for (int num : array2) {
    sumArray2 += num;
    }

    if (sumArray1 > sumArray2) {
    array1Wins++;
    } else if (sumArray1 < sumArray2) {
    array2Wins++;
    }
    }

    System.out.println("After 6 loops, array1 won " + array1Wins + " times and array2 won " + array2Wins + " times.");
    }
    }
    ```

    In this code:

    1. I've added two counters, `array1Wins` and `array2Wins`, to keep track of how many times each array wins.
    2. I've wrapped your existing code in a loop that runs six times (as you mentioned "after 6 loops").
    3. Inside the loop, after shuffling both arrays, I sum up the elements of each array.
    4. Then, I compare the sums and increment the respective counter if array1 wins (`sumArray1 > sumArray2`) or array2 wins (`sumArray1 < sumArray2`).
    5. Finally, I print out the results after the loops are done.

    After 6 loops, it's evident how Java programming assignments can benefit from thorough testing and analysis. For those seeking further help with Java assignment, exploring additional resources like programminghomeworkhelp.com could provide valuable insights and guidance.

Similar Threads

  1. java program to count characters
    By asim001 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2019, 10:03 AM
  2. Replies: 10
    Last Post: July 1st, 2014, 02:41 PM
  3. Java count up timer
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2014, 07:42 AM
  4. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  5. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 04:40 PM