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".
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