day02--Java basic programming: variables, data types, type conversion, operation rules, Scanner, operators, branch structures, loops (random numbers), methods, overloading, variable parameters, recursion, arrays, bubble sort

1 Day02–Variable + data type + type conversion

1.1 Preface

1.1.1 Identifier

It can be simply understood as a name. In Java, we need to identify many elements of the code, including package names, class names, methods, fields, variables, etc. The name we choose is called an identifier and follows these rules :

  • A. Identifiers can be composed of letters, numbers, underscores (_), dollar signs ($), but cannot contain other special characters such as @, %, spaces, etc.
  • B. Cannot start with a number.
  • C.Identifiers are strictly case-sensitive.
  • D. It is best to name the identifier to reflect its function and make it easy to understand the name in English.
  • E. You can write Chinese names, but it is generally not recommended, nor is it recommended to use Pinyin, which is very low.

1.1.2 Keywords

Some words that have been given specific meanings in the Java language . There are 53 keywords in total. There are two reserved words: const (constant, unchanged) and goto (go to). Keywords cannot be used as identifiers! !
Reserved words : Not used at the moment but may be used in the future.
All letters in keywords are lowercase .

There are many keywords in Java, so there is no need to force memorization, because if you use keywords as identifiers when compiling the program, the compiler will remind you of an error.

Insert image description here

1.1.3 Comments

1) Three annotation methods in java specification

//  单行注释               (快捷键:ctrl+/, 取消注释:重复使用一次)
/*  多行注释  */            (快捷键:ctrl+shift+/),取消注释:重复使用一次
/** 文档注释(java特有)  */  (快捷键:/**+enter)

使用位置:
类上,类的属性上,类的方法上-----推荐使用文档注释
方法内部----推荐使用单行、多行注释

2) Single-line comments and multi-line comments
2.1) Function:

  1. Explanation of Java code. It does not affect the running of the program and is used to assist in reading the program.
  2. Debugging the code you wrote

2.2) Features:
Single-line comments and multi-line comments, the commented content will not participate in compilation.
In other words, the bytecode file ending with .class generated after compilation does not contain the commented out information.

2.3) Precautions:Multi-line comments cannot be nested. Although other comments can be nested, they have no meaning.

3). Document comments
3.1) Function: The annotation content can be parsed by the tool javadoc provided by the JDK to generate a set of documentation for the program in the form of web page files.
3.2) Case:
Insert image description here
Generate your own API documentation : through the command line or through development tools such as idea, check Baidu for details.
Insert image description here
Insert image description here
Insert image description here
Orderjavadoc 参数 文件
Insert image description here
Insert image description here
Insert image description here

1.1.4 Shortcut keys in eclipse

View the source code, ctrl+mouse click.
syso+alt+/: Output statement shortcut key
ctrl +/: Select multiple lines of code and quickly add a single line comment. (Cancel, use the shortcut key again)
Ctrl+shift+o: Shortcut key for importing packages
Ctrl +d: Shortcut key for deleting one line at a time (in the code block to be deleted, use the shortcut key to delete a line)
Ctrl +n shortcut key Equivalent to new using the up and down keys (creating projects, packages, and classes without using the mouse)
Alt+up and down arrows (moving the code up and down)

1.1.5 Escape characters in java

Escape characters : used to represent special symbols or special meanings.
\b: means going back one character
\t: means a tab stop
\n: means switching to the next line
\r: carriage return

\": represents double quotation mark
\': represents single quotation mark
\: represents backslash character\

1.1.6 Output statements in java

System,out.print();   //输出不换行
System,out.println();   //换行
System,out.println(“ ”);//输出一个空格
System,out.println(“\t”); //对齐:输出的数加上空格一共8位。也可以改为(“     “)的形式。

1.1.7 Variables (default)

Example :
Room--------- Variable
Room name--------- Variable name
Room type-------- Variable type
Guests staying---------- Variable value

Concept : In JAVA, some data values ​​are not fixed and always changing. We also need to record these values. We can understand these values ​​as variables.

The essence of variables : It is actually a part of the space in the memory. When creating a variable, it means applying for a part of the memory space from the operating system. Declaring different types of variables essentially means applying for different sizes of memory space; (the first step in the execution of the program One step is to load the memory into the memory, and then follow the relationship between the execution of the program and the hardware)

Principles for using variables: Based on the principle of proximity, try to control it to the smallest scope.

Declaration of variables : variable type variable name = variable value .
= : Assignment number, the data on the right is assigned to the variable on the left.

int num = 18;   //声明的同时赋值

等价于:

int num;  //先声变量
num = 20;//在进行赋值

Note :

  1. The variable name must be a valid identifier.
  2. Variable names cannot use the java keyword.
  3. Within the same scope, variable names cannot be repeated .
  4. Punctuation marks in java are in English.
  5. Variables end each sentence with a semicolon.

Rules for using variables in Java :

  1. Variables in java need to be declared before use . (Local variables do not have default values. If you want to use them for output, they must be declared and assigned a value before they can be used. Member variables have default values. If you want to output them, they can be output without assigning values.)
  2. When using a variable, you can initialize it while declaring it (initialization: first assignment), or you can declare it first and then assign it .
  3. Only one value can be assigned to a variable at a time, but it can be modified multiple times.
  4. Variables defined in the Main method must be assigned a value before they can be output.
  5. Although there are no errors in the syntax, in actual development, it is not recommended to use Chinese for variable names, which may easily cause security risks, such as garbled characters in later cross-platform operations.
  6. The use of variables must match the data type.
  7. Variables are defined within their scope. Within the scope, it is valid. In other words, once it goes out of scope, it becomes invalid.
  8. variableScopeScope: from the declaration of the variable toContains its nearest { }The program ends.
  9. Variable naming rules: camel case "score, myScore, myJavaScore (the first letter of the second word is capitalized).

Variable classification :

  1. Divided according to the declared position : 成员变量(outside the method in the class) and 局部变量(inside the method). Member variables are divided into: 实例变量and 静态变量(add Static keyword)
  2. Divided by data type : 基本数据类型and 引用数据类型(类,接口,数组).

Local variables :

  1. Defined in a method (including method parameters), or in a local code block
  2. Must be manually initialized (assigned initial value) to allocate memory. Such as: int i=5;
  3. The scope is within a method or a local code block. The memory is released after the method is executed.

Member variables :

  1. Defined outside the method in the class.
  2. No initialization is required and it will be automatically initialized to the default value.
  3. The scope is within the entire class. When the class disappears, the variables are released.

Default value of member variable :

1). Reference type (except for the 8 basic types) : the default value is null
2). Basic type :
among them char: /u0000 (default value, generally not displayed. The code of the whitespace character is 0x20, and the code below 0x20 is an invisible control character. Certain Some systems will output a box when encountering invisible characters to indicate that invisible characters are encountered)

basic type default value
byte 0
shot 0
int 0
long 0L
float 0.0f
double 0.0d
boolean false

1.1.8 Constants

Constants learned in day03 - 3.3 --final.

Overview : A quantity that never changes while the program is running becomes a constant.

The definition form of constants : final 数据类型 常量名 = 值;, generally in order to facilitate external calls, they will be modified by static and can be directly accessed by the class name. The final form is: public static final 数据类型 常量名 = 值;.

Constant naming convention :It is recommended that all letters in constant names be capitalized, and multiple words separated by underscores ("_") (recommended but not mandatory), such as: MAX_VALUE

Notes on constants:

  1. It must be declared to be initialized at the same time (the initial value must be assigned directly and cannot be modified), (assignment in a static block is also possible, but not recommended)
  2. It is accessed through the class name point and cannot be changed.

Advantages of constants : Constants are directly replaced with specific values ​​at compile time - high efficiency

When are constants used ? Data never changes and is used frequently.

1.1.9 Variable test case

//测试一:
package cn.tedu.variable;
 
		import org.junit.Test;
 
		//测试 变量的使用
		public class Test6_Variable {
    
    
			//TODO 测试其他类型 的默认值
			
			//2,成员变量:位置是在类里方法外  +  有默认值 + 作用范围是整个类里
			double count ;
			
			//3,就近原则---前提是---当成员变量 和 局部变量同名时,你使用的一定是局部变量
			int sum = 20 ;
			
			//单元测试junit :要求方法要有标志@Test + 方法必须是public的 + 方法返回值是void,没有参数列表
			@Test    //需要导包(1 2 都可以)
			public void show() {
    
    
				//1,局部变量:位置是在方法里 + 必须初始化  + 作用范围是方法里
				int sum = 10;
				System.out.println(sum);//10,就近原则
				System.out.println(count);//0.0
			}
			
			
		}
 
 
 
 
//测试二: 
package cn.tedu.arrays;
 
public class Test2_Variable {
    
    
	
	//1、成员变量:在类里方法外
	//作用范围就是整个类里
	//可以不初始化,也会有默认值
	int age = 20;
	
	int sum = 30;
	
	public static void main(String[] args) {
    
    
		//2局部变量:在方法里
		//作用范围就是方法里,出了这个方法就不认识了
		//必须初始化
		int sum = 10;
		System.out.println(sum);//10
		
		//3、变量的就近原则,附近有同名的会去执行最近的
		System.out.println(sum);//10
	}
}

1.1.10 Points to note about member variables

Note: Member variables cannot be declared first and then assigned.

Explanation: Member variables are within the scope of the class and exist in the heap memory. They will have default initial values ​​and have been assigned values ​​when they are declared. Again, "assignment" actually uses statements, and only variables and methods can appear in the class body, not statements.

package com.cn.ins;

public class Aa {
    
    
	
	private int age = 10;
	int b;
	b= 20;//报错:成员变量不能先声明后赋值。
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub

	}

}

1.2 Data types

Strongly typed language : requires the use of variables to strictly comply with regulations, and all variables must be defined before use. Such as: Java , .net, Python, C++ and other languages.

Weakly typed language : A language in which data types can be ignored. It is the opposite of a strongly typed definition language, in which a variable can be assigned values ​​of different data types. Such as: vb, PHP, javascript and other languages.

Java data types are divided into 2 major categories :

  1. Basic data types:Variables store the data itself. (equivalent to the contents of a drawer)
  2. Reference data type: (except basic types, all reference types, such as classes, interfaces, arrays) variables are stored inThe space address where the data is saved. (equivalent to a drawer key)

1.2.1 Basic types (eight types)

Sort from small to large : byte short (char) int long float double
Explanation : Boolean type does not participate in sorting, char will be automatically converted to int.
Glossary : ​​What is a byte.
Insert image description here

Insert image description here

1.2.2 Exercise 1: Maximum and minimum values

package day0102;
//总结
//1,基本类型:byte short int    long   float  double char   boolean
//2,工具类:Byte Short Integer  Long   Float  Double Character Boolean       
//3,工具类提供了很多功能,其中就包含了大小值   
//基本类型对应的包装类提供了一些功能,最大值,最小值 没有括号不是方法
//注意包装类首字母大写,尤其2个特殊
 
public class Test1 {
    
    
	public static void main(String[] args){
    
    
		
		//=====1、整型测试开始
        //变量类型  变量名 = 变量值
		byte a=-128;   打印变量的值,不需要" "
		byte b=127;
		
		short c=Short.MIN_VALUE;
		short d=Short.MAX_VALUE;
		
		int e=Integer.MIN_VALUE;
		int f=Integer.MAX_VALUE;
		
		long g=Long.MIN_VALUE;
		long h=Long.MAX_VALUE;
		
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
		System.out.println(f);
		System.out.println(g);
		System.out.println(h);
		//=====整型测试结束
		
		//=====2、浮点测试开始
		/**
		 *float是单精度,对小数运算不精确。double是双精度,对小数运算精确。
		 *对于编程人员来说,double和float的区别是double精度高,但double消耗内存是float的两倍。
		 *且double的运算速度较float稍慢。
		 */
		float i=Float.MIN_VALUE; //1.4E-45(代表10的负45次方)
		float j=Float.MAX_VALUE; //3.4028235E38(代表10的38次方)
 
		double k=Double.MIN_VALUE; //4.9E-324
		double l=Double.MAX_VALUE; //1.7976931348623157E308
		System.out.println(i);
		System.out.println(j);
		System.out.println(k);
		System.out.println(l);
		//=====浮点测试结束
		
 
       //=====3、字符测试开始
       /**
       *字符char    单引号
       *字符串String(S大写)   双引号
       */ 
       
       //① 定义char型变量,通常使用一对'',内部只能写一个字符   
       char c  = 'a';   //可以存一个字母或者数字或者符号不能没有,可以是空格
       char c4 ='1';   //数字也只能存一个。
       char c2 = 97;   //可以直接存数字(可以写多个数字)
       char c3 = '中'; //可以存一个中文汉字
      
       //char类型可以存一个数字,但是在使用时,并不是使用了数字本身,而是去查数字对应的字符,然后使用字符。
       //ascii码表,规定了数字和字符的对应关系。其中数字97就是对应了字符a。 97----a    65---A    48---0
       //ascii码表里,规定了0~127数字对应的字符,char类型的取值
	   //范围是0~65535.那么从128~65535默认对应的字符是?.
       System.out.println(c);
       System.out.println(c2);//a
       System.out.println(c3);
       
	   //② 表示方式:1.声明一个字符 2.转义字符 3.直接使用 Unicode 值来表示字符型常量
		char c5 = '\n';//换行符
		c5 = '\t';//制表符
		System.out.print("hello" + c5);
		System.out.println("world");

		char c6 = '\u0043';
		System.out.println(c6);
      
      
       //=====4、布尔
 	   //① 只能取两个值之一:true 、 false
	   //② 常常在条件判断、循环结构中使用
       boolean b = true;
       boolean b2 = false;
		
	}
}

//long start = System.currentTimeMillis()用于获取自1970.1.1零时到此时此刻的毫秒数,返回值类型为long类型·

1.2.3 Exercise 2: Enter personal information

Wang Haitao is 20 years old this year, with a monthly salary of 20,000. He advises everyone to study Java hard so that they can reach the peak of life as soon as possible.

package day0201_规则;

import java.util.Scanner;
 
public class Test1_个人信息 {
    
    
 
	public static void main(String[] args) {
    
    
	
		System.out.println("姓名:");
		String name= new Scanner(System.in).nextLine();
		//注意:String对应的是Line
		System.out.println("性別:");
		String gender= new Scanner(System.in).nextLine();
		
		System.out.println("年龄:");
        //其它的都是首字母大写
		int age= new Scanner(System.in).nextInt();
		
		
		System.out.println("您输入的个人信息是:");
		System.out.println("姓名:"+name);
		System.out.println("性別:"+gender);
		System.out.println("年龄:"+age);
		
	}
	
}

1.2.4 Exercise 3: Use of String type variables

/*
String类型变量的使用
1. String属于引用数据类型,翻译为:字符串
2. 声明 String类型变量时,使用一对""
3. String可以和 8种基本数据类型变量做运算,且运算只能是连接运算:+
4. 运算的结果仍然是String类型

*/
class StringTest {
    
    
	public static void main(String[] args) {
    
    
		
		String s1 = "Hello World!";

		System.out.println(s1);

		String s2 = "a";
		String s3 = "";

		//char c = '';//编译不通过

		//***********************
		int number = 1001;
		String numberStr = "学号:";
		String info = numberStr + number;// +:连接运算
		boolean b1 = true;
		String info1 = info + b1;// +:连接运算
		System.out.println(info1);

		//***********************
		//练习1
		char c = 'a';//97   A:65
		int num = 10;
		String str = "hello";
		System.out.println(c + num + str);//107hello
		System.out.println(c + str + num);//ahello10
		System.out.println(c + (num + str));//a10hello
		System.out.println((c + num) + str);//107hello
		System.out.println(str + num + c);//hello10a

		//练习2
		//*	*
		System.out.println("*	*");
		System.out.println('*' + '\t' + '*');
		System.out.println('*' + "\t" + '*');
		System.out.println('*' + '\t' + "*");
		System.out.println('*' + ('\t' + "*"));


		//***********************

		//String str1 = 123;//编译不通过
		String str1 = 123 + "";
		System.out.println(str1);//"123"
		
		//数字类型不能和字符串类型进行 自动和强制类型转换
		//int num1 = str1;
		//int num1 = (int)str1;//"123"
        
        //基本类型和引用类型,可以使用包装类进行转换
		int num1 = Integer.parseInt(str1);
		System.out.println(num1);//123
	}
}

1.2.5 Exercise 4: String splicing process.

Hello everyone, my name is Zha Zhahui and I am 28 years old.

package cn.tedu.basic;
//这个类用来测试字符串拼接
public class Test2_Info {
    
    
    public static void main(String[] args) {
    
    
      
       //值是一串,Java里表示一串数据,就是String类型。
       String name = "蔡徐坤";
      
       byte age = 28;
      
       //通过+拼接字符串,  "+?+"
       System.out.println("大家好,我叫"+name+",今年"+age+"。");
      
    }
}
 

1.2.6 Exercise 5: Area of ​​a Circle

Area of ​​a circle: π*r*r
Circumference of a circle:2*π*r

package day0104;
 
import java.util.Scanner;
 
public class Test1 {
    
    
 
	public static void main(String[] args) {
    
    
		  //定义变量,记录半径r
//           double r = 10.1;//不好,值写死了。
              //动态的接受键盘输入的值
              //测试时:console试图中小红点常亮,表示等待用户的输入,输入完毕后敲回车,表示输入结束。
              double r = new Scanner(System.in).nextDouble();
             
              double result = 3.14*r*r;//带入公式
              System.out.println(result);
             
//或者    System.out.println(3.14*r*r);
             
       }
      
}

1.2.7 Exercise 6: Variable exchange (3 types)

Insert image description here

Receive the value entered by the user: Assume a=1, b=2 and exchange the values ​​​​of a and b.

package day0103;
 
import java.util.Scanner;
 
 
public class Test1 {
    
    
 
	public static void main(String[] args) {
    
    
	案例1//1,接受键盘输入整数a和整数b
		System.out.print("输入整数a:");
		int a=new Scanner(System.in).nextInt();
		//前后对照
		System.out.print("输入整数b:");
		int b=new Scanner(System.in).nextInt();
		//第一种方式:
		//2.开始交换
		int t=a;   //把a的值给t     首尾相连
		a=b;      //把b的值给a
		b=t;      //把t的值给b                                                                                                                                                                                                                                                                                       
		System.out.println(a);
		System.out.println(b);
	}
       //第二种方式:a=a+b; b=a-b;a=a-b 另一种2个数进行交换
 
}



案例2//练习:交换两个变量的值
		int num1 = 10;
		int num2 = 20;
		System.out.println("num1 = " + num1 + ",num2 = " + num2); //num1 = 10,num2 = 20

		//方式一:定义临时变量的方式
		//推荐的方式
		int temp = num1;
		num1 = num2;
		num2 = temp;

		//方式二:好处:不用定义临时变量  
		//弊端:① 相加操作可能超出存储范围 ② 有局限性:只能适用于数值类型
		//num1 = num1 + num2;
		//num2 = num1 - num2;
		//num1 = num1 - num2;

		//方式三:使用位运算符
		//有局限性:只能适用于数值类型
		//num1 = num1 ^ num2;
		//num2 = num1 ^ num2;
		//num1 = num1 ^ num2;

		System.out.println("num1 = " + num1 + ",num2 = " + num2);//num1 = 20,num2 = 10 

1.3 Default literal type of basic types (5 items)

1.3.1 The integer literal type (default value) is int type

int a = 9999999999;//错,右侧是int类型,但是超出范围

1.3.2 byte, short, and char integers smaller than int can be directly assigned using values ​​within the range.

byte b=127;//对
byte b=128;//错,右面已经超过byte范围是int类型的数据
超过范围的相加 加2次最大值等于它本身(所有的基本类型)

1.3.3 The literal value of floating point number is double type

double  a=3.14;//对
float a=3.14;//错,右面是double,float是四字节double是八字节存不下

1.3.4 Literal value suffix lfd

//因为它默认的是int类型虽然可以自动转,但是它超出本身的范围了。所以加上一个L 表示long 类型。
//这个整数默认是int类型,但是超出了int范围,还想使用怎么办?
//加后缀L,用来把字面值是int类型的数据,提升成long类型
Llong   如:long a = 99999999999;//错,超出范围,解决方案加L

//后缀f,用来把默认的double类型的小数,转成float类型
Ffloat   如:float a = 3.14;//错,右面是double类型,解决方案加F或者(float)

//后缀d,用来把字面值是int类型的3,转成double类型
Ddouble  如:double a=3;//错,右面是int,解决方案加D或者改成3.0

//后缀一般默认是大写,因为小写有时候想数字1不好区分。
注意不加也不报错,因为3double类型的取值范围之内。

1.3.5 Base prefix

0x   - 16进制
0    -8进制
\u   -char类型,16进制
0b   -2进制  0b0011

1.4 Type conversion of basic types

Notes :
1. Boolean values ​​cannot be converted. (The char type is OK because its corresponding encoding table is a number.)
2. The object type cannot be converted to an unrelated type.
3. Forced type conversion is required when converting high capacity to low capacity.
4. Small type to large type will be converted automatically.
5. Memory overflow or accuracy issues may occur during conversion.

1.4.1 Small to large (implicit conversion)

No cast required

//自动类型转换
Byte a =120;
Int b=a;//直接转    
整形可以转换为浮点型
char可以直接转化为int类型

Insert image description here

1.4.2 Large to small (explicit conversion)

Requires type conversion

//强制类型转换
int xx = 356;
byte y=(byte) xx;

//内存溢出问题
int i=128;
byte b=(byte)i;//-128 内存溢出,byte范围为-128~127


//精度问题:小数转成整数,小数直接舍弃。
 double y = 9.1;
 int x = (int)y;//右侧的y是大类型,给小类型x赋值时,需要强转、
 System.out.println(x);//9,丢弃小数部分
 //不管0.1还是0.9全都舍弃


//char类型的转换
char c='a'; //97
int d=c+1;
System.out.println(d);//98
System.out.println((char)d);//b

1.5 Operation rules (5 items)

1.5.1 The calculation result is the data type, which is consistent with the largest type

3/21 ,而不是1.5,结果是int类型
3d/21.5,相当于double/int,结果是double类型

1.5.2 byte, short, and char are three integers smaller than int. They will be automatically converted into int first during operation.

byte a=3;
byte b=4;
byte c=a+b;//错,运行时,byte会先自动转成int再运算,int+int还是int
byte c = (byte)(a+b);
//右侧的a+b运算时,会自动变成int类型,是大类型。给左面的c小类型赋值??--不能直接赋值需要强转

1.5.3 Integer operation overflow

Integer arithmetic is similar to a clock that goes to the maximum and then returns to the minimum.
Note : When operating relatively large numbers, pay attention to the overflow problem.
Solution : Expand the variable value range in advance, add the L suffix to the int literal value to expand it into a long type, usually add L after the first number.


//jdk1.7新特性:数字之间可以用下划线分割,输出时不会影响。
int money=10_0000_0000;
int years=20;
int total=money*years; //内存溢出 -1474836480
long total2=money*years; //内存溢出,计算完后默认是int类型,转换为long之前就出现问题了。 -1474836480		
long total3=(long)money*years;//20000000000
System.out.println(total);
System.out.println(total2);
System.out.println(total3);

//计算:光速运行一年的长度是多少米
System.out.println(300000000*60*60*24*365);
System.out.println(300000000l*60*60*24*365);
//运算结果,字面值就是int类型。但是,已经超出了int取值范围,就会整数运算溢出现象
 //解决方案:把int字面值加L后缀扩大,变成long类型、一般是第一个数后加L。

1.5.4 Floating point operations are inexact

Reason : The characteristics of floating-point numbers are limited and discrete, and there will be rounding errors. The result can only be a divisor, which is close but not equal to it.
Solution : Java provides a solution, which will be discussed later, BigDecimal .

System.out.println(1-0.8);//0.19999999999999996
System.out.println(4.35*100);//434.99999999999994

//最好完全避免使用浮点数进行比较
float f= 0.1f;//0.1
double d=1.0/10;//0.1
System.out.println(f==d);//false

1.5.5 Special values ​​of floating point numbers

Infinity 无穷大  3.14/0  
Nan  not a number  0/0.0
 
//输出结果不能出现这种情况,一旦出现检查代码(分母为0或分子分母都为0)

1.6 Expansion

1.6.1 Can a Chinese character be stored in a char variable? Why?

The char type can store a Chinese character, because the encoding used in Java is Unicode (do not choose any specific encoding, directly use the number of the character in the character set, this is the only way to unify), and a char type occupies 2 bytes ( 16 bits), so putting a Chinese one is no problem.

1.6.2 User interaction Scanner

Note :

  1. String corresponds to Line, and everything else has the first letter capitalized.
  2. For obtaining char type, Scanner does not provide related methods. Only one string can be obtained. If you must use the char type to receive, you can call the gender.charAt(0) method.
import java.util.Scanner;

/*
如何从键盘获取不同类型的变量:需要使用Scanner类

具体实现步骤:
1.导包:import java.util.Scanner;
2.Scanner的实例化:Scanner scan = new Scanner(System.in);可以写一块
3.调用Scanner类的相关方法(next() / nextXxx()),来获取指定类型的变量

注意:
需要根据相应的方法,来输入指定类型的值。如果输入的数据类型与要求的类型不匹配时,会报异常:InputMisMatchException
导致程序终止。
*/ 
public class Test1_个人信息 {
    
    
 
	public static void main(String[] args) {
    
    
		System.out.println("姓名:");
		String name= new Scanner(System.in).nextLine();//写一块的写法
		//注意:String对应的是Line
		System.out.println("性別:");
		String gender= new Scanner(System.in).nextLine();
		
		System.out.println("年龄:");
        //其它的都是首字母大写
		int age= new Scanner(System.in).nextInt();
		
		
		System.out.println("您输入的个人信息是:");
		System.out.println("姓名:"+name);
		System.out.println("性別:"+gender);
		System.out.println("年龄:"+age);


		//对于char型的获取,Scanner没有提供相关的方法。只能获取一个字符串
		System.out.println("请输入你的性别:(男/女)");
		String gender = scan.next();//"男"
		char genderChar = gender.charAt(0);//获取索引为0位置上的字符
		System.out.println(genderChar);
		
	}
	
}

Insert image description here
The output is: hello
Insert image description here
Output: hello world
Insert image description here
Reason :
Insert image description here
If it is a number, use the corresponding nextxxx() method to judge:

package lianxi;

import java.util.Scanner;

public class Ta {
    
    

	public static void main(String[] args) {
    
    
		Scanner scanner =new Scanner(System.in);
		
		//从键盘接收数据
		int i=0;
		float f=0.0f;
		System.out.println("请输入整数:");
		if(scanner.hasNextInt()) {
    
    
		/*
		* 如果什么也不写默认为true(老手写法)
		* if(scanner.hasNextInt()==true) { 新手写法
		*/
		
			i= scanner.nextInt();
			System.out.println("整数数据"+i);
			
		}else {
    
    
			System.out.println("输入的不是整数数据");
			
		}
		
		scanner.close();
	}

}

2 Day03–Operator + branch structure + loop

2.1 Operators

2.1.1 Overview: Common operators

Insert image description here

2.1.2 Bit operators

In addition to the common operators, there are also bitwise operators:
Insert image description here
How to distinguish &,|,^ when it is a logical operator and when it is a bitwise operator???
When a symbol is flanked by logical operators and the result is of type Boolean, it is a logical operator. If the sign is surrounded by integers and the result is an integer, it is a bitwise operator.

Case 1 :

/*
		 A = 0011 1100
		 B = 0000 1101
		 ------------------
		 A&B = 0000 1100   上下2个都是1才为1,否则为0
		 A/B = 0011 1101   上下2个都是0才为0,否则为1
		 A^b = 0011 0001   上下2个位置相同则为0,否则则为1.
		 ~B  = 1111 0010  取反
		 
		 2*8 = 16 2*2*2*2
		 效率极高!!!
		 << *2 左移扩大
		 >> /2 右移缩小
		 0000 0000  0
		 0000 0001  1
		 0000 0010  2
		 0000 0011  3
		 0000 0100  4
		 0000 1000  8
		 0001 0000  16
		 */
		System.out.println(2<<3);//16

Case 2:

package lianxi;

import java.util.Arrays;

/*
运算符之五:位运算符 (了解)

结论:
1. 位运算符操作的都是整型的数据
2. << :在一定范围内,每向左移1位,相当于 * 2
   >> :在一定范围内,每向右移1位,相当于 / 2



面试题:最高效方式的计算2 * 8 ?  2 << 3  或 8 << 1
*/
class BitTest {
    
    
	public static void main(String[] args) {
    
    
		int i = 21;
		i = -21;
		System.out.println("i << 2 :" + (i << 2));//i << 2 :-84  不改变符号
		System.out.println("i << 3 :" + (i << 3));//i << 3 :-168
		System.out.println("i << 27 :" + (i << 27));//i << 27 :1476395008

		int m = 12;
		int n = 5;
		System.out.println("m & n :" + (m & n));//m & n :4
		System.out.println("m | n :" + (m | n));//m | n :13
		System.out.println("m ^ n :" + (m ^ n));//m ^ n :9

		

	}
}

2.2 Operator precedence

Formula: Single-term multiplication and division are relations, and values ​​are assigned after three-term logic.

单目运算符:一次作用一个变量的运算符,又叫一元运算符
单目:单目运算符+(正负数)++ –,!(逻辑非),~(按位取反)
乘除:算数运算符:* / % + -* / %优先级肯定是大于+-的)
为:位运算符:~(按位取反)<<(左移) >>(右移)^(也可以位运算,二进制异或)
关系:关系运算符:> < >= <= == !=
逻辑:逻辑运算符(除!)&& || & | ^
三目:条件运算符A > B ? X : Y
后:无意义,仅仅为了凑字数
赋值:= += -= *= /= %= |= &= ^=
说明:前优先级大于后,比如单目运算符~也是位运算符,~的优先级是单目级别的。至于赋值运算符中没有见过的自行测试

Insert image description here
PS: There is no need to memorize the priority order of operators. In actual development, parentheses are generally used to assist in priority management. like:
Insert image description here

2.3 Case exercises

2.3.1 Exercise 1: Leap Year in Ordinary Years

输入年号,判断是否是闰年。两个条件:
1、能被4整除,并且不能被100整除
2、或者能被400整除
 
package day0203_平年闰年;
 
import java.util.Scanner;
 
public class Test1 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("年号:");
		int y = new Scanner(System.in).nextInt(); //1,接受用户输入的年号
 
		
		String r="平年";	//默认都是平年
        //2,判断,这个年号到底是平年还是闰年
 
		if(y%4==0){
    
    
			if(y%100!=0){
    
    
				r="闰年";
			}
		}
		if(y%400==0){
    
    
			r="闰年";
		}
 
 
或者
	   //说明:
       //两个大条件之间是或者的关系,为了高效,用双或
       //大条件1有两个小条件,两个小条件之间是并且的关系,为了高效,用双与
       //能否被整除,要取余数,余数为0就是能整除。余数不是0就是不能整除。
       if( ( year % 4 == 0  &&  year % 100 != 0  ) || year % 400 == 0){
    
    
          
           //如果年号,能满足判断条件,会进来修改r的默认值为闰年。如果没满足,就一直是默认值平年
           r = "闰年";//修改r的值为闰年
 
 
         if((y%4==0&&y%100!=0)||y%400==0){
    
    
			r="闰年";
		  }
 
		System.out.println(y+"年是"+r);
	}
}
 

2.3.2 Exercise 2: Self-increasing and self-decreasing

Insert image description here

package com.jia.jian;

public class JiaJian {
    
    

	public static void main(String[] args) {
    
    
		   //++,--代表自身加一和减一,运算结果与符号顺序有关。
		   //情况1:单独使用时,++ --无论放在变量前还是变量后,结果都是一样的
			int number1 = 10;
			//输出方式一: 输出number1++	
			//System.out.println(number1++);//10
			//输出方式二: 输出number1
			number1++;
			System.out.println(number1);//11
			//总结执行过程:符号在后,先使用后变化(先使用:number1++变为10,后变化:number1自身变为11),
			//所以输出number1++为10,输出number1为11.
		
			int number2 = 10;
			//输出方式一:输出++number2
			//System.out.println(++number2);//11
			//输出方式二:输出number2
			++number2;
			System.out.println(number2);//11
			//总结执行过程:符号在前,先变化后使用(先变化:++number2变为11,后使用:number2自身变为11),
			//所以输出number2++为11,输出number2为11.
			
			int number3 = 10;
			number3--;
			System.out.println(number3);//9
			
			int number4 = 10;
			--number4;
			System.out.println(number4);//9
			
			
			//情况2:参与操作时,符号在后:先使用后变化
			//执行过程:符号在后,先执行运算(先把10赋值给num2),后变化(num1++,num1变为11)
			int num1 = 10;
			int num2 = num1++;
			System.out.println("num1:"+num1+",num2:"+num2);//num1:11,num2:10
			
			//情况3:参与操作时,符号在前:先变化后使用
			//执行过程:符号在前,先变化(++num3,num3变为11),后执行运算(在把11赋值给num4),
			int num3 = 10;
			int num4 = ++num3;
			System.out.println("num3:"+num3+",num4:"+num4);//num3:11,num4:11
				     

	}

}

2.3.3 Exercise 3: Find the larger value of two numbers

/*
三元运算符:
1.结构:(条件表达式)? 表达式1 : 表达式2
2. 说明
① 条件表达式的结果为boolean类型
② 根据条件表达式真或假,决定执行表达式1,还是表达式2.
  如果表达式为true,则执行表达式1。
  如果表达式为false,则执行表达式2。
③ 表达式1 和表达式2要求是一致的。
④ 三元运算符可以嵌套使用

3. 
凡是可以使用三元运算符的地方,都可以改写为if-else
反之,不成立。

4. 如果程序既可以使用三元运算符,又可以使用if-else结构,那么优先选择三元运算符。原因:简洁、执行效率高。
*/
package cn.tedu.basic;
 
import java.util.Scanner;
 
//这个类用来测试两个数里的大值
public class Test3_Max {
    
    
    public static void main(String[] args) {
    
    
       //1,接受键盘输入的两个整数
       //注意:scanner可以直接写,然后三目运算符boolean是他的返回值,前面可以写其他的类型来接受
       int a = new Scanner(System.in).nextInt();
       int b = new Scanner(System.in).nextInt();
      
       //2,比大小  1  ?  2  :3 ,1判断完如果成立,得到的结果就是2
       //a > b成立的话,把a交给max记录,否则,把b交给max记录
       int max = a > b ? a : b ;
      
       System.out.println(max);
 
    }
}

2.3.4 Exercise 4: Find the maximum of three numbers

package day0203_平年闰年;
 
import java.util.Scanner;
 
public class Test1_三个数的最大值 {
    
    
 
	public static void main(String[] args) {
    
    
		
		System.out.println("整数a:");
		int a = new Scanner(System.in).nextInt();
		
		System.out.println("整数b:");
		int b = new Scanner(System.in).nextInt();
 
		System.out.println("整数c:");
		int c = new Scanner(System.in).nextInt();
		
        int max = a>b?a:b;
		max=max>c?max:c;  //分开写
 
     或者
        int max = a>b?(a>c?a:c):(b>c?b:c);    //合一块写
		System.out.println(max);	
	}
}

2.3.5 Exercise 5: Exponentiation

//2^3  2*2*2=8 很多运算我们会借助一些工具类来操作。
double pow=Math.pow(3,2);
System.out.println(pow);

2.3.6 Exercise 6: Compound assignment operators

Lazy writing method, built-in strong transfer function.

1int a=10;
        int b=20;
        a+=b; //a=a+b;
        a-=b; //a=a-b
        System.out.println(a);//302byte a = 1;
//		a=(byte) (a+4);//右侧int,左侧byte,大转小,强转。
//		a=(byte) (a+4);//右侧int,左侧byte,大转小,强转。
		a+=4;//会自动完成数据类型的转换  
		//注意:a=a+4和a+=4是有区别的!!
		System.out.println(a);

2.3.7 Exercise 7: String concatenation character “+”

//字符串连接符:若+一端为字符串,则会转化为Sring类型并进行拼接。
int a=10;
int b=20;
System.out.println(" "+a+b);//1020
System.out.println(a+b+" ");//30
		

2.3.8 Exercise 8: Testing logical operator short-circuit issues

Logical operators : & and &&, the relationship between | and ||.
When an & expression is evaluated, both operands are evaluated, and && is more like an operator shortcut. When an && expression is evaluated, the first operand is evaluated first, and if it returns true the second operand is evaluated. If the first operand evaluates to false, the second operand will not be evaluated.

一般在程序中想要表示true或者false,可以用1表示true,用0表示false
 
单与:&  --  表示并且的关系
    1 & 2 -- 12都为true,结果为true
 
单或:|  --  表示或者的关系
    1 | 2 -- 12有一个为true,结果为true
 
双与/短路与:&& --  表示并且的关系,高效
    1 && 2 -- 2会被短路,前提是1false
 
双或/短路或:|| --  表示或者的关系,高效
    1 || 2 -- 2会被短路,前提是1true&-》真
真  &-》假
假  &-》假
假  &-》假
 
假  |-》假
假  |-》真
真  |-》真
真  |-》真
package lianxi;

import java.util.Arrays;
/*
运算符之四:逻辑运算符

&  && |  || ! ^

说明:
1.逻辑运算符操作的都是boolean类型的变量


*/
class LogicTest {
    
    
	public static void main(String[] args) {
    
    
		
		//区分& 与 &&
		//相同点1:& 与  && 的运算结果相同
		//相同点2:当符号左边是true时,二者都会执行符号右边的运算
		//不同点:当符号左边是false时,&继续执行符号右边的运算。&&不再执行符号右边的运算。
		//开发中,推荐使用&&
		boolean b1 = true;
		b1 = false;
		int num1 = 10;
		if(b1 & (num1++ > 0)){
    
    
			System.out.println("我现在在北京");
		}else{
    
    
			System.out.println("我现在在南京");
		}

		System.out.println("num1 = " + num1); //我现在在南京   num1 = 11
		

		boolean b2 = true;
		b2 = false;
		int num2 = 10;
		if(b2 && (num2++ > 0)){
    
    
			System.out.println("我现在在北京");
		}else{
    
    
			System.out.println("我现在在南京");
		}

		System.out.println("num2 = " + num2); //我现在在南京   num2 = 10
		

		// 区分:| 与 || 
		//相同点1:| 与  || 的运算结果相同
		//相同点2:当符号左边是false时,二者都会执行符号右边的运算
		//不同点3:当符号左边是true时,|继续执行符号右边的运算,而||不再执行符号右边的运算
		//开发中,推荐使用||
		boolean b3 = false;
		b3 = true;
		int num3 = 10;
		if(b3 | (num3++ > 0)){
    
    
			System.out.println("我现在在北京");
		}else{
    
    
			System.out.println("我现在在南京");
		}
		System.out.println("num3 = " + num3);//我现在在北京  num3 = 11
		


		boolean b4 = false;
		b4 = true;
		int num4 = 10;
		if(b4 || (num4++ > 0)){
    
    
			System.out.println("我现在在北京");
		}else{
    
    
			System.out.println("我现在在南京");
		}
		System.out.println("num4 = " + num4);// 我现在在北京  num4 = 10
		
	}
}

2.4 Branch structure 1: if

2.4.1 Overview

Insert image description here

Problem : Although programs with a sequential structure can solve problems such as calculation and output, they cannot make judgments and make choices. For problems that require judgment first and then choice, a branching structure should be used.

Summary :
Any complex program logic can be realized through three structures:
1) Sequential structure: executed line by line from top to bottom, each sentence must be executed
2) Branch structure: conditionally execute a certain statement once, not every sentence must be executed Go
3) Loop structure: conditionally execute a certain statement multiple times, not every sentence must be executed

2.4.2 Form

单分支:满足判断条件,则执行大括号里面的内容。
注意:判断条件又叫条件表达式
条件表达式:结果是booleanif(判断条件/条件表达式){
    
    
	代码。。。
}

双分支:
if(判断条件){
    
    
	代码1。。。
}else{
    
    
	代码2。。。
}

多分支:

if(判断条件1){
    
    
	代码1。。。
}else if(条件2){
    
    
	代码2。。。
} else if(判断条件3){
    
    
	代码3。。。
}else{
    
    
	代码4。。。
}

嵌套分支:
if(判断条件){
    
    
 if(判断条件){
    
    
    代码
 }
} 

Things to note :

  1. The if statement has at most one else statement, and the else statement follows all else if statements.
  2. The if statement can have several else if statements, and they must precede the else statement.
  3. Once one of the else statements detects true, the other else if statements and else statements are no longer executed.
  4. For the conditional structure, if the braces are omitted , it will only take effect on the first sentence of code (and: if there is no brace until the end of the first semicolon), in other words: if there is only one sentence of code in the conditional structure, the braces Can be omitted. But it is not recommended to omit it.
  5. else is optional.
  6. if branches can be nested

2.4.3 Exercise 1: Product discounts

Receives the original price entered by the user. 10% off for orders over 1,000. 20% off for purchases over 2,000. 50% off for purchases over 5,000.

Note: Multiple conditions in Java cannot be written as: a>b>c, but must be output using logical operators a>b&&b>c.
Reason: Because the result of the comparison operator is of boolean type, Boolean types cannot be compared.

package day999;
 
import java.util.Scanner;
 
public class ttt {
    
    
	public static void main(String[] args) {
    
    
/*
说明:
1. else 结构是可选的。
2. 针对于条件表达式:
   > 如果多个条件表达式之间是“互斥”关系(或没有交集的关系),哪个判断和执行语句声明在上面还是下面,无所谓。
   > 如果多个条件表达式之间有交集的关系,需要根据实际情况,考虑清楚应该将哪个结构声明在上面。
   > 如果多个条件表达式之间有包含的关系,通常情况下,需要将范围小的声明在范围大的上面。否则,范围小的就没机会执行了。
*/

		System.out.println("输入总原价");
		
		 //1,接受用户输入的原价
              double price = new Scanner(System.in).nextDouble();
             
              //2,计算折后价,并输出
              double now = price;//记录折后价
             //注意java中的多个条件不能写成如:a>b>c  而是要用逻辑运算符a>b&&b>c来输出。
              if( price >= 5000 ) {
    
    //满5000
                     now = price * 0.5 ;//now记录5折的价
              }else if( price >= 2000 ) {
    
    //满2000
                     now = price * 0.8 ;//now记录8折的价
              }else if( price >= 1000 ) {
    
    //满1000
                     now = price * 0.9 ;//now记录9折的价
              }
             
或者
if( price >= 1000 && price < 2000) {
    
    //满5000
                     now = price * 0.9 ;//now记录9折的价
              }else if( price >= 2000 && price < 5000 ) {
    
    //满2000
                     now = price * 0.8 ;//now记录8折的价
              }else if( price >= 5000  ) {
    
    //满1000
                     now = price * 0.5 ;//now记录5折的价
              }
 
              System.out.println("原价是:"+price+",折后价是:"+now);
             
       }
      
}

2.4.4 Exercise 2: Statistician Score

90 points or above, excellent
80~89, good
70~79, average
60~69, pass
60 points or less, failed

package game;
 
import java.util.Scanner;
 
public class aa {
    
    
	public static void main(String[] args) {
    
    
		double score = new Scanner(System.in).nextDouble();
         //!!!用来增强代码的健壮性
		if (score >= 100 || score <= 0) {
    
    
			System.out.println("请输入0~100以内的值");
            return;  //结束程序
		}
		if (score > 90 && score <= 100) {
    
    
			System.out.println("优秀");
		} else if (score >= 80 && score <= 90) {
    
    
			System.out.println("良好");
		} else if (score >= 70 && score <= 79) {
    
    
			System.out.println("中等");
		} else if (score >= 60 && score <= 69) {
    
    
			System.out.println("及格");
		}else if (score < 60) {
    
    
			System.out.println("不及格");
		}
 
	}
}
//优化
       String desc = "优秀";
      
       if(score >= 90) {
    
    //90分以上 优秀
           desc = "优秀";
       }else if(score >= 80 && score < 90 ) {
    
    //80~89 良好
           desc = "良好";
       }else if(score >= 70 && score < 80 ) {
    
    //70~79 中等
           desc = "中等";
       }else if(score >= 60 && score < 70 ) {
    
    //60~69 及格
           desc = "及格";
       }else if(score < 60) {
    
    //60分以下 不及格
           desc = "不及格";
       }
      
       System.out.println("得分是:"+score+",属于:"+desc);

2.4.5 Exercise 3: Use if to compare three numbers

/*
编写程序:由键盘输入三个整数分别存入变量num1、num2、num3,
对它们进行排序(使用 if-else if-else),并且从小到大输出。

说明:
1. if-else结构是可以相互嵌套的。
2. 如果if-else结构中的执行语句只有一行时,对应的一对{}可以省略的。但是,不建议大家省略。
*/
import java.util.Scanner;
class IfTest2 {
    
    
	public static void main(String[] args) {
    
    
		
		Scanner scanner = new Scanner(System.in);

		System.out.println("请输入第一个整数:");
		int num1 = scanner.nextInt();
		System.out.println("请输入第二个整数:");
		int num2 = scanner.nextInt();
		System.out.println("请输入第三个整数:");
		int num3 = scanner.nextInt();

		if(num1 >= num2){
    
    
			if(num3 >= num1)
				System.out.println(num2 + "," + num1 + "," + num3);
			else if(num3 <= num2)
				System.out.println(num3 + "," + num2 + "," + num1);
			else
				System.out.println(num2 + "," + num3 + "," + num1);
			
		
		}else{
    
    
			if(num3 >= num2)
				System.out.println(num1 + "," + num2 + "," + num3);
			else if(num3 <= num1)
				System.out.println(num3 + "," + num1 + "," + num2);
			else
				System.out.println(num1 + "," + num3 + "," + num2);
			
		}

	}
}

2.5 [Understanding] Branch structure 2: switch

2.5.1 Overview

When a case is established, all cases from this matched case, including default, will be penetrated backward until the program ends or a break program is encountered .
Advantages : high efficiency, clear structure
Disadvantages : only applicable to integers and equality

2.5.2 Form

In switch(expr1), expr1 is an integer expression . The integer expression can be the int basic type or the Integer packaging type . Since byte, short, and char can be implicitly converted to int, they are also supported.
Note :

  1. The case label must be a string constant or literal and cannot declare a scope .
  2. Note that it is an expression rather than a conditional expression (conditional expression: the result is boolean).
  3. The expression in the switch structure can only be one of the following six data types:
    byte, short, char, int, enumeration type (new in JDK5.0), String type (new in JDK7.0)
  4. break, can be used in the switch-case structure, indicating that once this keyword is executed, the switch-case structure will be jumped out.
  5. The break keyword is optional.
  6. default: Equivalent to else in the if-else structure.
    The default structure is optional and the position is flexible. There is no need to add break when written at the end.
switch(变量或者表达式){
    
    
	case 1: 
	      break;
	case 2: 
	      break;    //break:  跳出switch循环,可选。
	case 3:
	      break; 
    case 4:
         break; 
	default:   //default 加个保险也可以不写,当前面都没有匹配到的时候相当于加个保险走默认的。
 
}

2.5.3 Exercise 1: Number matching

package cn.tedu.ifdemo;
 
//测试switch语法
public class Test7_Switch {
    
    
    public static void main(String[] args) {
    
    
      测试1int i = 2;
      
//  i 的类型 也就是括号里表示的类型 -- byte short int char jdk1.7新增了String
       switch( i ) {
    
    
       case 1 : 
            System.out.println(1);
            break;
       case '1' : 
            System.out.println('1');
            break;
      
       /*1,   当匹配到case时,程序会继续向后穿透所有case包括default,即
       *满足一个case条件时会把剩余所有case包括default里面的内容都会
       *进行输出,直到程序结束或者遇到break程序才结束。
       */
       case 2 : 
            System.out.println(2);
            break;
       //2,   break用来结束程序
       case 3 : 
            System.out.println(3);
            break;
       case 4 : 
            System.out.println(4);
            break;
       case 5 : 
            System.out.println(5);
            break;
       case 6 : 
            System.out.println(6);
            break;
      
       default: 
            System.out.println(0);//default之后不需要再写break
      
       }
	//如果输出的条件一样则可以简写为如:
         case 0:
		 case 1:
		 case 2:
		 case 3:
			 System.out.println("小明");
		     break;

	测试2String season = "summer";
		switch (season) {
    
    
		case "spring":
			System.out.println("春暖花开");
			break;
		case "summer":
			System.out.println("夏日炎炎");
			break;
		case "autumn":
			System.out.println("秋高气爽");
			break;
		case "winter":
			System.out.println("冬雪皑皑");
			break;
		default:
			System.out.println("季节输入有误");
			break;
		}


		测试3//**************如下的两种情况都编译不通过*********************
		//情况一:表达式为布尔类型
		/*
		boolean isHandsome = true;
		switch(isHandsome){
		
		case true:
			System.out.println("我好帅啊~~~");
			break;
		case false:
			System.out.println("我好丑啊~~~");
			break;
		default:
			System.out.println("输入有误~~~");
		}
		*/
		//情况二:case值不是常量
		/*
		int age = 10;
		switch(age){
		case age > 18:
			System.out.println("成年了");
			break;
		default:
			System.out.println("未成年");
		}
		*/
      
    }
   
}

2.5.4 Exercise 2: Abbreviation of case

/*
例题:对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。

说明:如果switch-case结构中的多个case的执行语句相同,则可以考虑进行合并。
*/
class SwitchCaseTest1 {
    
    
	public static void main(String[] args) {
    
    
		
		/*
		int score = 78;
		switch(score){
		case 0:

		case 1:

		case 2:

			...
		case 100:
		
		}
		*/

		/*
		int score = 78;
		if(score >= 60){
		
		}else{
		
		}
		*/
		
		int score = 78;
		switch(score / 10){
    
    
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			System.out.println("不及格");
			break;
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			System.out.println("及格");
			break;

		}
		
		//更优的解决方案:
		switch(score / 60){
    
    
		case 0:
			System.out.println("不及格");
			break;
		case 1:
			System.out.println("及格");
			break;
		}
	
	}
}

2.6 Comparison between if and switch

/*
从键盘分别输入年、月、日,判断这一天是当年的第几天
 
   注:判断一年是否是闰年的标准:
       1)可以被4整除,但不可被100整除
	或
       2)可以被400整除


说明:
1. 凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。
2. 我们写分支结构时,当发现既可以使用switch-case,(同时,switch中表达式的取值情况不太多),
  又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。

*/
import java.util.Scanner;
class SwitchCaseExer {
    
    
	public static void main(String[] args) {
    
    
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入year:");
		int year = scan.nextInt();
		System.out.println("请输入month:");
		int month = scan.nextInt();
		System.out.println("请输入day:");
		int day = scan.nextInt();


		//定义一个变量来保存总天数
		int sumDays = 0;

		switch(month){
    
    
		case 12:
			sumDays += 30;
		case 11:
			sumDays += 31;
		case 10:
			sumDays += 30;
		case 9:
			sumDays += 31;
		case 8:
			sumDays += 31;
		case 7:
			sumDays += 30;
		case 6:
			sumDays += 31;
		case 5:
			sumDays += 30;
		case 4:
			sumDays += 31;
		case 3:
			//sumDays += 28;
			//判断year是否是闰年
			if((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0){
    
    
				sumDays += 29;
			}else{
    
    
				sumDays += 28;
			}

		case 2:
			sumDays += 31;
		case 1:
			sumDays += day;
		}

		System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays + "天");
	}
}

3 Day04–Loop

3.1 Loop structure 1: for

3.1.1 Overview

A loop structure refers to a program structure that is set up to perform a certain function repeatedly in a program . It determines whether to continue executing a certain function or to exit the loop based on the conditions in the loop body.According to the judgment conditions, the loop structure can be subdivided into a loop structure that judges first and then executes and a loop structure that executes first and then judges.

Advantages of for loop : The for loop statement is a general structure that supports iteration.The most efficient and flexiblecycle structure.

Three elements of circulation :

  1. Initialization of loop variables
  2. The condition of the loop is of Boolean type (based on the loop variable)
  3. Changes in loop variables (towards the end of the loop)

Loop variable : The number that changes repeatedly throughout the loop.

3.1.2 Form

for(开始条件;循环条件;更改条件){
    
    
	循环体代码…
}
 
For循环的特殊用法:
1. 死循环
  For(;  ;){
    
      //死循环
}
2. 可以拆开写
 int i=1; 
 for(;i<10;i++){
    
     //向上拆
 }
 
for(int i=1;i<10;){
    
      //向下拆
     i++;   //注意这个地方多了个分号(;)}
3. 可以同时声明2个数:
  for(int i=1,j=6;i<6;i+2,j--){
    
    
 }   //for中表达式一和表达式三可以使用逗号,而表达式二多个只能使用 &&, || ,!(且,或,非)

3.1.3 Exercise 1: Print 0 to 10

Insert image description here

package cn.tedu.fordemo;
 
//测试for循环
public class Test8_For {
    
    
    public static void main(String[] args) {
    
    
       //for( 开始条件    ;   判断条件    ;  更改条件   ){   循环体   }
//     打印0到10
      
       //i用来记录每次获取到的值,默认是从0开始
       //i定义在for循环里面:在for循环内有效。出了for循环就失效了。
       for( int i = 0 ; i <= 10 ;  i++){
    
     
          
           System.out.println(i);  
       }
      
      
      
    }
}

3.1.4 Exercise 2: Print 10 to 0

//从10开始,只要>=0,就打印,打印的数据依次递减
for( int i = 10 ; i >= 0 ; i-- ){
    
     
          
    System.out.println(i);
}


3.1.5 Exercise 3: Print 8,88,888,8888

for( int i = 8 ; i <= 8888 ; i=i*10+8 ){
    
      
   
	System.out.print(i+",");//同行展示数据。如8,88,....
}

3.1.6 Exercise 4: Find the number of odd numbers and the number of even numbers in [1,100]

int count = 0, why not assign an additional value of 0 instead of int count? ? I talked about it when day03 was abnormal..

       int count = 0;//定义变量,记录奇数的个数
       int oucount = 0;//定义变量,记录偶数的个数,(定义为0,下面赋值不会影响程序)
       for(int i = 1 ; i <= 100 ; i++) {
    
    
           //判断,如果是奇数
           if( i % 2 == 1 ) {
    
       //也可以写!=0
              count++;//每次遇到一个奇数就+1
           }
           //判断,如果是偶数
           if( i % 2 == 0 ) {
    
    
              oucount++;//每次遇到一个偶数就+1
           }
       }
      
       System.out.println("奇数的个数是:"+count+",偶数的个数是:"+oucount);

3.1.7 Exercise 5: Find the sum of even numbers and odd numbers in [1,100]

       int sum = 0;//定义变量,记录偶数和
       int jisum = 0;//定义变量,记录奇数和
       
       for(int i = 1 ; i <= 100 ; i++) {
    
    
           //判断,如果是偶数
           if(i % 2 == 0) {
    
    
              //对获取到的每个偶数求和
              sum = sum + i;
           }
          
           //判断,如果是奇数
           if(i % 2 == 1) {
    
     //或则i%2!=0也行
              jisum = jisum + i;//把每个奇数求和
           }
          
          
       }
      
       System.out.println("偶数和是:"+sum+",奇数和是:"+jisum);

3.2 Nested for loop

3.2.1 Overview

According to the conditions of the outer layer, it is judged whether the inner layer can be executed. If it can be executed, the inner layer code will be looped, and then the outer layer will be executed and the judgment will be continued. .

3.2.2 Form

for(){
    
    
	for(){
    
    
		
	}
}

3.2.3 Getting Started Case

package day0000;
/*
嵌套循环的使用
1.嵌套循环:将一个循环结构A声明在另一个循环结构B的循环体中,就构成了嵌套循环

2.
外层循环:循环结构B
内层循环:循环结构A

3. 说明
① 内层循环结构遍历一遍,只相当于外层循环循环体执行了一次
② 假设外层循环需要执行m次,内层循环需要执行n次。此时内层循环的循环体一共执行了m * n次

4. 技巧:
   外层循环控制行数,内层循环控制每行的列数
*/ 
public class T {
    
    
 
	public static void main(String[] args) {
    
    
//		f1();
		f2();
	}
 
	//总结1:当i=1时,j取到了所有满足条件的数据,1,2,3,4,5。
	//也就是说外循环执行1次,内循环执行多次
	private static void f1() {
    
    
		for(int i=1;i<=3;i++){
    
    //外循环
			System.out.println("i="+i);//1,2,3
			for(int j=1;j<=5;j++){
    
    //内循环
				System.out.println("j="+j);//1,2,3,4,5
			}
		}
	}
 
//总结2:外循环控制行,内循环控制列
	private static void f2() {
    
    
		for(int i=1;i<=3;i++){
    
    
			for(int j=1;j<=5;j++){
    
    
				System.out.print("*");
			}
			System.out.println();
		}
	}
 
}

3.2.4 Exercise 1: Print a square

****
****
****
****
for(int i=1;i<5;i++){
    
    
	for(int j=1;j<5;j++){
    
    
		System.out.print("*");
	}
	System.out.println();
}	

3.2.5 Exercise 2: Print left right triangle

*
**
***
****
*****
//正直角三角形
private static void f4() {
    
    
	for (int i = 0; i < 5; i++) {
    
    //外循环,控制行,是一定的
		for (int j = 0; j <= i; j++) {
    
    //内循环,列是不固定的,是递增的
			System.out.print("*");//保证第一行打印出来一个*,注意条件
		}
		System.out.println();
	}
}

3.2.6 Exercise 3: Print the 99 multiplication table

//99乘法表   正序
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
private static void f4() {
    
    
	for (int i = 1; i < 9; i++) {
    
    //外循环,控制行,是一定的
		for (int j = 1; j <= i; j++) {
    
    //内循环,列是不固定的,是递增的
			System.out.print(i+"*"+j+"="+i*j+" ");//保证第一行打印出来一个*,注意条件
		}
		System.out.println();
	}
}

//乘法表倒序
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*4=4	2*4=8	3*4=12	4*4=16	
1*3=3	2*3=6	3*3=9	
1*2=2	2*2=4	
1*1=1	

		for(int i=9;i>=1;i--) {
    
    
			for(int j=1;j<=i;j++) {
    
    
				System.out.print(j+"*"+i+"="+i*j+"\t");
			}
			System.out.println();
		}
 
 

3.2.7 Exercise 4: Print right right triangle

Print right angle

    *
   **
  ***
 ****
*****
 
package day999;
public class a {
    
    
    public static void main(String[] args) {
    
    
       //输出5行
       for(int i=1;i<=5;i++){
    
    
           //空格三角
           for(int x=5;x>i;x--){
    
    //还可以改为:for(int x=1;j<=5-i;x++) 
              System.out.print(" ");
           }
           //*号三角
           for(int j=1;j<=i;j++){
    
    
              System.out.print("*");
           }
           System.out.println();
       }
    }
}

3.2.8 Exercise 5: Print full triangles

Print full triangle

     *
    ***
   *****
  *******
 ********* 
package day999;
public class a {
    
    
    public static void main(String[] args) {
    
    
       //打印5行
       for(int i=1;i<=5;i++){
    
    
           //打印空格的倒三角(原理是:每轮输出一个空格)
           for(int j=5;j>=i;j--){
    
     //可换:for(int j=1;j<=5-i;j++)
              System.out.print(" ");
           }
          
           //打印*号的正三角
           for(int k=1;k<=i*2-1;k++){
    
    
              System.out.print("*");
           }
          
           System.out.println();
       }
    }
}
 

3.3 Enhanced for loop

Insert image description here

package lianxi;

import java.util.Random;
import java.util.Scanner;

public class Ta {
    
    

	public static void main(String[] args) {
    
    
		
		
		int [] numbers = {
    
    10,20,50,7,0,9,22,11};//定义一个数组
		for (int aa: numbers) {
    
    
			System.out.println(aa);
		}

	}

}

3.4 break和continue

To terminate the loop, you can use these two methods.
Insert image description here
Precautions: Break, continue cannot be declared after the direct item, otherwise compilation or error will be reported. As shown below:
Insert image description here

3.4.1 Form

break: 中断当前循环,简单粗暴
for(){
    
    
	代码1
if(条件){
    
    
  代码3break;//如果成立,直接跳出这个for循环
}
代码2}
continue:跳出本次循环,进入下一轮
for(){
    
    
	代码1
if(条件){
    
    
	代码3continue;//如果成立,跳出本次for循环,进入下一轮
}
代码2}

3.4.2 Exercise 1: Find the number 88

Receive 100 numbers entered by the user. If it is not 88, continue to enter, and return if 88 is found.

package cn.tedu.breakdemo;
 
import java.util.Scanner;
 
//测试break和continue
//总结:break和continue都可以结束循环。
//1,break可以立刻结束循环简单粗暴。continue是结束当前循环可以继续下一次循环。
//2,break和continue之后都不能出现代码,出现了也是不可到达的代码
public class Test3_Break {
    
    
       public static void main(String[] args) {
    
    
             
              for(int i = 1 ; i <= 100 ; i++) {
    
    
                    
                     //1,接受用户输入的数字
                     int input = new Scanner(System.in).nextInt();
                    
                     //2,判断是不是88
                     if(input == 88) {
    
    //是88
                           
                            System.out.println("中了");
                            break;//程序结束
        
                           
                     }else if(input != 88) {
    
    //不是88
                           
                            continue;//继续输入
                     }          
              }       
       }    
}

3.5 Loop structure 2: while

Judge first, then execute
Insert image description here

3.5.1 Format

while(执行条件){
    
    
	代码…
}

3.5.2 Exercise 1: Generate random numbers 1~100 and guess the numbers

Generate a random number and compare it to the number the user has been entering.
To generate random numbers, view the API for details.

package cn.tedu.whiledemo;
 
import java.util.Random;
import java.util.Scanner;
/**
 *测试while循环
 *总结:
 * 1.while也是一种循环结构:while(循环条件){循环体}
 * 2.while(true)是简单的实现了死循环的形式,for也可以实现代码显得复杂一些。
 * 3.这种结构  --  先判断,再执行
 */
public class Test4_While {
    
    
       public static void main(String[] args) {
    
    
        /**
		 * 产生随机数的2种方法:
		 *  第1种:int num=(int)(Math.random()*1000+1);  生成随机数1~1000
		 *  解释:Math.random()返回的是0(包含)到1(不包含)之间的double值,转化为整数需要强转。
		 *  第2种:int random = new Random().nextInt(100) +1; 生成随机数1~100
		 *  解释:要产生m以内的随机数,默认从0开始 ,不包括m 如:0~100 不包括100(0~99)可以加个1变为(1~100)
		 */
		           
		         //1,让程序产生随机数
		         int random = new Random().nextInt(100) +1;
		         System.out.println(random);
		             
		         //2,开始猜 --重复做a b的事情,放入循环结构中
                 //while(random > 50) {
    
    
		             
		        //for形式的死循环:让循环条件一直为true就行 
                //for(int i = 1; i >= 1 ; i++ ) {
    
    
		         while(true) {
    
     //死循环  -- 必须设置程序的出口!!

		                //a,接受键盘输入的值
		                int input = new Scanner(System.in).nextInt();
		                    
		                //b,拿到输入的值,和 ,random去比
		                if(input > random) {
    
    
		                       System.out.println("大了");
		                }else if(input < random) {
    
    
		                       System.out.println("小了");
		                }else if(input == random) {
    
    
		                       System.out.println("中了");
		                       break;//程序结束  -- 死循环的出口!!!
		                }
		                    
		         }
             
             
       }
      
}
 

3.5.2 Exercise 2: Generate random numbers 10~100

//如何获取一个随机数:10 - 99
// [0.0,1.0) --> [0.0,90.0) --->[10.0, 100.0) -->[10,99]
/*解释:
Math.random()生成double类型的值:[0.0,1.0),包含0无限接近于1。
乘以90变为[0.0,90.0),包含0无限接近于90。
加10变为[10.0, 100.0),包含10无限接近于100(99.99999999...)。
在强转为int类型,舍弃掉小数位变成-->[10,99]
*/
int value = (int)(Math.random() * 90 + 10);
System.out.println(value);
//公式:[a,b]  :  (int)(Math.random() * (b - a + 1) )+ a

3.6 Loop structure 3: do-while

Execute first, judge later
Insert image description here

3.6.1 Format

do{
    
    
	代码…
}while(执行条件);    //注意这里有个分号

3.6.2 Exercise 1: Guess the number

Generate a random number and compare it to the number the user has been entering.

package cn.tedu.whiledemo;
 
import java.util.Random;
import java.util.Scanner;
 
//测试do...while循环
//总结:
//do...while  结构,保证循环体代码 ,最少,执行一次
//能不能继续循环,要看能不能满足循环条件
public class Test5_Dowhile {
    
    
    public static void main(String[] args) {
    
    
       int random = new Random().nextInt(100) + 1;
       System.out.println(random);
 
       do {
    
    
           // a,接受键盘输入的值
           int input = new Scanner(System.in).nextInt();
 
           // b,拿到输入的值,和 ,random去比
           if (input > random) {
    
    
              System.out.println("大了");
           } else if (input < random) {
    
    
              System.out.println("小了");
           } else if (input == random) {
    
    
              System.out.println("中了");
              break;// 程序结束 -- 死循环的出口!!!
           }
          
       }while (random > 50);//明确循环条件
 
    }
   
   
}

3.7 Summary: Differences between the three types of cycles

All three types of loops can replace each other
1. for: know the number of loops (highest application rate)
2. while/do while: when the number of loops is uncertain
3. while: judge first, if it does not comply with the rules, the code will not be executed (possibly once Neither is executed)
4. do while: The code is executed at least once, and then it is judged to comply with the rules and the code is executed again.

How to determine which loop to use?
1. If it is related to the number of times, use a for loop
2. When judging whether condition 1 and condition 3 are the same: if they are the same, use do while; if they are different, use while (variable initialization, condition, change amount)

4 Day05–Method + Array

4.1 Method

4.1.1 Overview: methods (functions, procedures)

1. Method: Encapsulate a specific business logic function.
2. Methods should be as independent as possible, and each method only does one thing.
3. Methods can be called multiple times.
4. Reduce code duplication, which is beneficial to code maintenance and team collaboration.
5. Methods can have parameters or no parameters. Multiple parameters are separated by commas (depending on requirements).
6. Methods and methods are in a sibling relationship.
7. Method name naming rules: camel case naming method.
8. Other methods can be called within a method, but methods cannot be defined within a method.

4.1.2 Form

修饰符  返回值类型  方法名(参数列表){
    
    
	方法体;
    return 返回值;// void(无返回值)   int,double,String…有返回值
}

4.1.3 Exercise 1: Method Calling

Method call :Called based on method signature

Method signature : method name + parameter list

Syntax :

  1. No return value type (void): 方法名(有参传参);
  2. There are return value types :数据类型 变量 = 方法名(有参传参);

Note :

  1. In a class , there cannot be two methods with exactly the same signature .
  2. As long as the return value type is not void, there must be a return value return, and you need to declare a data type to receive it.
  3. Actual parameters : parameters passed when calling the method (can be a definite number or an uncertain number)
  4. Formal parameters : When defining a method, the parameters are defined in the parameter list of the method.
  5. When the method is called, the actual parameter values ​​are passed to the method's parameter variables. It must be ensured that the type and number of parameters passed conform to the declaration of the method.

Insert image description here
Description :The following calls are all 同一个类calls between methods, and the methods need to be modified as static resources because static methods can call each other..
If you want to call non-static resources in static resources, you can call them by creating objects.

package cn.tedu.method;
//包名可以直接在代码上修改,鼠标放上去会提示创建没有的包。
//同理 :类也一样
 
//测试方法的使用+-------------------------------
public class Test6_Method {
    
    
    public static void main(String[] args) {
    
    
       System.out.println(1);
      
       function();//调用了指定的function()
      
       System.out.println(2);
    }
   
    //创建function()  如果没有在main方法中调用方法,则程序不会主动执行这个方法。
    //修饰符  返回值  方法名(参数列表){方法体}
    public static void function() {
    
    
       System.out.println(3);
       System.out.println(4);
       System.out.println(5);
      
    }
   
   
}

通过创建对象可以在静态调用非静态:
package com.cn.ins;


//包名可以直接在代码上修改,鼠标放上去会提示创建没有的包。
//同理 :类也一样

//测试方法的使用+-------------------------------
public class Test6_Method {
    
    
  public static void main(String[] args) {
    
    
     System.out.println(1);
    
    // function();//调用了指定的function()
    
     System.out.println(2);
     Test6_Method a=new Test6_Method();
     a.function();
     
  }
 
  //创建function()  如果没有在main方法中调用方法,则程序不会主动执行这个方法。
  //修饰符  返回值  方法名(参数列表){方法体}
  public  void function() {
    
    
     System.out.println(3);
     System.out.println(4);
     System.out.println(5);
    
  }
 
 
}


4.1.4 Exercise 2: Method passing parameters

package cn.tedu.method;
 
//测试方法的使用
public class Test6_Method {
    
    
    public static void main(String[] args) {
    
    
       System.out.println(1);
      
//     function();//调用了指定的function()
      
//     param(10);//方法传参    
//     param2("jack",10);//传两参数--叫实参
//注意:调用方法时传的参数叫做实参,不管是数字还是一个变量都是实参
    
//     param3(5,10,"jack"); 参数的个数和类型相同,但是顺序不同也代表参数列表不同,方法不同。
       param4("jack",5,10);
      
       System.out.println(2);
       //在括号外面赋值,与方法内直接赋值一样。
       int m=10;
       String n=”rose”;
       Param5(m,n);
    }
    //创建param5的方法
    public static void param5(inta,String b) {
    
    
    System.out.println(a+b+);
}
 
    //TODO 创建param4("jack",5,10) --方法传参:参数类型必须匹配
    public static void param4(String a,int b,int c) {
    
    
       System.out.println(a+b+c);//jack510
    }
   
    //TODO 创建param3(5,10,"jack") --方法传参:参数类型必须匹配
//多个参数之间用”,”隔开。
    public static void param3(int a,int b,String c) {
    
    
       //+在数字间是加法运算,和字符串+是拼接字符串
       System.out.println(a+b+c);//15jack
    }
   
    //创建param2("jack",10) --方法传参:参数类型必须匹配
    //String n,int a  -- 叫形参:方法里面的参数叫形参
    public static void param2(String n,int a) {
    
    
       System.out.println(n);
       System.out.println(a);
    }
   
   
    //创建param() --方法传参:参数类型必须匹配
    //修饰符  返回值  方法名(参数列表){方法体}
//  public static void param(参数类型  参数名) {
    
    
    public static void param(int num) {
    
    
       System.out.println(num);
    }
   
    //创建function()
    //修饰符  返回值  方法名(参数列表){方法体}
    public static void function() {
    
    
       System.out.println(3);
       System.out.println(4);
       System.out.println(5);
      
    }
   
}

4.1.5 Exercise 3: Method return value

return 返回值;:After the method ends, it can return a piece of data, called a return value. The method must specify the type of the return value when declaring it.

Note :

  1. Methods can have a return value or no return value (void).
  2. Do not declare execution statements similar to break and continue after the return direct item, otherwise the compilation will report an error.
    Insert image description here

When is there a return value? When is there no return value?
When method execution ends :

  1. If you still need to use a certain data in the method - there is a return value (the return value type cannot be void, and it is used in conjunction with return. If there is a return value, you need to define a variable or array to accept it.)
  2. If you no longer need to use a certain data in the method - there is no return value (the return value type is void, there is no need to write return.)

What are the functions of the two forms of Return?
The first one: return 值 ;
1. End the execution of the method
2. Return to the caller. (Note: As long as there is a return value type, there must be a return value)

The second type: return ;
3. End the execution of the method (Note: There is no need to write return for a type without a return value, because the program ends after execution, and it makes no sense to write it directly. Sometimes it can be used in conjunction with the if branch structure to jump out of the program .)

package cn.tedu.method;
                           
//测试方法返回值
public class Test7_Method2 {
    
    
    public static void main(String[] args) {
    
    
      
       //result记录着method()的执行结果a b的和
       int result = method(10,5);
       System.out.println(result);
      
       //TODO 
       String name = method2("jack","rose","jerry");
       System.out.println(name);
      
    }
   
    //TODO 创建 method2("jack","rose","jerry")
    public static String method2(String a,String b,String c){
    
    
      
       return a+b+c;//返回值不单单是一个变量,数。也可以返回一个式子
    }
   
   
    //创建method(10,5)
    //注意:方法的返回值要和result变量的类型一致
    public static int method(int a,int b) {
    
    
      
       //通过 return 可以把方法执行的结果,返回给,调用位置!!
       return a+b;
    }
   
   
   
}
//注意:例子,返回的是不同类型看是否是字符串拼接,用String
String sum2=method("jack",10,5);
		System.out.println(sum2);
	public static String method(String c,int a,int b) {
    
    
		return c+a+b; //这里是一个字符串的拼接所以用String类型。返回值类型与return后的类型一致。
	}

4.2 Overloading of methods

4.2.1 Concept

1. Method overloading refers toin the same class, the method name is the same, the parameter list is different , the method body is generally different, and the same has no meaning. Overloading has nothing to do with method bodies.
2. The compiler will automatically bind and call different methods according to the signature of the method during compilation. (We can think of overloaded methods as completely different methods, but they just have the same method name.
3.Overloading is for methods

Note :Determining method overloading has nothing to do with the method's permission modifier, return value type, formal parameter variable name, or method body!

The significance of overloading : For programmers, the method names that need to be remembered are very simple. They only need to remember a method with a name, and the parameters of the method can be passed in very flexibly.

The parameter list is different : including parameter type, order, and number.

4.2.2 Exercise 1: Summing numbers

package cn.tedu.overload;
 
//测试方法重载:
//要求:方法名相同+参数列表不同
 
//意义:由于用户在使用时,传入的参数我们不知道,为了体现我的程序非常的灵活
//尽量多提供一些参数不同,名字相同的方法
public class Test8_Overload {
    
    
   
    public static void main(String[] args) {
    
    
       add(5);
       add(10,5);
       add("jack",5);
       add(10,"rose");
 
 
    //TODO
       print(10);
       print("jerry");
       print('中');
    }
   
    //创建add(10,"rose")
    public static void add(int a,String b) {
    
    
       System.out.println(a+b);//10rose
    }
   
    //创建add("jack",5)
    public static void add(String a,int b) {
    
    
       System.out.println(a+b);//jack5
    }
   
    //创建add(10,5)
    public static void add(int a,int b) {
    
    
       System.out.println(a+b);
    }
   
    //创建add(5)
    public static void add(int a) {
    
    
       System.out.println(a*a);
    }
   
}
//总结:方法,传参,数据类型与参数列表保持一致,(即:个数,类型,顺序保持一致)。
//返回值:若有返回值应与返回值类型,返回值接收的类型保持一致。
 
//如:
public static void main(String[] args) {
    
    
		//方法练习
		method(2,3,"小明");
		String aa=method2(2,3,"小红");
		System.out.println(aa);
 
	}
	public static void method(int a,int b,String c) {
    
    
		System.out.println(a+b+c);
	}
	public static String method2(int a,int b,String c) {
    
    
		return a+b+c;//有String类型,int 类型。它拼接的是字符串类型,所以应该用String作为它的返回值类型,它的接受值也是String类型。
	}

4.2.3 Exercise 2: Printing of data

package day004;                          
                                            
public class Test07 {
    
                        
                                           
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		print(10);                         
		print(2.2);                           
		print(true);                  
		print("jack");                          
                                                   
	}                                       
	public static void print(int a) {
    
    
		System.out.println(a);               
	}                                      
	public static void print(double a) {
    
    
		System.out.println(a);               
	}                                            
	public static void print(boolean a) {
    
    
		System.out.println(a);               
	}                                         
	public static void print(String name) {
    
    
		System.out.println(name);             
	}                                            
                                               
}                     

4.2.4 Exercise 3: Automatic type promotion for overloading

When overloading a method, if there is no corresponding method for the actual parameters entered when calling the method, and automatic type conversion happens to be met, the method of the large type that meets the conditions will be called.

package com.cn.ca;

public class StudentTest {
    
    

	public static void main(String[] args) {
    
    
		Teacher teacher = new Teacher();
		/*解释:自动类型提升
		 * getSum方法没有注释之前根据方法的调用规则,调用getSum(int i,int j)输出为1
		 * getSum方法没有注释之后虽然调用时输入的值为int类型,但会自动类型提升,调用getSum(double d1,double d2)输出为2
		 */
		teacher.getSum(1, 5); 

	}

}

class Teacher{
    
    
	
//	public void getSum(int i,int j){
    
    
//		System.out.println("1");
//	}
	
	public void getSum(double d1,double d2){
    
    
		System.out.println("2");
	}
	
}

4.3 Variable parameters

Variadics are essentially arrays.
Insert image description here

package com.cn.ca;


/*
 * 可变个数形参的方法
 * 
 * 1.jdk 5.0新增的内容
 * 2.具体使用:
 *   2.1 可变个数形参的格式:数据类型 ... 变量名
 *   2.2 当调用可变个数形参的方法时,传入的参数个数可以是:0个,1个,2个,。。。
 *   2.3 可变个数形参的方法与本类中方法名相同,形参不同的方法之间构成重载
 *   2.4 可变个数形参的方法与本类中方法名相同,形参类型也相同的数组之间不构成重载。换句话说,二者不能共存。
 *   2.5 可变个数形参在方法的形参中,必须声明在末尾
 * 	 2.6 可变个数形参在方法的形参中,最多只能声明一个可变形参。
 *   2.7 同一类型普通方式优先级比可变参数更高
 *   2.8 遍历方式和数组相同
 *   
 * 
 */
public class MethodArgsTest {
    
    
	
	public static void main(String[] args) {
    
    
		
		MethodArgsTest test = new MethodArgsTest();
		test.show(12);
//		test.show("hello");
//		test.show("hello","world");
//		test.show();
		
		
		//调用方法形参是数组相比是可变参数更麻烦些,当然这个数组的调用方式也可以调用可变参数
		test.show(new String[]{
    
    "AA","BB","CC"});
		
	}
	
	
	public void show(int i){
    
    
		
	}
	
	public void show(String s){
    
    //同一类型普通方式优先级比可变参数更高
		System.out.println("show(String)");
	}
	
	public void show(String ... strs){
    
    //调用时输入的参数可以是0个,可以是多个
		System.out.println("show(String ... strs)");
		
		for(int i = 0;i < strs.length;i++){
    
    //遍历方式和数组相同
			System.out.println(strs[i]);
		}
	}
	/*不能与上一个方法同时存在String[] strs和String ... strs,
	  编译器认为方法名相同 参数列表相同的数组和可变参数是同一个方法,不能共存。 
	*/
//	public void show(String[] strs){
    
    
//		
//	}
	
	//The variable argument type String of the method 
	//show must be the last parameter
//	public void show(String ...strs,int i){ 多个参数,可变参数只能声明在最后
//		
//	}
	
}


4.4 Value transfer of methods

4.4.1 Review: Variable assignment operations

package com.cn.ca;


/*
 * 
 * 关于变量的赋值:
 * 
 *  如果变量是基本数据类型,此时赋值的是变量所保存的数据值。
 *  如果变量是引用数据类型,此时赋值的是变量所保存的数据的地址值。
 * 
 */
public class ValueTransferTest {
    
    
	
	public static void main(String[] args) {
    
    
		
		System.out.println("***********基本数据类型:****************");
		int m = 10;
		int n = m;
		
		System.out.println("m = " + m + ", n = " + n);//m = 10, n = 10
		
		n = 20;
		
		System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20
		
		System.out.println("***********引用数据类型:****************");
		
		Order o1 = new Order();
		o1.orderId = 1001;
		
		Order o2 = o1;//赋值以后,o1和o2的地址值相同,都指向了堆空间中同一个对象实体。
		
		System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);//o1.orderId = 1001,o2.orderId = 1001
		
		o2.orderId = 1002;//改变了o2的值 o1也随之改变
		
		System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);//o1.orderId = 1002,o2.orderId = 1002
		
	}
	
}

class Order{
    
    
	
	int orderId;
	
}



4.4.2 Similarly: formal parameter assignment operation of method – basic data type

package com.cn.ca;
/*
 * 方法的形参的传递机制:值传递
 * 
 * 1.形参:方法定义时,声明的小括号内的参数
 *   实参:方法调用时,实际传递给形参的数据
 * 
 * 2.值传递机制:
 * 如果参数是基本数据类型,此时实参赋给形参的是实参真实存储的数据值。
 * 如果参数是引用数据类型,此时实参赋给形参的是实参存储数据的地址值。
 * 
 */
public class ValueTransferTest1 {
    
    
	public static void main(String[] args) {
    
    
		
		int m = 10;
		int n = 20;
		
		System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20
		//交换两个变量的值的操作
//		int temp = m ;
//		m = n;
//		n = temp;
		
		ValueTransferTest1 test = new ValueTransferTest1();
		test.swap(m, n);
		
		System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20 此时仍输出的是main中的m n而不是swap方法中的m n
		
		
	}
	
	
	public void swap(int m,int n){
    
    //相当于是把10 20赋值给了新的变量m n,形参是局部变量出了方法就失效
		                          //如果复制的是引用地址,堆的数据只有一份可以交换成功,出了方法也不会失效
		int temp = m ;
		m = n;
		n = temp;
		System.out.println("m = " + m + ", n = " + n);//m = 20, n = 10
	}
}

4.4.3 Similarly: method parameter assignment operation – reference data type

package com.cn.ca;


public class ValueTransferTest2 {
    
    
	
	public static void main(String[] args) {
    
    
		
		Data data = new Data();
		
		data.m = 10;
		data.n = 20;
		
		System.out.println("m = " + data.m + ", n = " + data.n);//m = 10, n = 20
		
		//交换m和n的值
//		int temp = data.m;
//		data.m = data.n;
//		data.n = temp;
		
		ValueTransferTest2 test = new ValueTransferTest2();
		test.swap(data);
		
		
		System.out.println("m = " + data.m + ", n = " + data.n);//m = 20, n = 10交换成功
		
	}
	
	public void swap(Data data){
    
    
		int temp = data.m;
		data.m = data.n;
		data.n = temp;
	}
	
}

class Data{
    
    
	
	int m;
	int n;
	
}

4.5 Recursion

Insert image description here
Note :

  1. There should not be too many recursion levels. If there are too many, the computer efficiency will be greatly reduced.
  2. If you don't need recursion, you don't need it. It can be achieved through some other algorithms.

4.5.1 Recursive case exercise

package com.atguigu.java2;

/*
 * 递归方法的使用(了解)
 * 1. 递归方法:一个方法体内调用它自身。
 * 2. 方法递归包含了一种隐式的循环,它会重复执行某段代码,但这种重复执行无需循环控制。
 * 递归一定要向已知方向递归,否则这种递归就变成了无穷递归,类似于死循环。
 * 
 */
public class RecursionTest {
    
    

	public static void main(String[] args) {
    
    

		// 例1:计算1-100之间所有自然数的和
		// 方式一:for循环
		int sum = 0;
		for (int i = 1; i <= 100; i++) {
    
    
			sum += i;
		}
		System.out.println(sum);
		// 方式二:递归
		RecursionTest test = new RecursionTest();
		int sum1 = test.getSum(100);
		System.out.println(sum1);
		
		System.out.println("*****************");
		int value = test.f(10);
		System.out.println(value);

	}

	// 例1:计算1-n之间所有自然数的和
	public int getSum(int n) {
    
    // 3

		if (n == 1) {
    
    
			return 1;
		} else {
    
    
			return n + getSum(n - 1);
		}

	}

	// 例2:计算1-n之间所有自然数的乘积:n! 阶乘如5*4*3*2*1
	public int getSum1(int n) {
    
    

		if (n == 1) {
    
    
			return 1;
		} else {
    
    
			return n * getSum1(n - 1);
		}

	}
	
	//例3:已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),
	//其中n是大于0的整数,求f(10)的值。
	public int f(int n){
    
    
		if(n == 0){
    
    
			return 1;
		}else if(n == 1){
    
    
			return 4;
		}else{
    
    
//			return f(n + 2) - 2 * f(n + 1);
			return 2*f(n - 1) + f(n - 2);
		}
	}

	//例4:斐波那契数列
	
	//例5:汉诺塔问题
	
	//例6:快排

}


Insert image description here

4.6 Array

4.6.1 Overview and classification of arrays

Overview : Array is a collection of multiple data of the same type arranged in a certain order , named with a name, and managed uniformly through numbering.

Classification of arrays :

  • ① According to the dimension: one-dimensional array, two-dimensional array,…
  • ② According to the type of array elements: array of basic data type elements, array of reference data type elements

4.6.2 Array-related concepts

The symbol of array Array is : [ ]

Get the element value in the array : You can get it through the subscript of the element. The subscript starts from 0, and the maximum value of the subscript is its length -1.

Subscript : also called subscript and index

The length of the array : the number of elements

Type of array : Array Array is a reference type.The address is stored, the address occupies very little memory in the space (just a string of numbers). The elements of the array can be either basic data types or reference data types.
Insert image description here

4.6.3 Characteristics of arrays

  1. Arrays are ordered.
  2. Creating an array object opens up a contiguous space in memory.
  3. The length of the array is determined and cannot be modified once created.
  4. The elements in the array are of the same type, and mixed types are not allowed.
  5. Arrays are variables of reference data type. The elements of the array can be either basic data types or reference data types.
  6. Array variables are reference types. Arrays can also be regarded as objects. Each element in the array is equivalent to a member variable of the object.
    The array itself is an object. In Java, objects are in the heap. Therefore, whether an array array stores primitive types or other object types,The array object itself is in the heap

4.6.4 Declaration of one-dimensional array

Generally divided into dynamic initialization and static initialization .

Syntax :

数组类型[ ]  数组名 = 数组对象;//推荐使用
   或者
数组类型  数组名[ ] = 数组对象;//不推荐使用,这是c++语言的习惯,早期为了c++的程序员学习java创建的。

Case :

动态初始化:先声明后赋值,需要指定存入的元素个数。
int[] a = new int[4];//数组的类型应该与所存入的每一个数据类型一致
a[0] =1;
a[1] =4;
a[2] =7;
a[3] =5;

静态初始化:声明数组的同时赋值
int[] b = new int[]{
    
    1,2,3,4,5};//数组之间的元素用逗号隔开。
int[] c = {
    
    1,2,3,4,5};//简写形式

Default initialization of arrays : The array is a reference type, and its elements are equivalent to the instantiation variables of the class, soThe array has allocated space, where each element is also implicitly initialized in the same way as an instance variable.

int[] aa = new int[5];
此时已经在内存中分配好空间了,每个元素的默认值都为int类型的值 0.

Insert image description here

4.6.5 Length of array

  • lengthProperty gets the array length.
  • Once an array is created, its length is immutable, that is, the array is of fixed length.
  • Arrays of 0 length are allowed.

4.6.6 Array Bounds

Insert image description here

4.6.7 Exercise 1: Store hello in an array

package cn.tedu.array;
 
import java.util.Scanner;
 
//这个类用来测试数组的创建
public class Test1_Array {
    
    
       public static void main(String[] args) {
    
    
              //1,创建数组。存入hello
              //确定数组类型,数组长度 
//           静态
//因为每个格子放一个字符,所以用char类型。
              char[] a = new char[] {
    
    'h','e','l','l','o'};
              char[] b =  {
    
    'h','e','l','l','o'};
              System.out.println(a);
              System.out.println(b);
             
              // 2,动态 创建数组,需要,指定数组的长度!!
              char[] c = new char[5];
             //注意:存字符串是用的char 数组。不是string
 
              //3,数组中的元素都有下标,默认从0开始
              c[0]='h';//给第1个元素,也就是下标为0的元素,赋值
              c[1]='e';//给第2个元素,也就是下标为1的元素,赋值
              c[2]='l';//给第3个元素,也就是下标为2的元素,赋值
              c[3]='l';//给第4个元素,也就是下标为3的元素,赋值
              c[4]='o';//给第5个元素,也就是下标为4的元素,赋值
             
              //4,java.lang.ArrayIndexOutOfBoundsException数组下标越界异常
             //原因是数组的长度如果是5,最大下标是4,根本不存在下标为5的情况,下标5超出范围
             //c[5]='o';//给第6个元素,也就是下标为5的元素,赋值  ---  报错!!
             
              System.out.println(c);
              
       }
      
}

4.7 Array traversal

Access the array positions sequentially from beginning to end.

4.7.1 Form

1.普通for循环
for(从下标为0的位置开始;到数组长度-1结束;下标++){
    
    
    循环体代码
}
2.增强for循环,但是取不到下标。
for(声明语句:表达式){
    
    
    循环体代码
} 

4.7.2 Exercise 1: Enhanced for loop traversal

package lianxi;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Ta {
    
    

	public static void main(String[] args) {
    
    
		int[] arrays = {
    
    1,3,6,3,8,9,0};
		for(int aa:arrays) {
    
    
			System.out.println(aa);
		}
	}
	
}

4.7.3 Exercise 2: Output the number of days in each month

package cn.tedu.array;
 
//数组练习
public class Test2_Array2 {
    
    
    public static void main(String[] args) {
    
    
      
       method();// 输出每个月的天数
    }
 
    // 创建method()
    public static void method() {
    
    
       // 1,静态创建数组
       int[] days = {
    
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
       // 2,用下标遍历数组
       // i代表了下标
       // int i = 0 ,从下标 0开始
       // i <= days.length-1,到下标的最大值结束
       // i++,依次向后遍历
       for (int i = 0; i <= days.length - 1; i++) {
    
    
           // days[i] 指days数组中,每个下标存着的值
           System.out.println((i + 1) + "月有" + days[i] + "天");
       }
    }
 
}//直接输出数组则输出的是地址,输出对应的下标才是它代表的元素

Output the elements represented by each subscript in the array in turn.
Insert image description here

4.7.4 Exercise 3: Traverse the array and store 1 to 10

package lianxi;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Ta {
    
    

	public static void main(String[] args) {
    
    
		
		  //1,动态 创建数组
        int[] a = new int[10];
        System.out.println(Arrays.toString(a));//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
       
        //2,遍历数组,存数据
        for(int i = 0 ; i <= a.length-1 ; i++ ) {
    
    
               //获取每个元素a[i]
               a[i] = i+1;
        }
       
        //3,查看数组中的元素  
        System.out.println(a);//[I@659e0bfd]
        /**
         * 除了char数组可以直接输出里面的元素,其它的数组都是地址.
         * 想看不是char[]的数组里的元素是啥--Arrays工具类  toString()可以将指定的数组数据显示成一个字符串
         */
        System.out.println(Arrays.toString(a));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	
	}
	
}

Output the array directly:
Insert image description here

4.7.5 Exercise 4: Creating a random array

Get an array of random values ​​within 100: new Random() method

// 创建method3()
    public static void method3() {
    
    
       //1,  动态 创建数组
       int a[] = new int[10];
      
       //2.   遍历数组,重新赋值
       for(int i = 0 ; i <= a.length-1 ; i++) {
    
    
           //a[i]获取元素
           a[i]=new Random().nextInt(100);
       }
      
       //3.   查看数组里的元素
       System.out.println(Arrays.toString(a));
      
    }

4.7.6 Exercise 5: Create a random array to find the maximum and minimum average

Get an array of random values ​​within [10,99]: new Random() method

package com.atguigu.java;
/*
 * 算法的考查:求数值型数组中元素的最大值、最小值、平均数、总和等
 * 
 * 定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,
 * 然后求出所有元素的最大值,最小值,和值,平均值,并输出出来。	
 * 要求:所有随机数都是两位数。
 * 
 * [10,99]
 * 公式:(int)(Math.random() * (99 - 10 + 1) + 10)
 * 
 */
public class ArrayTest1 {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = new int[10];
		
		for(int i = 0;i < arr.length;i++){
    
    
			arr[i] = (int)(Math.random() * (99 - 10 + 1) + 10);
		}
		
		//遍历
		for(int i = 0;i < arr.length;i++){
    
    
			System.out.print(arr[i] + "\t");
		}
		System.out.println();
		
		//求数组元素的最大值
		int maxValue = arr[0];//注意这个地方写 0,如果里面的值都是负数无法比较。
		for(int i = 1;i < arr.length;i++){
    
    
			if(maxValue < arr[i]){
    
    
				maxValue = arr[i];
			}
		}
		System.out.println("最大值为:" + maxValue);
		
		//求数组元素的最小值
		int minValue = arr[0];
		for(int i = 1;i < arr.length;i++){
    
    
			if(minValue > arr[i]){
    
    
				minValue = arr[i];
			}
		}
		System.out.println("最小值为:" + minValue);
		//求数组元素的总和
		int sum = 0;
		for(int i = 0;i < arr.length;i++){
    
    
			sum += arr[i];
		}
		System.out.println("总和为:" + sum);
		//求数组元素的平均数
		int avgValue = sum / arr.length;
		System.out.println("平均数为:" + avgValue);
	}
}

4.7.7 Exercise 6: Array assignment explanation

package com.atguigu.exer;
/*
 * 使用简单数组
(1)创建一个名为ArrayExer2的类,在main()方法中声明array1和array2两个变量,他们是int[]类型的数组。
(2)使用大括号{},把array1初始化为8个素数:2,3,5,7,11,13,17,19。
(3)显示array1的内容。
(4)赋值array2变量等于array1,修改array2中的索引元素,使其等于索引值(如array[0]=0,array[2]=2)。打印出array1。
 * 
 * 思考:array1和array2是什么关系?array1和array2地址值相同,都指向了堆空间的唯一的一个数组实体。
 * 拓展:修改题目,实现array2对array1数组的复制
 */
public class ArrayExer2 {
    
    
	public static void main(String[] args) {
    
      //alt + /
		int[] array1,array2;
		
		array1 = new int[]{
    
    2,3,5,7,11,13,17,19};
		
		//显示array1的内容
		for(int i = 0;i < array1.length;i++){
    
    
			System.out.print(array1[i] + "\t");
		}
		
		//赋值array2变量等于array1
		//不能称作数组的复制。
		array2 = array1;
		
		/*修改array2中的偶索引元素,使其等于索引值(如array[0]=0,array[2]=2)
		 即下标为偶数时,元素值等于它本身的下标值。
		*/
		for(int i = 0;i < array2.length;i++){
    
    
			if(i % 2 == 0){
    
    
				array2[i] = i;
			}
			
		}
		System.out.println();
		//打印出array1,发现结果和修改后的array2结果相同
		for(int i = 0;i < array1.length;i++){
    
    
			System.out.print(array1[i] + "\t");
		}
	}
}

Explanation : Because the object is only new once, there is only one value in the heap.
array1 = new int[]{2,3,5,7,11,13,17,19};
array2 = array1;
These two steps mean assigning the address value of the heap to array1, and then assigning the address value of array1 to array2 , array1 and array2 have the same address value at this time. Modifying the value of array2 means that there is only one value in the heap, so of course the value of array1 has also changed.
Real-life analogy : It is equivalent to creating a file on the disk and then sending the desktop shortcut. The contents of the file you open on the desktop and the file you open on the disk are the same.
Insert image description here

4.7.8 Exercise 7: Copy reverse search of array (line bisection)

package com.atguigu.java;
/*
 * 算法的考查:数组的复制、反转、查找(线性查找、二分法查找)
 * 
 * 
 */
public class ArrayTest2 {
    
    
	
	public static void main(String[] args) {
    
    
		
		String[] arr = new String[]{
    
    "JJ","DD","MM","BB","GG","AA"};
		
		
		//数组的复制(区别于数组变量的赋值:arr1 = arr)
		//相当于磁盘文件复制到桌面一份,对桌面的文件进行修改
		String[] arr1 = new String[arr.length];
		for(int i = 0;i < arr1.length;i++){
    
    
			arr1[i] = arr[i];
		}
		
		//数组的反转
		/*方法一:不管是除进还是除不尽都不影响,除于2是因为 第一步第一个元素和最后一个进行交换
		最后一步 最后一个与第一个进行交换,相当于交换了2次。*/
//		for(int i = 0;i < arr.length / 2;i++){
    
    
//			String temp = arr[i];
//			arr[i] = arr[arr.length - i -1]; 把最后元素赋值给第一个
//			arr[arr.length - i -1] = temp; 把第一个交换给第二个
//		}
		
		//方法二:
//		for(int i = 0,j = arr.length - 1;i < j;i++,j--){
    
    
//			String temp = arr[i];
//			arr[i] = arr[j];
//			arr[j] = temp;
//		}
		
		//遍历
		for(int i = 0;i < arr.length;i++){
    
    
			System.out.print(arr[i] + "\t");
		}
		
		System.out.println();
		//查找(或搜索)
		//线性查找:重头到尾依次查找
		String dest = "BB";
		dest = "CC";
		
		boolean isFlag = true;
		
		for(int i = 0;i < arr.length;i++){
    
    
			
			if(dest.equals(arr[i])){
    
    
				System.out.println("找到了指定的元素,位置为:" + i);
				isFlag = false;
				break;
			}
			
		}
		if(isFlag){
    
    
			System.out.println("很遗憾,没有找到的啦!");
			
		}
		//二分法查找:(熟悉)先分为2半,如果查找的第1份有对应值,则另一份就不找了。效率更高
		//前提:所要查找的数组必须有序。
		int[] arr2 = new int[]{
    
    -98,-34,2,34,54,66,79,105,210,333};
		
		int dest1 = -34;
		dest1 = 35;
		int head = 0;//初始的首索引
		int end = arr2.length - 1;//初始的末索引
		boolean isFlag1 = true;
		while(head <= end){
    
    
			
			int middle = (head + end)/2;    //4
			
			if(dest1 == arr2[middle]){
    
    
				System.out.println("找到了指定的元素,位置为:" + middle);
				isFlag1 = false;
				break;
			}else if(arr2[middle] > dest1){
    
    
				end = middle - 1;
			}else{
    
    //arr2[middle] < dest1
				head = middle + 1;
			}

			
		}
		
		if(isFlag1){
    
    
			System.out.println("很遗憾,没有找到的啦!");
		}
		
		
	}
}

4.8 Array tool class Arrays

java.util.Arrays : Tool class for operating arrays, which defines many methods for operating arrays

Note: Array sorting, copying, bisection algorithms, etc. are encapsulated. When we use them, we can directly call the tool class methods, unlike handwriting them above. During development, if the tool class has a corresponding method, use the tool class to write it yourself.

4.8.1 Arrays.toString(array)

Concatenate the data in the array into a string using commas. (The return value is String)
Format: [10, 14, 20, 46, 51]

package day006;
 
import java.util.Arrays;
 
public class Test02 {
    
    
 
	public static void main(String[] args) {
    
    
		// to String
		method();
 
	}
	public static void method() {
    
    
		int[] aa= {
    
    1,2,3,4,5,6};
//直接输出整个数组不需要遍历,之前遍历是插入。		
//		for(int i=0;i<aa.length-1;i++) {
    
    
//		}
        //直接打印是地址值如:[I@15db9742使用工具类后是:[1, 2, 3, 4, 5, 6]                                           
		System.out.println(Arrays.toString(aa));
	}   
 
}

4.8.2 Arrays.sort(array)

Sort the array in ascending order : Use the optimized quick sort algorithm for basic type arrays, which is highly efficient. For reference type arrays, use the optimized merge sort algorithm.

No return value.
example:

package lianxi;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Ta {
    
    

	public static void main(String[] args) {
    
    
		int[] as = {
    
    4,9,4,1,2,5,9,6,4,5,2};
		Arrays.sort(as);
	    System.out.println(Arrays.toString(as));
	}
	
}

Insert image description here

4.8.3 Arrays.copyOf(array, new length)

Copies an array into a new array of specified length . (Only the new array has changed, the original array has not changed)
The length of the new array is larger than the original array, which is equivalent to copying and increasing the position. --Array expansion.
The length of the new array is smaller than the original array, which is equivalent to intercepting the previous part of the data. --Array reduction
There is a return value

4.8.4 Test toString, sort, copyOf

package cn.tedu.array;
 
import java.util.Arrays;
 
//测试数组工具类
//为什么sort()没有返回值是,void
//为什么copyOf()有返回值,如:int[]
//原因是:sort()在原数组基础上,把数组调整位置
//copyOf():数组一旦创建,长度不可变!!!所以必须把产生的新数组返回/而这里改变了长度。
public class Test4_Arrays {
    
    
    public static void main(String[] args) {
    
    
       int[] a = {
    
    97, 7, 41, 37, 55, 3, 3, 34, 34, 83};
      
//     sortArray(a);//a是实参,给无序数组排序
      
       int[] newA = copyArray(a);//数组复制a是原数组
//     [97, 7, 41, 37, 55, 3, 3, 34, 34, 83, 0, 0, 0, 0, 0]
//     [97, 7, 41, 37, 55]
       System.out.println(Arrays.toString(newA));
      
    }
   
    //创建copyArray第一种方式,可以扩容,缩容(及新数组的长度大于小于新数组的长度都可以)
    public static int[] copyArray(int[] from) {
    
    
      
       //copyOf(from,num)--from是原数组名,num是新数组长度  -- 新数组长度 > 原数组长度  -- 扩容
       int[] to = Arrays.copyOf(from, 15);
//     [97, 7, 41, 37, 55, 3, 3, 34, 34, 83, 0, 0, 0, 0, 0]
      
       //from是原数组名,5是新数组长度  -- 新数组长度 < 原数组长度 -- 缩容  --相当于截取数据。
       int[] to2 = Arrays.copyOf(from, 5);
//     [97, 7, 41, 37, 55]
      
       return to2;
    }
 
   
   
    //创建sortArray
    public static void sortArray(int[] a) {
    
    //形参
       //sort()用来对无序数组进行排序
       Arrays.sort(a);//底层源码
      
//     [3, 3, 7, 34, 34, 37, 41, 55, 83, 97]
       //toString()对指定的数组内容,显示成字符串
       System.out.println(Arrays.toString(a));
    }
   
   
}
例子:
	//数组的复制 第2种arraycopy   都是在新的数组上改变,注意两者写法的区别。
		int [] we= {
    
    4,8,5,8,9};    //新数组小了自动截取,大了会自动填充。
		int[] we1=Arrays.copyOf(we, 4);
		System.out.println(Arrays.toString(we));
		System.out.println(Arrays.toString(we1));
		
		System.out.println("11111111111");//打桩
		int[] we2=new int[5];//这个是新数组长度小则会有数组下标越界异常。那个不会。优点是显得更灵活。
		System.arraycopy(we,0,we2,0,5);
        System.out.println(Arrays.toString(we2));
        //数组的扩容
        int we3[]=Arrays.copyOf(we, we.length+1);
        System.out.println(Arrays.toString(we3));

4.8.5 Arrays.equals

//boolean equals(int[] a,int[] b):判断两个数组是否相等。
		int[] arr1 = new int[]{
    
    1,2,3,4};
		int[] arr2 = new int[]{
    
    1,3,2,4};
		boolean isEquals = Arrays.equals(arr1, arr2);
		System.out.println(isEquals);//false

4.8.6 Arrays.fill

int[] arr1 = new int[]{
    
    1,2,3,4};
//void fill(int[] a,int val):将指定值填充到数组之中。
Arrays.fill(arr1,10);
System.out.println(Arrays.toString(arr1));//[10, 10, 10, 10]

4.8.7 Arrays.binarySearch binary algorithm search

//int binarySearch(int[] a,int key)
		int[] arr3 = new int[]{
    
    -98,-34,2,34,54,66,79,105,210,333};
		int index = Arrays.binarySearch(arr3, 210);
		if(index >= 0){
    
    //返回值为整数代表找到,为什么正负代表找到没找到,因为源码是这样设计的
			System.out.println(index);
		}else{
    
    //返回值为负数没找到
			System.out.println("未找到");
		}

4.9 Common exceptions in arrays

package com.atguigu.java;
/*
 * 数组中的常见异常:
 * 1. 数组角标越界的异常:ArrayIndexOutOfBoundsExcetion
 * 
 * 2. 空指针异常:NullPointerException
 * 
 */
public class ArrayExceptionTest {
    
    
	public static void main(String[] args) {
    
    
		
		//1. 数组角标越界的异常:ArrayIndexOutOfBoundsExcetion
		int[] arr = new int[]{
    
    1,2,3,4,5};
		
//		for(int i = 0;i <= arr.length;i++){
    
    
//			System.out.println(arr[i]);
//		}
		
//		System.out.println(arr[-2]);
		
//		System.out.println("hello");
		
		//2.2. 空指针异常:NullPointerException
		//情况一:原来有地址,现在改为Null了没有地址,在根据地址值找对的值
//		int[] arr1 = new int[]{1,2,3};
//		arr1 = null;
//		System.out.println(arr1[0]);
		
		//情况二:
//		int[][] arr2 = new int[4][];
//		System.out.println(arr2[0][0]);
		
		//情况三:
		String[] arr3 = new String[]{
    
    "AA","BB","CC"};
		arr3[0] = null;
		System.out.println(arr3[0].toString());
	}
}

4.10 Expansion:

4.10.1 Understanding two-dimensional arrays

An array that stores arrays, that is to say, the data stored in the array is still in the form of an array.
Insert image description here

Case 1:

public class Test06 {
    
    
       //ctrl +n   快捷键相当于new在用上下键 (创建项目,包,类全程不用鼠标)
	public static void main(String[] args) {
    
    
		// 二维数组: 数组套数组
		//格式: 定位:如,c[0][1] 遍历2次.
		int [][] c={
    
    {
    
    1,2},{
    
    3,4},{
    
    5,6}};//声明形式
		for(int i=0;i<=c.length-1;i++) {
    
    //遍历外层数组
			for(int j=0;j<=c[i].length-1;j++) {
    
     //遍历内层数组
				System.out.println(c[i][j]);//输出形式
				
			}
			
		}
 

Case 2:

package com.atguigu.java;
/*
 * 二维数组的使用
 * 
 * 1.理解:
 * 对于二维数组的理解,我们可以看成是一维数组array1又作为另一个一维数组array2的元素而存在。
 * 其实,从数组底层的运行机制来看,其实没有多维数组。
 * 
 * 2. 二维数组的使用:
 *   ① 二维数组的声明和初始化
 *   ② 如何调用数组的指定位置的元素
 *   ③ 如何获取数组的长度
 *   ④ 如何遍历数组
 *   ⑤ 数组元素的默认初始化值 :见 ArrayTest3.java
 *   ⑥ 数组的内存解析 :见 ArrayTest3.java
 * 
 * 
 */
public class ArrayTest2 {
    
    
	public static void main(String[] args) {
    
    
		//1.二维数组的声明和初始化
		int[] arr = new int[]{
    
    1,2,3};//一维数组
		//静态初始化
		int[][] arr1 = new int[][]{
    
    {
    
    1,2,3},{
    
    4,5},{
    
    6,7,8}};
		//动态初始化1
		String[][] arr2 = new String[3][2];
		//动态初始化2
		String[][] arr3 = new String[3][];
		//错误的情况 
//		String[][] arr4 = new String[][4];
//		String[4][3] arr5 = new String[][];
//		int[][] arr6 = new int[4][3]{
    
    {1,2,3},{4,5},{6,7,8}};
		
		//也是正确的写法:
		int[] arr4[] = new int[][]{
    
    {
    
    1,2,3},{
    
    4,5,9,10},{
    
    6,7,8}};
		int[] arr5[] = {
    
    {
    
    1,2,3},{
    
    4,5},{
    
    6,7,8}};
		
		//2.如何调用数组的指定位置的元素
		System.out.println(arr1[0][1]);//2
		System.out.println(arr2[1][1]);//null
		
		arr3[1] = new String[4];
		System.out.println(arr3[1][0]);
		
		//3.获取数组的长度
		System.out.println(arr4.length);//3
		System.out.println(arr4[0].length);//3
		System.out.println(arr4[1].length);//4
		
		//4.如何遍历二维数组
		for(int i = 0;i < arr4.length;i++){
    
    
			
			for(int j = 0;j < arr4[i].length;j++){
    
    
				System.out.print(arr4[i][j] + "  ");
			}
			System.out.println();
		}
		
	}
}

Case 3:

package com.atguigu.java;
/*
 * 二维数组的使用:
 * 	规定:二维数组分为外层数组的元素,内层数组的元素
 * 		int[][] arr = new int[4][3];
 * 		外层元素:arr[0],arr[1]等
 * 		内层元素:arr[0][0],arr[1][2]等
 * 
 *   ⑤ 数组元素的默认初始化值 
 *   针对于初始化方式一:比如:int[][] arr = new int[4][3];
 *      外层元素的初始化值为:地址值
 *      内层元素的初始化值为:与一维数组初始化情况相同
 *      
 *   针对于初始化方式二:比如:int[][] arr = new int[4][];
 *   	外层元素的初始化值为:null
 *      内层元素的初始化值为:不能调用,否则报错。
 *   
 *   ⑥ 数组的内存解析 
 * 
 */
public class ArrayTest3 {
    
    
	public static void main(String[] args) {
    
    
		
		int[][] arr = new int[4][3];
		System.out.println(arr[0]);//[I@15db9742 
		System.out.println(arr[0][0]);//0
		
//		System.out.println(arr);//[[I@6d06d69c
		
		System.out.println("*****************");
		float[][] arr1 = new float[4][3];
		System.out.println(arr1[0]);//地址值
		System.out.println(arr1[0][0]);//0.0
		
		System.out.println("*****************");
		
		String[][] arr2 = new String[4][2];
		System.out.println(arr2[1]);//地址值
		System.out.println(arr2[1][1]);//null
		
		System.out.println("*****************");
		double[][] arr3 = new double[4][];
		System.out.println(arr3[1]);//null
//		System.out.println(arr3[1][0]);//报错
		
	}
}

4.10.2 Understanding bubble sort

Bubble Sort is a relatively simple sorting algorithm in the field of computer science.
It repeatedly visits the column of elements to be sorted, compares two adjacent elements in turn, and swaps them if their order (such as from large to small, first letter from A to Z) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, which means that the elements have been sorted.
The name of this algorithm comes from the fact that larger elements will slowly "float" to the top of the sequence through exchange (arranged in ascending or descending order), just like the carbon dioxide bubbles in carbonated drinks will eventually float to the top, hence the name "bubble" Sort".

Insert image description here
Bubble sort explained :

  1. Compare 2 adjacent elements in the array. If the first number is greater than the second number, we swap its positions.
  2. Each comparison produces a maximum or minimum number.
  3. The next round can be reduced by one sorting.
  4. Repeat until the end.

Insert image description here
Optimization of bubble sorting : There are many. The most basic one can be judged by variable identification. If the sorting is correct, there is no need to compare.

4.10.3 Bubble sort code

package cn.tedu.bubble;
 //第一种 20个数比 20轮   每轮比-i-1次
		//第二种 20个数比19轮    每轮比-i次
import java.util.Arrays;
 
//这个类用来测试冒泡排序算法
public class Test3_Bubble {
    
    
    public static void main(String[] args) {
    
    
       //1,定义无序数组
       int[] a = {
    
    21, 94, 21, 58, 47, 16, 10, 9, 50, 44};
      
       //2,排序
       //5个数,比4轮。外循环控制轮数,让外循环执行4次
       //i < a.length-1   a.length是5,-1 就是 4,i<4,外循环执行4次
       for(int i = 0 ; i < a.length-1 ; i++ ) {
    
    
          
           //内循环控制每轮需要相邻比较多少次
       //-i :第1轮,需要比4次,五个数参与比较 -0,
           //第2轮,需要比3次,四个数参与比较 -1,(因为第一轮沉出来一个大值不参与比较)
           //第3轮,需要比2次,四个数参与比较 -2,(因为前两轮沉出来两个大值不参与比较)
           //第4轮,需要比1次,四个数参与比较 -3,(因为前三轮沉出来三个大值不参与比较)
           for(int j = 0 ; j < a.length-1 - i; j++ ) {
    
    
              //1,相邻元素比较
              //前一个值a[j]大于后面的值a[j+1]
              if(a[j]>a[j+1]) {
    
    
                  //2,交换顺序s
                  int t = a[j];
                 
                  a[j] = a[j+1];
                 
                  a[j+1] = t;
              }
           }
          
       }
      
      
       System.out.println(Arrays.toString(a));
      
    }
   
}

4.10.4 How to optimize bubble sort code

package lianxi;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Ta {
    
    

	public static void main(String[] args) {
    
    
		//1,定义无序数组
	       int[] a = {
    
    21, 94, 21, 58, 47, 16, 10, 9, 50, 44};
	      
	       //2,排序
	       //5个数,比4轮。外循环控制轮数,让外循环执行4次
	       //i < a.length-1   a.length是5,-1 就是 4,i<4,外循环执行4次
	       for(int i = 0 ; i < a.length-1 ; i++ ) {
    
    
	    	   boolean flag=false;//通过flag标识减少没有意义的比较
	          
	           //内循环控制每轮需要相邻比较多少次
	       //-i :第1轮,需要比4次,五个数参与比较 -0,
	           //第2轮,需要比3次,四个数参与比较 -1,(因为第一轮沉出来一个大值不参与比较)
	           //第3轮,需要比2次,四个数参与比较 -2,(因为前两轮沉出来两个大值不参与比较)
	           //第4轮,需要比1次,四个数参与比较 -3,(因为前三轮沉出来三个大值不参与比较)
	           for(int j = 0 ; j < a.length-1 - i; j++ ) {
    
    
	        	   
	              //1,相邻元素比较
	              //前一个值a[j]大于后面的值a[j+1]
	              if(a[j]>a[j+1]) {
    
    
	                  //2,交换顺序s
	                  int t = a[j];                 
	                  a[j] = a[j+1];                 
	                  a[j+1] = t;
	                  flag=true;
	              }	              
	           }
	           if(flag==false) {
    
    
	            	  break;
	              }	          
	       }	      
	       System.out.println(Arrays.toString(a));
	}	
}

Insert image description here

4.10.5 Understanding other algorithms

Such as: merge algorithm, bisection algorithm, fast algorithm, etc. Bubbles are the most common and are also the ones with the highest occurrence rate in interviews.

4.10.6 Sparse arrays

B station Crazy God sparse array video link: https://www.bilibili.com/video/BV12J41137hu?p=59&spm_id_from=pageDriver

Guess you like

Origin blog.csdn.net/aa35434/article/details/131211508