Studying Java ―― 9

Judgment of odd and even numbers

After writing the conclusion, I should have used "%" in the conditional expression of if.

When I do four arithmetic operations, I always use "remainder" I thought, but I see, I used it.

Divide by 2 and if there is no remainder ... even Divide by 2 and if there is a remainder ... odd

I wish I had this kind of idea.

■ Determine whether the number entered from the keyboard is odd or even Add ↓ to the shape of the person who can input with the usual keyboard

python


int num1;

System.out.print("Enter an integer> ");
num1 = Integer.parseInt(br.readLine());      //Keyboard input

if (num1 % 2 == 0){         //When there is no remainder after dividing by 2
	System.out.println("The entered number is an even number");
} else if (num1 % 2 == 1){     //If there is a remainder, this is it
	System.out.println("The entered number is odd");
}

"Else if" could have been "else" I wanted to add a conditional expression somehow, so I set it to "else if".

Judge whether it is a multiple of 〇

After writing the conclusion, I should have used "%" in the conditional expression of if.

When I do four arithmetic operations, I always use "remainder" I thought, but I see, I used it "again".

When it is a multiple of 3 Divide by 3 and if there is no remainder ... a multiple of 3 Divide by 3 and if there is a remainder ... not a multiple of 3

After writing, I noticed that even numbers are multiples of 2. What a weakness in math.

■ Determine if the number entered from the keyboard is a multiple of 3 Add ↓ to the shape of the person who can input with the usual keyboard

python


int num1;

System.out.print("Enter an integer> ");
num1 = Integer.parseInt(br.readLine());      //Keyboard input

if (num1 % 3 == 0){         //When there is no remainder after dividing by 3
	System.out.println("The entered number is a multiple of 3");
} else {                     //If there is a remainder, this is it
	System.out.println("The number entered is not a multiple of 3");
}

Where "else" is "else if"(num1%3==1 || num1%3==2)Is it okay? Rather, I should have gone with the latter because I am studying.

「||(Or)It is also a practice to use.

Recommended Posts

Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying java9 (jShell)
Studying Java 8 (Stream)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see method)
Studying Java 8 (see constructor)
Java
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Java
Java learning (0)
[Java] array
[Java] Annotation
[Java] Module
Java array
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
Studying Java # 6 (How to write blocks)
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Java (set)
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Array
[Java] Polymorphism
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3