[Note] Java Output of the sum of odd and even elements

Introduction

I went to a programming school in February of this year and got a job at an IT company in July. I wanted to output the language I learned, so I decided to start posting articles! This time I will post an article about Java that I did not study at programming school.

Development environment

This time I tried it in a comprehensive development environment called "Eclipse". Please refer to this article to build a development environment.

Creating a Java development environment with Eclipse (Mac version)

Implementation


class Main {
  public static void main(String[] args) {

    int[] numbers = {1, 5, 8, 10, 13, 18};
    int oddSum = 0;
    int evenSum = 0;

    for (int number : numbers) {
      if (number % 2 == 0) {
        evenSum += number;
      } else {
        oddSum += number;
      }
    }

    System.out.println("The sum of odd numbers is" + oddSum + "is");
    System.out.println("The sum of even numbers" + evenSum + "is");
  }
}

I will explain the above code in order from the top!

Define the main method in the Main class.

class Main {
  public static void main(String[] args) {

Write some integers you like in the variable number in an array with int type elements.

int[] numbers = {1, 5, 8, 10, 13, 18};

Define variables oddSum and evenSum in int type and put 0 in both variables.

int oddSum = 0;
int evenSum = 0;

Repeat the process with the for statement and create a conditional branch with the if statement. This time we will use something called an "extended for statement".

for(Data type variable:Array name) {
Iterative processing;
}

Enter the int type variable number and the array numbers in the statement. In the if statement, condition "when number is even".

for (int number : numbers) {
      if (number % 2 == 0) {
        evenSum += number;
      } else {
        oddSum += number;
      }
}

And finally I will output it!


System.out.println("The sum of odd numbers is" + oddSum + "is");
System.out.println("The sum of even numbers" + evenSum + "is");

Don't forget the closing brackets for the Main class and main methods! ]

 }
}

Run

Execution result


The sum of odd numbers is 19.
The sum of even numbers is 36

Is displayed, it is successful!

If you get an error

There is a possibility that ; is missing, so please check it!

Finally

Everyone who is studying programming, although I am also studying acclaim Let's be patient and work together to make coding fun! Thank you for watching till the end! !! !!

Recommended Posts

[Note] Java Output of the sum of odd and even elements
A program (Java) that outputs the sum of odd and even numbers in an array
Please note the division (division) of java kotlin Int and Int
[Java] Delete the elements of List
Read the first 4 bytes of the Java class file and output CAFEBABE
Examine the memory usage of Java elements
Compare the elements of an array (Java)
Output of the book "Introduction to Java"
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
Feel the passage of time even in Java
Output the sum of each name and its contents from a multiple array
The comparison of enums is ==, and equals is good [Java]
Convert the array of errors.full_messages to characters and output
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
I summarized the types and basics of Java exceptions
Summarize the additional elements of the Optional class in Java 9
What if the results of sum and inject (: +) are different?
I didn't understand the behavior of Java Scanner and .nextLine ().
I tried to summarize the basics of kotlin and java
Command to check the number and status of Java threads
Java beginners briefly summarized the behavior of Array and ArrayList
I tried JAX-RS and made a note of the procedure
[Java] Try editing the elements of the Json string using the library
Change the file name and output destination of the JavaVM error file
Advantages and disadvantages of Java
[Java] I thought about the merits and uses of "interface"
Handling of java floating point [Note] while reading the reference book
[Java] Various summaries attached to the heads of classes and members
Utilization of Java array elements, for, length, value, and extended for statements
[Java] Get the dates of the past Monday and Sunday in order
Output the difference between each field of two objects in Java
I tried the input / output type of Java Lambda ~ Map edition ~
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
About fastqc of Biocontainers and Java
[Java version] The story of serialization
Note on the path of request.getRequestDispatcher
[Java] Output by FormatStyle of DateTimeFormatter
This and that of the JDK
Output PDF and TIFF with Java 8
[Note] Handling of Java decimal point
[Java] Judgment of identity and equivalence
The origin of Java lambda expressions
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
How to output the sum of any three numbers, excluding the same value
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of forgetting to close a file in Java and failing
A note on the differences between interfaces and abstract classes in Java
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
This and that of the implementation of date judgment within the period in Java
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
[Java improvement case] How to reach the limit of self-study and beyond
Find the maximum and minimum of the five numbers you entered in Java
I translated the grammar of R and Java [Updated from time to time]
Display Japanese calendar and days of the week using java8 standard class
Get the result of POST in Java