Getting Started with java|Operators

Started at: Introduction to java (3) | Operators Start from scratch!!! The last issue will cover eight basic types of ...
operator

Started at: Introduction to java (3) | Operators
Start from scratch!!!

The last issue will cover eight basic types of java: int byte short long float double char boolean and the use of these eight basic types

This issue explains the use of operators in java. What operators do you know about them? You can learn how to use them. Let's start today.

operator

1. Overview

Logical Operator: The result is boolean type & -- the relationship of yes and only (and) For 1 & 2 to get the final result true, the requirements for 1 and 2 are: both must be true && -- the relationship of yes and yes (double and/or short-circuit and) 1 && 2, efficient, 2 will be short-circuited when 1 result is false | -- Yes or Relationship 1 | 2 To get the final result true, the requirements for 1 and 2 are: 1 or 2 can have one true || -- Yes or Relation (Double or/Short Circuit or) 1 || 2, efficient, 2 will be short-circuited when the result of 1 is true

2. Exercise 1: Leap Year in Ordinary Year

Enter the year number to determine if it is a leap year.
Two conditions:
1. Can be divided by 4 and cannot be divided by 100
2. divisible by 400

package cn.qile.basic; import java.util.Scanner; public class Test4_RunNian { public static void main(String[] args) { //1. The year number entered by the receiving user System.out.print("Please enter a year:"); int year = new Scanner(System.in).nextInt(); //2. Determine whether year is a normal or leap year String desc = "Ordinary year";//Setting default value is flat year //If // 1. Can be divided by 4 and cannot be divided by 100 if (year % 4 == 0) { //Divisible by 4 if (year % 100 != 0) { //Cannot be divisible by 100 desc = "Leap year"; //Modify desc to leap year } } // 2. divisible by 400 if (year % 400 == 0) { desc = "Leap year"; //Modify desc to leap year } // System.out.println("Year 2000 is a leap year"); //+Split String System.out.println(year + "Year is" + desc); } }
Simplify Code

Through || -- Yes or Relationship (Double or/Short Circuit or)
-- 1 || 2, efficient, 2 will be short-circuited when the result of 1 is true

//If ((small judgement 1 &&small judgement 2) ||big judgement 2) if(( year%4==0 && year%100 != 0 ) || year%400 == 0 ){ desc="Leap year"; }

3. Exercise 2: Increase and Decrease

package cn.qile.basic; //Test Self-Increasing++ Self-Decreasing-- public class Test5_ZiZeng { public static void main(String[] args) { //Symbol before, change before use //Symbol after, use before change int a = 1; System.out.println(a++);//1 int b = 1; System.out.println(++b);//2 System.out.println(++b+a+b++);//8,3+2+3 //TODO Decrease int m = 1; System.out.println(m--);//1 int n = 1; System.out.println(--n);//0 System.out.println(--m-n-m--);//0,-1-0-(-1) } }

4. Ternary operations

package cn.qile.basic; import java.util.Scanner; //Ternary operator public class Test6_Max { public static void main(String[] args) { //Large values in two numbers int a = 10; int b = 6; //Ternary 1? 2: 3 //The final result is 2 or 3, depending on the result of 1, if 1 is established, 2 is obtained, if 1 is not established, 3 is obtained. int max = a > b ? a : b ; System.out.println(max); //Receive keyboard input, three numbers, take out the maximum System.out.print("Please enter m ="); int m = new Scanner(System.in).nextInt(); System.out.print("Please enter n ="); int n = new Scanner(System.in).nextInt(); System.out.print("Please enter o ="); int o = new Scanner(System.in).nextInt(); //1. Define the maximum value of a variable record, hold the m to n ratio, and give the result to the variable to be saved int result = m > n ? m : n; //2. Hold result and o ratio to get the maximum value to be saved by maxValue // result = result > o ? result : o ; int maxValue = result > o ? result : o ; System.out.println(maxValue); //TODO optimization //Take m to n ratio? M to o ratio: n to O ratio int result2 = m > n ? (m > o ? m : o ) : (n > o ? n : o ); System.out.println(result2); } }

Next issue: Getting started with java (4) | Data types

It will be launched tomorrow, so look forward to it. If you want to read the previous issues, follow me and reply to Getting Started with java!!!

If you like, you can press down the QR code for a long time
Focus on it!!

Happy waiting for you

11 June 2020, 21:06 | Views: 2750

Add new comment

For adding a comment, please log in
or create account

0 comments