Operators & Flow Control Statements

First, the java operator

    1. Arithmetic operators

        Arithmetic operators include: "+", "-", "*", "/", "%", "++", "--", "+ (can mean adding strings)"

代码示例:
public class test01 {
public static void main(String[] args) {
                int a = 3;
                int b = 3;
                int c = 50;
                int d = 101;
System.out.println(++d%3*5 + ++d*2%2/2);
System.out.println("d"+"d+1");
System.out.println((c+b-a)/2%2*d);
}

}

        The principle of self-increment and self-decrement is: if the operator is in front of the variable, the variable will be incremented by 1 or decremented by 1, and then the new value of the variable will be returned. If the operator is behind the variable, the variable will also be incremented. Or decrement by 1, but return the original value of the variable. ++ is the first operation, and then the value, ++ is the first value, and then the operation. 
For example: int b = 3;
System.out.print(b++); //=3
System.out.print(b); //=4
System.out.print(b++); //=4
System.out. print(b++); //=5
System.out.print(++b); //=7
System.out.print(++b); //=8
System.out.println(++b); //=9

    2. Assignment operators
    "=", "+=", "-=", "*=", "/=", "%="

代码示例:
 public class test07 {
public static void main(String[] args) {
int a = 5;
a += 2;
System.out.println(a);
a-=4;
System.out.println(a);a*=4;System.out.println(a);a/=2;System.out.println(a);a%=4;System.out.println(a);}







}

3. Comparison operators (boolean)
  include: "==", "!=", "<", ">", "<=", ">="
4. Logical operators
 include: "&", "| ", "^", "!", "&&", "||"

示例:      int q =10;
int w = 16;
int e = 10;
int r = 19;
System.out.println(q==e);                    //T
System.out.println(q<=r);                     //T
System.out.println(q<r);                        //T
System.out.println(q>w);                      //F
System.out.println(w>=q);                    //T
System.out.println(w==q);                    //F
System.out.println((q<=w)&(q>=w));    //F
System.out.println((q<=w)&(r>=w));     //T
System.out.println((q>=w)^(q<=w));     //T

System.out.println((q!=w));                     //T

5. Ternary operator

    Format: (conditional expression)?expression1:expression2
    means: if the condition is true, the result of the operation is expression1;
                 if the condition is false, the result of the operation is expression2; 

    示例:
public class test08 {
public static void main(String[] args) {
int z,x = 10,y = 11;
z=(x>y)?x:y;
System.out.println(z);
}

}

Simple reference types (Scanner and Random) 

1. Scanner class 

        The Scanner class we want to learn is a reference data type. Let's first understand the reference data type.
        The use of reference data types is
                different from the definition of basic data type variables. The variable definition and assignment of reference data types has a relatively fixed procedure or format.
                data type variable name = new data type ();
                each reference data type has its function, we can call the function of the instance of that type.
                Variable name.Method name(); 
Scanner class
                Scanner class is a kind of reference data type, we can use this class to complete the user's keyboard input and get the input data.
                Scanner usage steps:
                import package: import java.util.Scanner;
                create object instance: Scanner sc = new Scanner(System.in);
        call method:   
                int i = sc.nextInt(); used to receive the digital   
                String entered by the console s = sc.next(); Example of a string to receive console input 
:
import java.util.Scanner;
public class test05 {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
System.out.println("请输入你的成绩:");
int a = 60;
int b = 70;
int c = 80;
int d = 90;
int e = 100;
int input = sr.nextInt();
if(input<b && input>=0) {
System.out.println("差");
}
else if (input >=b && input<=c) {
System.out.println("中");
}else if (input >=c && input<=d) {
System.out.println("良");
}else if (input >=d && input<=e) {
System.out.println("优");
}else{
System.out.println("你输入的是什么玩意!输入不合法。");}}


}

2. Random number class Random 
        Let's learn the class Random used to generate random numbers, which is also a reference data type.
        This Random class, which can generate random numbers of various data types, here we mainly introduce the way to generate integers and decimals.
        Method introduction
              public int nextInt(int maxValue) Generate a random integer in the range [0,maxValue), including 0, excluding maxValue;
              public double nextDouble() Generate a random decimal in the range [0,1), including 0.0, excluding 1.0. 

        We have already learned how to use the reference data type when learning how to enter Scanner by keyboard. Here, we will review it again: 
           How to use Random: 
                   Import import package: belongs to the package java.util.Random 
                   Create instance format: Random variable name = new Random(); 

示例:
import java.util.Random;
public class test04 {
public static void main(String[] args) {
Random rd = new Random();
int aa = rd.nextInt(100);
System.out.println(aa);
double dd = rd.nextDouble();
System.out.println(dd);
long ll = rd.nextLong();
System.out.println(ll);
float ff = rd.nextFloat();
System.out.println(ff);
double d = rd.nextDouble();
int f = (int)(d*100);
System.out.println(f);
}

}

Flow control statement 
    selection structure if 

    if statement
    if...else statement
    if....else if...else if...else statement
example:
import java.util.Scanner;
public class test06 {
public static void main(String[] args ) {
System.out.println("Let's play a small game of guessing the score, give you five chances, if you guess wrong, invite me to dinner.");
System.out.println("Start entering your first number :");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int scd =77;
if(a == scd) {
System.out.println("You are so smart that Guess the answer at once, and give you candy.");
}else if(a>77){
System.out.println("The score of your guess is too high, it is lower than this, you still have Four chances!");
}else if(a<77){
System.out.println("The score of your guess is too low, it is higher than this, you have four chances!");
}
int b = sc.nextInt();
if(b == scd) {
System.out.println("You are too smart to guess the answer at once, and give you candy.");
}else if(b>77){
System.out.println("The score of your guess Too high, it is lower than this, you still have three chances!");
}else if(b<77){
System.out.println("The score of your guess is too low, it is higher than this, You still have three chances!");
}
int c = sc.nextInt();
if(c == scd) {
System.out.println("You are too smart to guess the answer at once, give You give candy.");
}else if(c>77){
System.out.println("The score of your guess is too high, it is lower than this, you have a second chance!");
} else if(c<77){
System.out.println("The score of your guess is too low, it is higher than this, you have a second chance!");
}
int d = sc.nextInt();
if(d == scd) {
System.out.println("You're too smart to guess the answer at once, and give you candy.");
}else if(d>77){
System.out .println("The score of your guess is too high, it is lower than this, you have another chance!");
}else if(d<77){
System.out.println("The score of your guess is too low, it is higher than this, you have another chance!");
}
int e = sc.nextInt();
if(e == scd) {
System .out.println("You are too smart to guess the answer at once and give you candy.");
}else if(e>77){
System.out.println("gameover, you are stupid , Hahahaha...");
}else if(e<77){
System.out.println("gameover, you are so stupid, hahaha...");
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324857646&siteId=291194637