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
Last time, I dealt with + used for addition and concatenation, and = used for substitution. There are many Java operators, but this time we will deal with typical ones. Listed below.
** 1. Multiplication (*), division (/), remainder (%) ** ** 2. Equivalent (==) and unequal (! =) ** ** 3. Increment (++) and decrement (-), and their pre-postfix **
In Java (at least Python, as I know), use * instead of x for multiplication. Use / instead of ÷ for division, and% for the remainder (which gives the number of remainders).
Division by zero is not allowed in division by zero (division by zero). It does not appear as an error when compiling, but an exception is thrown at the time of execution and the process stops. Some people in Qiita have an article about division by zero on this issue, so I'll link to it (though it's an article over 3 years ago).
"Divid by 0" @yamayasan
Here's an example of what I actually made a mistake.
ZeroDivisionError.java
int dayInMonth = 0;
...
//I forgot to enter a value for dayInMonth in the process so far.
averageFeedCatFood =
totalFeedCatFood / dayInMonth;
To calculate the average amount of food given to a cat in a month, simply divide the total amount of food by the number of days in a month. It's bad to set the initial value to 0, but don't forget to enter the value in dayInMonth in the middle of the process.
I wrote last time that = is not equivalent and is used as an assignment. Use == to represent the original equivalence, and! = To represent the inequality (no spaces between = and!). Equivalence and inequality examine the relationship between operands 2 and are also called ** relational operators **. It is often used when equivalence / inequality is used as a condition of an if statement, and it is judged by the truth value of the expression.
ex) if( int lastDayInFeburary == 29){ totalPayment = totalPayment + dailyPayment; } else{ totalPayment = totalPayment; } //2月に29日がある場合、合計支払いに一日分の支払いを追加する処理。
I made a mistake below.
WrongRerational.java
boolean judge = True;
int judgementDay = 30;
if(judgementDay != 30){
judge = False;
}
else{
judge = True;
}
System.out.println("Today's announcement" + judge + "So it's not Judgment Day.")
//I simply misunderstood which judge was
//I have made the sentence a helpful sentence.
When it comes to more difficult conditional expressions, if you rely on if, you will be in trouble because the results will be different from what you expected.
Increment (++) increases the value of the variable by 1 and assigns it, and decrement (-) decreases the value of the variable by 1 and assigns it. This is how to write it. ex) int day++; // This is the same process as int day = day + 1 ;. // int day + = 1 is the same ** (assignment operator) **
int couponTickets--; // This is the same as int couponTickets = couponTickets -1; // couponTickets-= 1 is the same ** (assignment operator) **
Also, this increment / decrement is ** processed differently depending on whether it is placed before or after the variable **.
For example, find out which comment is wrong. Note that it is too long (post the entire class process).
piggyBank.java
class piggyBank
{
public static void main(String[] args)
{
int piggyBank = 99;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//99
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//99
piggyBank = piggyBank++;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//99
piggyBank = piggyBank++;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//99
piggyBank = ++piggyBank;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//100
piggyBank = ++piggyBank;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//101
piggyBank = piggyBank--;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//100?
piggyBank = piggyBank--;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//100
piggyBank = --piggyBank;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//99
piggyBank = --piggyBank;
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//99
System.out.println("The current piggy bank" + piggyBank + "There is a circle.");
//
}
}
In the case of postfix, substitute and then add +1 to the value. In the case of prefix, substitute after adding +1 to the value. Review required here.
I don't think we can understand the equivalence and inequality yet. Review at the time of binary operation. There are many mistakes in increment decrement. You will have to make it yourself and practice.
I haven't written it before, so I'll give you my reference book. Add to past articles. 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