Postscript: 2020/3/6 We received a large editing request from @shiracamus, so we will replace it there. (I left the old article as a memorandum for myself in the limited post) Postscript 2020/3/6 Corrected the content that the semicolon was required at the end of the conditional operator. I am always grateful for your help.
This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
** Logical Operator ** is an operator used to describe more complicated conditions while using the conditions and relational operators mentioned last time. This time, I will write "how many operands (terms) are operators?" An operator that takes two operands is called a ** binary operator **, and an operator that takes only one operand is called a ** unary operator **. Write an expression in the operand.
&&
: Right item && Left item
Logical AND. Binary operator.
Evaluates the expressions in the right and left terms, and returns true if both ** are true **.
However, the right term is evaluated first, and then the left term is evaluated. If the right term is false, the left term is not evaluated. This is called short-circuit evaluation.||
: Right item||Left item
&&
, it is a short-circuit evaluation.!
: ! Unary
</ li> Logical denial. Unary operator.
** Returns true if the unary expression evaluation result ** is false, and returns false if true.You can see what the truth values are by using the Venn diagrams that you often see in mathematical "sets". I'm sorry I'm not good at it, but I applied it. (Most important issue: drawing ability)
Green && Red: true is blue Green||Red:true is everything except ashes ! (Gray): Red Blue Green
Will be. (I'm sorry to point out that blue is actually distorted.)
digitForture.java
import java.io.*;
class digitForture
{
public static void main(String[] args) throws IOException
{
System.out.println("Please enter your favorite single digit number.");
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int digit = Integer.parseInt(str);
if(digit > 10 && digit <= -1){
System.out.println("Please enter a number up to one digit.");
//Hmm?
}
else{
System.out.println("Your numbers" + digit + "is not it?");
System.out.println("I will check it from now on, so please wait for a while....。");
}
}
...//Omitted thereafter. Suppose you have prepared a fortune-telling table linked to a single-digit number used for fortune-telling.
At this rate, the logical operations prepared will not be effective, so properly**&&
To||
To**Must.
Are you dealing with AND or OR? I felt that it was very necessary to practice making Venn diagrams (easy to see) in order to organize my thoughts.
** Conditional operator ** takes three ** operands **, and for ** conditional expression evaluation result **, ** expression when true ** and expression when ** false * * Can be written and returns the evaluation result of either expression. The other formula is not evaluated.
After the "conditional expression", write "?
" And "expression when true", and then write ":
" and "expression when false".
Uninflected form of conditional operator
Conditional expression?Expression when true:Expression when false
One point to note. There is no trailing; (semicolon) in the quote. ~~ ** Must be included **. ~~
2020/3/6 postscript: It is not necessary to include it because it can be used as a condition in the text. The end of the sentence really needs a semicolon. This is not limited to this operator. (You don't have to be afraid because it will be played with a compile error. At first, I was frustrated that it was often played. Now I feel that it helps me to play it.)
I came up with "pancakeSearch.java" to distinguish between bread and cake, but I have the following problems.
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Recommended Posts