Java operator, keyboard entry, select process control statement

Java operator, keyboard entry, select process control statement

operator

Operator overview

  • operator
    Symbols that operate on constants and variables are called operators
  • Expression
    Operators are used to connect constants or variables, which can be called expressions. Expressions connected by different operators represent different types of expressions.
    Define two variables of type int a,b, add (a + b)
  • Common operators:
    Arithmetic operator
    Assignment Operators
    Relational operators
    Logical operators
    Ternary operator

Arithmetic operator

Numerical operation
  • Arithmetic operator:
    +,-,* ,/,%,++,–
package com.itheima_01;
/*
 * Common operators:
 * 		Arithmetic operator
 * 		Auto increment and auto decrement operator
 * 		Assignment Operators 
 * 		Relational operators
 * 		Logical operators
 * 		Ternary operator 
 * 
 * Arithmetic operator:
 * 		+,-,*,/,%
 * 
 * 		/Difference between and%
 * 			/: Obtain the quotient of the division of two data
 * 			%: Get the remainder of the division of two data
 * 
 * Be careful:
 * 		Integer division can only get integer. In order to get a decimal, floating-point numbers must be used.
 */
public class OperatorDemo {
	public static void main(String[] args) {
		//Define two variables of type int
		int a = 5;
		int b = 3;
		
		System.out.println(a+b);
		System.out.println(a-b);
		System.out.println(a*b);
		System.out.println(a/b);
		System.out.println(a%b);
		System.out.println("------------");
		
		System.out.println(5/4);
		System.out.println(5.0/4);
		System.out.println(5/4.0);
	}
}
  • The difference between arithmetic operators and division:
    %: remainder operator. What we get is the remainder of the two divided data.
    /: division operator. The result is a quotient of two data divided by each other.
    Usage scenario:%: judge whether two data are divided.
Characters and strings participate in addition operations

In fact, the character participates in the operation by using the corresponding value of the character
'a' 97
'A' 65
'0' 48
What we are doing here is not adding, but string splicing.
String and other types of data are spliced, and the result is string type.

package com.itheima_01;
/*
 * Character participates in addition operation: in fact, it is calculated by the data value represented by the character stored in the computer.
 * 		'a'	97
 * 		'A'	65
 * 		'0'	48	
 * 
 * String participates in addition operation: in fact, this is not addition, but string splicing.
 */
public class OperatorDemo2 {
	public static void main(String[] args) {
		//Define two variables, an int type and a char type
		int a = 10;
		char ch = 'a';
		System.out.println(a + ch);
		System.out.println("----------------");
		
		//String addition
		System.out.println("hello"+"world");
		System.out.println("hello"+10);
		System.out.println("hello"+10+20);
		System.out.println(10+20+"hello");
	}
}
An overview and usage of auto increasing and auto decreasing operators
  • ++, - Operator: add 1 or subtract 1 to a variable.
  • ++Or - it can be placed after or in front of a variable.
    When used alone, + + or - whether placed before or after a variable, the result is the same.
  • When participating in the operation:
    If + + or - after a variable, first take the variable to participate in the operation, and then do + + or –
    If + + or - is in front of the variable, do + + or - first, and then take the variable to participate in the operation
package com.itheima_02;
/*
 * Auto increment and auto decrement operator: + +--
 * 
 * Function: self + 1 or - 1
 * 
 * ++And -- can be placed before or after a variable.
 * When using a variable alone, put it in front of or behind the variable, and the effect is the same.
 * When participating in other operations:
 * 		++After the variable, first operate the variable, and then the variable++
 * 		++In front of the variable, first variable + +, then operation
 */
public class OperatorDemo {
	public static void main(String[] args) {
		//Define a variable of type int
		int a = 10;
		System.out.println("a:"+a);
		
		//a++;
		//a--;
		//++a;
		//--a;
		//System.out.println("a:"+a);
		
		//int b = a++;
		int b = ++a;
		System.out.println("a:"+a);
		System.out.println("b:"+b);
	}
}

Assignment Operators

Assignment operator classification:
Basic assignment operators:=
Extended assignment operators: + =, - =, * =, / =,%=
+=: a+=20; equivalent to a = (a's data type) (a + 20);

package com.itheima_03;
/*
 * Assignment operator:
 * 		Basic assignment operators:=
 * 		Extended assignment operators: + =, - =
 */
public class OperatorDemo {
	public static void main(String[] args) {
		//Defining variables
		int a = 10; //Assign 10 to variable a of type int
		System.out.println("a:"+a);
		
		//Extended assignment operator:+=
		//Operate the data on the left and the data on the right of the operator, and assign the result to the left
		//a = a + 20;
		a += 20;
		System.out.println("a:"+a);
		
		//short s = 1;
		//s = s + 1;
		
		//The extended assignment operator implies cast.
		//a+=20
		//Equivalent to
		//a =(a's data type) (a+20);
		short s = 1;
		s += 1;
		System.out.println("s:"+s);
	}
}

Relational operators

Relational operators:
== ,!=,>,>=,<,<=
The results of relational operators are boolean, that is, either true or false.
matters needing attention:
The relational operator '= =' cannot be written as' = '.

package com.itheima_04;
/*
 * Relational operators:
 * 		==,!=,>,>=,<,<=
 * 		The result of the relational operator operation is a boolean type.
 * 
 * matters needing attention:
 * 		Never write = = as=
 */
public class OperatorDemo {
	public static void main(String[] args) {
		//Define three variables
		int a = 10;
		int b = 20;
		int c = 10;
	
		//==
		System.out.println(a == b);
		System.out.println(a == c);
		System.out.println("------------");
		
		//!=
		System.out.println(a != b);
		System.out.println(a != c);
		System.out.println("------------");
		
		//>
		System.out.println(a > b);
		System.out.println(a > c);
		System.out.println("------------");
		
		//>=
		System.out.println(a >= b);
		System.out.println(a >= c);
		System.out.println("------------");
		
		System.out.println(a == b);
		System.out.println(a = b);//20. Assign the value of b to a, and output a as the result
	}
}

Logical operators

Overview of logical operators
  • Logical operators are used to connect relational expressions. In Java, they cannot be written as 3 < x < 6, but should be written as x > 3 & & x < 6.
  • What are the logical operators
    &&,|| !
  • Conclusion:
    &&: false if there is false.
    ||: true if there is true.
    !: true if not false, false if not true.
  • Features: even number does not change itself.
package com.itheima_05;
/*
 * Logical operators:
 * &&:False if there is false
 * ||:True if true
 * !:true Then false,false then true
 */
public class OperatorDemo {
	public static void main(String[] args) {
		//Defining variables
		int a = 3;
		int b = 4;
		int c = 5;
		
		//& Logic and
		System.out.println((a>b) && (a>c)); //false && false
		System.out.println((a<b) && (a>c)); //true && false
		System.out.println((a>b) && (a<c)); //false && true
		System.out.println((a<b) && (a<c)); //true && true
		System.out.println("------------");
		
		//Logic or
		System.out.println((a>b) || (a>c)); //false || false
		System.out.println((a<b) || (a>c)); //true || false
		System.out.println((a>b) || (a<c)); //false || true
		System.out.println((a<b) || (a<c)); //true || true
		System.out.println("------------");
		
		//Logic is not
		System.out.println((a>b));
		System.out.println(!(a>b));
		System.out.println(!!(a>b));
	}
}

Ternary operator

Overview of ternary operators
  • format
    (relation expression)? Expression 1: expression 2;
    If the condition is true, the result after the operation is expression 1;
    If the condition is false, the result of the operation is expression 2;
package com.itheima_06;
/*
 * Ternary operator:
 * 		Relation expression? Expression 1: expression 2;
 * 
 * Execution process:
 * 		A:Evaluate the value of the relationship expression to see whether the result is true or false
 * 		B:If true, expression 1 is the result
 *        If false, expression 2 is the result
 */
public class OperatorDemo {
	public static void main(String[] args) {
		//Define two variables
		int a = 10;
		int b = 20;
		
		int c = (a>b)?a:b;
		System.out.println("c:"+c);
	}
}
  • The comparison of three element operator exercises: whether two integers are the same
package com.itheima_06;
/*
 * Requirement: compare whether two integers are the same
 */
public class OperatorTest {
	public static void main(String[] args) {
		//Define two variables of type int
		int a = 10;
		//int b = 20;
		int b = 10;
		
		boolean flag = (a==b)?true:false;
		System.out.println(flag);
	}
}

Keyboard Entry

Basic steps of keyboard entry

At present, when we write the program, the data value is fixed, but in the actual development, the data value must be changed. Therefore, we should improve the data into keyboard input to improve the flexibility of the program.
To enter data by keyboard:

  • Guide Package (put the location above the class definition)
    import java.util.Scanner;
  • create object
    Scanner sc = new Scanner(System.in);
  • receive data
    int x = sc.nextInt();
Code sample
package com.itheima;
import java.util.Scanner;
/*
 * In order to improve the flexibility of the program, we change the data into keyboard input.
 * How to achieve keyboard data entry? Currently, the class Scanner provided by JDK is used.
 * How to use Scanner to get data? At present, you can remember the steps.
 * 
 * Use steps:
 * 		A:Guide bag
 * 			import java.util.Scanner;
 * 			Note: in a class, there is such a sequential relationship
 * 				package > import > class
 * 		B:Create keyboard entry object
 * 			Scanner sc = new Scanner(System.in);
 * 		C:get data
 * 			int i = sc.nextInt();
 */
public class ScannerDemo {
	public static void main(String[] args) {
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in);
		
		//Give hints
		System.out.println("Please enter an integer:");
		//get data
		int i = sc.nextInt();
		
		//Output the acquired data
		System.out.println("i:"+i);
	}
}
  • To find the sum of two integers in the data entry exercise of Scanner
package com.itheima;

import java.util.Scanner;
/*
 * Requirement: input two data by keyboard, sum the two data and output the result
 */
public class ScannerTest {
	public static void main(String[] args) {
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in);
		
		//Give hints
		System.out.println("Please enter the first integer:");
		int a = sc.nextInt();
		
		System.out.println("Please enter a second integer:");
		int b = sc.nextInt();
		
		//Sum data
		int sum = a + b;
		
		//Output result
		System.out.println("sum:"+sum);
	}
}

Select process control statement

Overview of selecting process control statements

• during the execution of a program, the execution order of each statement has a direct impact on the result of the program. In other words, the process of the program has a direct impact on the running results. Therefore, we must be clear about the execution process of each statement. Moreover, many times we need to control the execution order of statements to achieve the functions we need to complete.
• process control statement classification
Sequential structure
Selection structure
Cyclic structure

Sequential structure

Sequence structure overview

It is the simplest and most basic process control in a program. There is no specific syntax structure. It is executed in sequence according to the sequence of codes. Most codes in a program are executed in this way

Sequence structure flow chart

package com.itheima_01;

/*
 * Sequence structure: from top to bottom, execute successively
 */
public class OrderDemo {
	public static void main(String[] args) {
		System.out.println("start");
		System.out.println("Sentence A");
		System.out.println("Sentence B");
		System.out.println("Sentence C");
		System.out.println("End");
	}
}

Selection structure

if selection structure
Overview and use of if statement format 1
  • The first format of if statement:
    If (relation expression){
    Sentence body
    }
  • Execution process:
    First, judge whether the result of a relational expression is true or false
    Execute statement body if true
    If false, the statement body will not be executed
if statement format 1 Flowchart

Code sample
package com.itheima;
/*
 * if There are three formats for statements.
 * 
 * Format 1:
 * 		if(Relationship expression){
 * 			Sentence body;
 * 		}
 * 
 * Execution process:
 * 		A:First calculate the value of the relationship expression to see whether it is true or false
 * 		B:If true, the statement body is executed
 * 		C:If false, the statement body is not executed
 */
public class IfDemo {
	public static void main(String[] args) {
		System.out.println("start");
		
		//Define two variables of type int
		int a = 10;
		int b = 20;
		
		//Judge whether two variables are equal
		if(a == b) {
			System.out.println("a Be equal to b");
		}
		
		//Defining variables
		int c = 10;
		if(a == c) {
			System.out.println("a Be equal to c");
		}
		
		System.out.println("End");
	}
}
Overview and use of if statement format 2
  • The second format of if statement:
    If (relation expression){
    Statement body 1;
    }else {
    Statement body 2;
    }
  • Execution process
    First, judge whether the result of a relational expression is true or false
    Execute statement body 1 if true
    Execute statement body 2 if false
if statement format 2 Flowchart

Code sample
package com.itheima;
/*
 * Format 2:
 * 		if(Relationship expression){
 * 			Statement body 1;
 * 		}else {
 * 			Statement body 2;
 * 		}
 * 
 * Execution process:
 * 		A:First calculate the value of the relationship expression to see whether the result is true or false		
 * 		B:If true, execute statement body 1
 * 		C:If false, execute statement body 2
 */
public class IfDemo2 {
	public static void main(String[] args) {
		System.out.println("start");
		
		//Judge whether a data is odd or even
		//Idea: if the result of a data redundancy of 2 is 0, it means that the number is even
		//Define a variable
		int a = 100;
		//Reassign a
		a = 99;
		
		if(a%2 == 0) {
			System.out.println("a Even numbers");
		}else {
			System.out.println("a It's odd.");
		}
		
		System.out.println("End");
	}
}
  • Getting the larger value of two integers in the if statement exercise
package com.itheima;

import java.util.Scanner;

/*
 * Requirement: input two data by keyboard to obtain the larger value of the two data
 * 
 * Analysis:
 * 		A:When we see keyboard input, we should think of three steps of keyboard input
 * 			Guide package, create keyboard entry object, obtain data
 * 		B:Get the larger value of two data, and use the format 2 of if statement to realize judgment
 * 		C:Output large results
 * 
 * Guide Kit:
 * 		A:Manual guide pack
 * 		B:Click to generate automatically
 * 		C:Shortcut keys
 * 			ctrl+shift+o
 */
public class IfTest {
	public static void main(String[] args) {
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in);
		
		//Give hints
		System.out.println("Please enter the first integer:");
		int a = sc.nextInt();
		
		System.out.println("Please enter a second integer:");
		int b = sc.nextInt();
		
		//if format 2 implementation judgment
		/*
		if(a > b) {
			System.out.println("The larger value is: "+ a";
		}else {
			System.out.println("The larger value is: "+ B";
		}
		*/
		
		//If we get a larger value, it may not be the output, but there may be other operations, so we need to improve the code
		//Define a variable to hold larger values
		int max;
		
		if(a > b) {
			max = a;
		}else {
			max = b;
		}
		
		//I can do it.
		//max += 10;
		
		System.out.println("The larger values are:"+max);
	}
}

Overview and use of if statement format 3
  • The third format of if statement:
    If (relation expression 1){
    Statement body 1;
    }Else if (relation expression 2){
    Statement body 2;
    }
    ...
    else {
    Statement body n+1;
    }
  • Execution process
    First, judge whether the result of relational expression 1 is true or false
    Execute statement body 1 if true
    If it is false, continue to judge the relationship expression 2 to see whether the result is true or false
    Execute statement body 2 if true
    If it is false, continue to judge the relationship expression See if the result is true or false
    ...
    If no relation expression is true, execute statement body n+1
if statement format 3 execution flow chart

Code sample
package com.itheima;
/*
 * Format 3:
 * 		if(Relation expression 1){
 * 			Statement body 1;
 * 		}else if(Relation expression 2){
 * 			Statement body 2;
 * 		}
 * 		...
 * 		else {
 * 			Statement body n+1;
 * 		}
 * 
 * Execution process:
 * 		A:Evaluate the value of relationship expression 1 to see whether it is true or false
 * 		B:If true, execute statement body 1
 * 		C:If it is false, continue to calculate the value of relationship expression 2 to see whether it is true or false		
 * 		D:If true, execute statement body 2
 * 		E:If it is false, continue to calculate
 * 		F:The result of all relational expressions is false, executing statement body n+1
 */
public class IfDemo3 {
	public static void main(String[] args) {
		System.out.println("start");
		
		//If x and y satisfy the following relationship:
		//x>=3	y=2x+1
		//-1<=x<3	y=2x
		//x<-1	y=2x-1
		
		//Calculate the value of y according to the given value of x
		int x = 5;
		x = 0;
		x = -5;
		
		//Define variable y
		int y;
		
		if(x >= 3) {
			y = 2*x+1;
		}else if(x>=-1 && x<3) {
			y = 2*x;
		}else if(x<-1) {
			y = 2*x-1;
		}else {
			y = 0;
			System.out.println("There's no such thing x Value");
		}
		
		System.out.println("y:"+y);
		
		System.out.println("End");
	}
}
  • Output corresponding level according to student's score in if sentence exercise
package com.itheima;

import java.util.Scanner;

/*
 * Enter the test scores of the students by keyboard. Please judge the level of the students according to the scores
 * 90-100	excellent
 * 80-90	good
 * 70-80	good
 * 60-70	pass
 * 60 Below fail
 * 
 * Analysis:
 * A:The steps of inputting data by keyboard
 * B:Through simple analysis, we know that the format 3 of if statement should be used for judgment
 * 		According to the judgment, directly output the corresponding level
 * 
 * When writing a program, you should test the data in the following situations:
 * 		Correct data
 * 		Boundary data
 * 		Erroneous data
 */
public class IfTest2 {
	public static void main(String[] args) {
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in);
		
		//Give me a hint.
		System.out.println("Please enter the student's test scores:");
		int score = sc.nextInt();
		
		//if statement format 3 Implementation
		/*
		if(score>=90 && score<=100) {
			System.out.println("Excellent "";
		}else if(score>=80 && score<90) {
			System.out.println(""Good".
		}else if(score>=70 && score<80) {
			System.out.println("Liang ";
		}else if(score>=60 && score<70) {
			System.out.println("Pass "";
		}else {
			System.out.println("Fail "";
		}
		*/
		
		//Through the test of data, we found that the procedure was not rigorous enough, and the judgment of illegal data was not added
		if(score>100 || score<0) {
			System.out.println("The score you entered is wrong");
		}else if(score>=90 && score<=100) {
			System.out.println("excellent");
		}else if(score>=80 && score<90) {
			System.out.println("good");
		}else if(score>=70 && score<80) {
			System.out.println("good");
		}else if(score>=60 && score<70) {
			System.out.println("pass");
		}else {
			System.out.println("Fail,");
		}
	}
}

switch Statements

An overview of switch statement
  • switch statement format:
    Switch (expression){
    case value 1:
    Statement body 1;
    break;
    case value 2:
    Statement body 2;
    break;
    case value 3:
    Statement body 3;
    break;
    ...
    default:
    Statement body n+1;
    break;
    }
  • Switch means this is a switch statement
  • Value of expression: byte,short,int,char
    JDK5 can be enumeration later
    JDK7 can be String later
  • case is followed by the value to be compared with the expression
    The body part of a statement can be one or more statements
  • Break means break or end. You can end the switch statement
    The default statement means that when all conditions do not match, the content of the statement is executed, similar to the else of the if statement.
  • Execution process
    First calculate the value of the expression
    Secondly, compare with case in turn. Once there is a corresponding value, the corresponding statement will be executed. In the process of execution, the break will end.
    Finally, if all case s don't match the value of the expression, the body part of the default statement is executed and the program ends.
switch statement flowchart

Code sample
  • The case of the switch statement corresponds to the week according to the digital output
package com.itheima;

import java.util.Scanner;

/*
 * switch Statement format:
 * 		switch(Expression) {
 * 			case Value 1:
 * 				Statement body 1;
 * 				break;
 * 			case Value 2:
 * 				Statement body 2;
 * 				break;
 * 			case Value 3:
 * 				Statement body 3;
 * 				break;
 * 			...
 * 			default:
 * 				Statement body n+1;
 * 				break;
 * 		}
 * 
 * Format explanation:
 * 		Expression: byte,short,int,char
 * 			JDK5 It can be enumeration later, JDK7 can be string later
 * 		case Later value: used to match the value of the expression
 * 		break: Means to interrupt
 * 		default: If all values do not match the expression, execute the corresponding content of default
 * 
 * Execution process:
 * 		A:Evaluate the value of an expression
 * 		B:Take this value and compare it with the value after case in turn. Once there is a match, execute the corresponding statement. In the process of execution, the break will end.
 * 		C:If all case s do not match, execute statement body n+1
 * 
 * Case study:
 * 		Output the corresponding Monday to Sunday according to the data 1-7 entered by the keyboard
 * 
 * Shortcuts: formatting code
 * 		ctrl+shift+f
 */
public class SwitchDemo {
	public static void main(String[] args) {
		// Create keyboard entry data
		Scanner sc = new Scanner(System.in);

		// Give hints
		System.out.println("Please enter an integer(1-7): ");
		int weekDay = sc.nextInt();

		// Using switch statement to realize judgment
		switch (weekDay) {
		case 1:
			System.out.println("Monday");
			break;
		case 2:
			System.out.println("Tuesday");
			break;
		case 3:
			System.out.println("Wednesday");
			break;
		case 4:
			System.out.println("Thursday");
			break;
		case 5:
			System.out.println("Friday");
			break;
		case 6:
			System.out.println("Saturday");
			break;
		case 7:
			System.out.println("Sunday");
			break;
		default:
			System.out.println("The data you entered is wrong");
			break;
		}
	}
}
Published 30 original articles, won praise 0, visited 675
Private letter follow

Keywords: Java JDK

Added by David-fethiye on Sat, 14 Mar 2020 11:58:46 +0200