When seeking multiple in a Java array



class Main {
  public static void main(String[] args) {
    int[] numbers = {1, 6, 11, 16, 21, 26};
    int oddNumber = 0;
    int evenNumber = 0;
    for (int number : numbers) {
      if (number % 2 == 0) {
        evenNumber += number;
      } else {
        oddNumber += number;

    System.out.println("The odd sum is" + oddNumber + "is");
    System.out.println("Even sum is" + evenNumber + "is");

The above result is the sum of odd numbers is 33 Even sum is 48 It will be. Get the elements of the array numbers one by one and the value of that element is If it is odd, use the variable oddNumber If it is an even number, the variable evenNumber is used. "%" Is used in the if statement to determine whether it is an even number or an odd number. Is it even with k "even Number + = number;" by setting "% 2 == 0"? If not, else "oddNumber + = number;" will be executed.

Recommended Posts

When seeking multiple in a Java array
Swift: A trap when setting multiple initial elements in an array
A note when you want Tuple in Java
Multiplication in a Ruby array
Find a subset in Java
What I learned when building a server in Java
How to convert a file to a byte array in Java
<Java> When creating a fixed-length (row) * variable-length (column) two-dimensional array
Resolve CreateProcess error = 206 when running Java in a Windows environment
Convert a Java byte array to a string in hexadecimal notation
Read WAV data as a byte array in Android Java
Sorting hashes in a Ruby array
3 Implement a simple interpreter in Java
I created a PDF in Java.
<java> When "EXCEPTION_ACCESS_VIOLATION" appears in awt
A simple sample callback in Java
Get stuck in a Java primer
Catch multiple exceptions together in java
How to make a Java array
[Java] array
Java array
Java array
java (array)
Java array
[Java] Array
Java array
Cast an array of Strings to a List of Integers in Java
java array
[Java] Array
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
Map without using an array in java
[Creating] A memorandum about coding in Java
Organized memo in the head (Java --Array)
Java creates a table in a Word document
Java creates a pie chart in Excel
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Implement something like a stack in Java
Split a string with ". (Dot)" in Java
Creating a matrix class in Java Part 1
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Bad habits pointed out in code reviews when Java was a beginner
How to pass a proxy when throwing REST over SSL in Java
I want to ForEach an array with a Lambda expression in Java
[Java] [For beginners] How to insert elements directly in a 2D array
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
Add .gitignore when creating a project in Xcode
Escape processing when creating a URL in Ruby
java array variable
Two ways to start a thread in Java + @
Read a string in a PDF file with Java
Create a CSR with extended information in Java
A story about the JDK in the Java 11 era
Partization in Java
How to display a web page in Java
[Android / Java] Operate a local database in Room
Measure the size of a folder in Java