-----------siwuxie095
Logical operation is an operation on the basis of relational operation, which can deal with more complicated problems.
The result of the logical operation is true or false
Types of logical operations:
1. Logic and Operation:
(1) long Road and Operation
The operation symbol of long road and Operation:&
& Both sides are integers, is the bitwise and operation (binary);& on both sides of the relationship operation, is the logic and Operation
Long distances and operations calculate the results of the relational operations on both sides.
Such as:
(2) Short circuit and Operation
Operation Symbol:&& for short circuit and operation
The result of the relational operation to the left of the operator is false , the relational operation on the right is no longer
Calculation (this calculation feature can improve the efficiency of the program, and is safe)
Such as:
2. Logic or operation:
(1) long road or Operation
Operators for long distances or operations: |
| is a bitwise OR operation (binary) when both sides are integers;| on either side of a relational operation, is a logical or an operation
A long road or operation calculates the results of the relational operations on both sides.
Such as:
(2) Short circuit or Operation
Operator of short Circuit or operation: | |
When the relationship operation on the left side of the operation symbol evaluates to true, the relational operation on the right is no longer performed.
Direct result of True (also improves computational efficiency)
Such as:
3. Logical non-operation:
Such as:
4, the logical operation priority:
such as: (Note: The precedence of relational operations is higher than all logical operations)
Program Application: Keyboard input a 5- point score to determine whether the number is 5- point score
Code:
|
package com.siwuxie095.test;
import java.util.Scanner;
Public class Test18 {
Public static void main (string[] args) {
Scanner scanner=new Scanner (System. In);
System.out.println (" Please enter 5 score:");
int score=scanner.nextint ();
// and arithmetic
boolean isfivescore=score>=0&&score<=5;
// Non-operational
//boolean isfivescore=! ( score<0| | SCORE>5);
System.out.println (score+" is a 5 -point score:"+isfivescore);
// or operations
//boolean isnotfivescore=score<0| | score>5;
//system.out.println (score+ " is not a 5 -point score:" +isnotfivescore);
}
}
|
In Eclipse:
Run at a glance:
"Made by siwuxie095"
Java Logical Operations