Home > OS >  sum of values of an array filled with integers randomly generated
sum of values of an array filled with integers randomly generated

Time:05-27

Write a method sum(int[] values) that returns the sum of elements of the array values.

when I display my sum method it only displays the last element of the array

(I'm still a beginner, so go easy on me please)

        System.out.print("Generated numbers for player 1 are: ");
        int values1 = 0;
        for (int i = 0; i <= 10; i  ) {
           
            values1 = 1 (int)(Math.random()*(6-1));
            System.out.print(values1 " ");
        }
    
        System.out.print("\nGenerated numbers for player 2 are: ");
        int values2 = 0;
        for (int i = 0; i <= 10; i  ) {
           
            values2 = 1 (int)(Math.random()*(6-1));
            System.out.print(values2 " ");
        }
        int[] player1 = {values1};
        int[] player2 = {values2};
        System.out.println("\n");
        sumArray(player1);
        sumArray(player2);
        
        System.out.println(sumArray(player1));
       
    }
    public static int sumArray( int[] sum) {
    int add=0;
    for(int i=0; i< sum.length; i  ) { 
      add  = sum[i];
    }

    return add;

CodePudding user response:

Main problem lies within your for loop. You are just updating values1 and values2 variable .You should be also adding them to the the arrays.

code will be

int[] player1 = new int[11];
for (int i = 0; i <= 10; i  ) {        
   values1 = 1 (int)(Math.random()*(6-1));
   player1[i] = values1;
   System.out.print(values1 " ");
}

In addition you don't need these 2 lines.

int[] player1 = {values};
int[] player2 = {values2};

CodePudding user response:

You are not understanding how to populate arrays

int[] player1 = new int[10];
for (int i = 0; i < 10; i  ) {
       
        values2 = 1 (int)(Math.random()*(6-1));
        System.out.print(values2 " ");
        player1[i] = values2;
}

I have changed the size of the array to be 10 and not as you have coded it (which result in entry of 11 elements)

CodePudding user response:

Your player1 & player2 are only containing single element & that is reason you are getting sum as last element's (or single element's ) value.

If you want to have values1 containing multiple values then consider defining it as array instead of single integer.

int values1 = new int[11];
for (int i = 0; i <= 10; i  ) {
           
            values1[i] = 1 (int)(Math.random()*(6-1));
            System.out.print(values1[i] " ");
        }

Similar case for your values2. After that, you can directly call:

sumArray(values1);

No need to define separate player1 & player2.

CodePudding user response:

I would suggest a few things.
First, you should try not to "duplicate" the code. Here Player1 and Player2 could be just passed as parameters for a method that would generate their arrays.
Second, if you want to do any manipulations on arrays, you should avoid [] but instead use ArrayList for example.
Also, you may want to look into Object Oriented logic, as you don't seem to use class for Person, depending on what you want to achieve, you could use a class "Person" with attribute "values" which would be of type ArrayList.

As for your code, you reallocate "value1" and "value2", so they are just integers, not lists.
This would give something like this:

public static int generateValue(PersonClass person) {
    ArrayList<Integer> values = new ArrayList<>();

    for (int i = 0; i <= 10; i  ) {
        values.add(1 (int)(Math.random()*(6-1)));
    }

    person.setValues(values);

    return sumArray(values);
}

public static int sumArray(ArrayList<Integer> values) {
    int add=0;
    for (Integer value : values) {
        add  = value;
    }
    return add;
}

This way, you will be able to retrieve your values and to print them as you want. The "PersonClass" part is not mandatory, it just depends if you want to keep your values associated to a Person or not (how long will you need those data).

(Example of class PersonClass :

public class PersonClass {
    private ArrayList<Integer> values = new ArrayList<>();

    public ArrayList<Integer> getValues() {
        return this.values;
    }

    public void setValues(ArrayList<Integer> newValues) {
        this.values = newValues;
    }
}

as you should always use getters and setters). Good luck on you way to Java learning.

  •  Tags:  
  • java
  • Related