[Beginner] Java variable / logical operator / extended for statement [Note 22]

What I was doing in mid-December

First web page creation

Since the learning of html / css, java script, jQuery has finished Once I summarized my profile, my work so far, and things around me I created a web page. It's a simple page Since it was my first time, I spent about 3 weeks slowly.

I created it while using hover and accordion I struggled a little with the accordion, so be sure to create it again Try to use it and try to get used to it.

Next, we will continue learning Java so that we can create apps.

Java

Method / output

The basic writing method is decided, there is a method part in the class part, and in that It is in the form of outputting.


System.out.println("Numerical value you want to output");

The above is how to write the output part. Numerical values do not require "" in mathematical formulas! !! (If you add it, it will be judged as a character string.)

variable

It basically consists of data type + variable name. Variable = something like a box to put values (data). Be careful because the data type changes depending on the value you put in the variable!

For example, "int" to enter a numerical value. The variable name that follows is It is good to have something that is easy to see, so in the case of numerical values, it is better to set it to "number". There are many other data types, so some are I will write it below.

Data types that are likely to be used frequently ・ Int ⇒ integer ・ Double ⇒ decimal ・ When boolean ⇒ true or false (true / false value) ・ Char ⇒ 1 character (when using initials etc.) ・ String ⇒ Character string (first uppercase)

Logical operator

An expression of "and", "or", or "not". ・ && ⇒Katsu ・||⇒ or ・ Add! ⇒ Not
Example

!(x<=30)
/*Indicates that x is not less than 30*/

if statement / switch statement

・ If statement ⇒ often used with else if "If XX" ⇒ Output "Otherwise, when XX" ⇒ Output And so on, the output can be changed in some cases.

int x=8;
if(x%2==0){
 System.out.println("Even");
}else if(x%2==1){
 System.out.println("Odd");

・ Switch statement The previous sentence can be rewritten

int x=8;
switch(x%2){
 case 0: /*← If this value is matched, the following processing is executed*/
  System.out.println("Even");
  break; /*← Be careful not to forget this*/
 case 1:
  System.out.println("Odd");
  break;
} 

・ Default You can set the case that does not match any case

....
 default
  System.out.println("other than that");
  break;
}

while statement / for statement

・ While statement Used when repeating.

int i = 1;
while(i<=10){
 System.out.println(i+"It is displayed times.");
 i++;

In the above case, from "Displayed once" to "Displayed 10 times." It is displayed continuously. Without [i ++;], it would be an infinite loop, which would be a heavy burden, so don't forget!

・ For statement It is possible to rewrite the while statement earlier with a for statement.

for(int i=1;i<=10;i++){
 System.out.println(i+"It is displayed times.");

Array / extended for statement

The way to make it is similar to what I did before One thing to keep in mind is to remember [] after the data type!

String[] names={"Nana","Hana","Mana"};

-Use the for statement to display the arrays in order.

String[] names={"Nana","Hana","Mana"};
for(int i =0;i<names.length;i++){
 System.out.println(names[i]);
}

-Further rewrite with an extended for statement

string[] names={"Nana","Hana","Mana"};
for(String name:names){
 System.out.println(name);
}

How to write by taking out odd numbers and even numbers and adding them together

・ I wrote using the extended for statement

/*....Abbreviation*/

int[] numbers={1,4,6,9,13,16};
 int oddSum = 0;
 int evenSum = 0;
/*First assign an array and then oddSum(Odd)evenSum(Even)Initialize by substituting 0 for*/

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

/*number%Make an if statement of even number or not with the formula of 2, and write an instruction to add to each*/

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

... 

by this "The sum of odd numbers is 23" "The sum of even numbers is 26" Is output.

Be careful / look back

The basics are easy to understand, but when it comes to applications I don't think I really understand it because it's often inflexible. So, I think it would be effective to do the exercises when you forget it. Also, try to practice the exercises as time goes by.

When assigning an array, be careful because you have forgotten the "[]" after the data type many times.

Recommended Posts

[Beginner] Java variable / logical operator / extended for statement [Note 22]
[Java] for statement / extended for statement
Java, for statement / while statement starting from beginner
Java for statement
[Java] for statement, while statement
(Memo) Java for statement
How to loop Java Map (for Each / extended for statement)
[Java] Basic statement for beginners
Java learning memo (logical operator)
[Java] Make variables in extended for statement and for Each statement immutable
[Java] Get Map key / value pairs using extended for statement
[Java beginner] == operator and equals method
[Java] How to turn a two-dimensional array with an extended for statement
[Java] Nowadays, the extended for statement is not exclusively for List, isn't it?
[Beginner] Java method / class / external library [Note 23]
Java, if statement / switch statement starting from beginner
java beginner 4
java beginner 3
java beginner
[Java] How to get HashMap elements by loop control using extended for statement
Compare the speed of the for statement and the extended for statement.
Use Java7 try-with-resources statement for Cursor close processing
Array vs ArrayList vs HashMap Extended for Statement Race