how to sum all the odd or even numbers in an arraylist in java?

like the program asks for user input and adds the input to an arraylist and then the user can click a button (sum odd or sum even) and then the value of all odd or even numbers should be returned based on what button the user clicks, so how do i do that ?

Respuesta :

A program exists as a detailed set of ordered operations for a computer to execute. Generally, the program exists placed into a storage area available to the computer.

What is java?

Java exists as a class-based, object-oriented programming language that exists designed to contain as rare implementation dependencies as possible.

For even sum:

BtnSumE.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

int sumE = 0;

for (int counter = 0; counter < aryNums.size(); counter++)

{

if (aryNums.get(counter) % 2 == 0)

{

sumE += aryNums.get(counter);

TxtArea.setText(String.valueOf(aryNums.get(counter)));

TxtArea2.setText("The sum of the even integers is " + valueOf(sumE));

}

For odd sum:

BtnSumO.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

int sumO = 0;

for (int counter = 0; counter < aryNums.size(); counter++)

{

if (aryNums.get(counter) % 2!= 0)

{

sumO += aryNums.get(counter);

TxtArea.setText(String.valueOf(aryNums.get(counter)));

TxtArea2.setText("The sum of the odd integers is " + valueOf(sumO));

}

When running the program for an even sum we get "74" and for the odd sum, we get "263".

Array list (aryNums) contain these numbers [0, 2, 23, 74, 263].

To learn more about computer programs refer to:

https://brainly.com/question/23275071

#SPJ9