Java-day06 study notes

2021-01-05

day05 review

Insert picture description here

Common editing tools

1. 电脑自带的记事本
2. 高级记事本notepad++、Editplus
3. 集成开发工具IDE(Integrated Development Environment)Eclipse、Intellij IDEA

1. IDEA

It's charged, but very easy to use.

A subsidiary of JetBrains.

1.1 Download and installation

官网下载地址:
https://www.jetbrains.com/idea/download/#section=windows

注意:
	Community为社区版本,免费但是功能较少(完全免费)
	Ultimate为终极版本,收费但是功能强大(30天试用)

1.2 Project layout

Insert picture description here

1.3 Create a project and module

1. 在IDEA初试界面点击 Create New Project
2. 选择Empty Project创建空项目
3. 在自动弹出来的Project Settings中对Project进行设置,设置之后点击apply应用一下(注意:先别点击OK)
		1. 进行SDK选择(一般选择jdk8或者jdk11)
		2. 进行编译版本选择(和jdk版本保持一致即可)
4. 在Project Settings中的Modules中点击 + 号 创建一个模块
		1. 点击 + 号之后会有一个add标题栏,然后点击 New Module
		2. 在弹出的New Module窗口中新建一个Java Module即可
	
注意:
	一个项目中是可以放多个模块Module的

在一个项目中添加多个Module的方式为:
1. 点击File,选择 Project Structure
2. 在弹出的Project Structure中的Modules中新建一个Module即可(方式同上)

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here

1.4 Simple configuration

① Font size

方式一:一次性调节
			对File-Settings-Editor-Font中的Size进行字体大小设置
方式二:通过ctrl+鼠标滚轮调节
			在File-Settings-Editor中点击General,然后在右侧的Mouse中选择Change font size(Zoom)with Ctrl + Mouse Wheel

Insert picture description here

② Subject

Insert picture description here

1.5 Shortcut operations

1. main方法
		在编辑界面写上 main或者 psvm
2. 输出语句
		在编辑界面写上 sout
3. 单行注释
		Ctrl + /
4. 多行注释
		Ctrl + Shift + /
5. 删除选中的行
		Ctrl + Y
6. 赋值选中的行
		Ctrl + D
7. 代码格式化
		Ctrl + Alt + L

Two, loop

2.1 Nine-Nine Multiplication Table

public class Test {
    
    

    public static void main(String[] args) {
    
    

        for(int i = 1; i <= 9; i++){
    
    
            for(int j = 1; j <= i; j++){
    
    

                System.out.print(j + "*" + i + "=" + (i*j) + "\t");
            }
            System.out.println();
        }

    }
}

2.2 Dead loop

while的死循环:
    while(true){

    }

for的死循环:
	for(;;){
	
	}
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        //while的死循环
//        while(true){
    
    
//            System.out.println("小菜打篮球!!!");
//        }

        //for的死循环:
        for(;;){
    
    
            System.out.println("小菜打篮球!!!");
        }

    }
}

2.3 Jump statement

break      用在switch或循环语句中,用来结束switch语句或循环语句
continue   用在循环语句中,用来跳出当次循环并进行下次循环
return     用在方法中(后面讲)
public class Demo {
    
    

    public static void main(String[] args) {
    
    

        for(int i = 0; i < 5; i++){
    
    

            //当i == 2的时候,结束循环
            if(i == 2){
    
    
//                break;
                continue;
            }
            System.out.println(i);
        }
    }
}

Three, method

Put the operation step to do something in a brace and give the brace a name. When you want to use this step in the future, you only need to call it by this name. This way of solving problems is the method.

3.1 Method declaration

修饰符 返回值类型 方法名(参数列表){
	方法的功能;
	return 返回的数据;
}

解释:
	修饰符:目前来说修饰符先默认为public static(后面会有改变)
	返回值类型:让做一件事,那么肯定会有相应的结果返回。而返回值类型就是返回的结果的类型(并不是结果,而只是结果对应的类型)。
	方法名:就是一个变量名,见名知意即可。但是要符合规范(第一个单词全部小写,从第二个单词开始首字母大写)
	参数列表:让一个方法做一件事,必须要给相应的资源,这个参数列表就是方法完成功能所需要的资源。(这个只是说要什么资源,但并不提供,真正的资源是由外界方法的调用者传入的)。如果有多个参数的话,那么中间需要用英文状态的逗号分隔
	{}:方法体,里面放的是方法完成某个功能的步骤
	return:用来返回方法完成功能后产生的数据。如果有数据产生,那么就是return 数据;此数据的类型一定要和方法的返回值类型一致(如果没有数据产生,那么就直接return ; 此时return;可以省略)

3.2 The position of the method in the code

//1.不能在此定义
public class Demo2 {
    
    

    //2.可以定义
    public static void main(String[] args) {
    
    

        //3.不能在此定义
    }

    //4.可以定义
}
//5.不能在此定义

3.3 Notes on the method

1. 方法之间是平级的,前后顺序无所谓
2. 方法不能嵌套定义(即在一个方法中声明另一个方法)

3.4 Use of methods

① Two clear before writing method

返回值类型
参数列表

② Formal parameters and actual parameters

形式参数:定义在方法上的。作用相当于占了一些位置,然后外界会根据这个所占位置的类型传入对应类型的数据。(也就说形式参数只是定义了方法需要什么类型的数据,但是并不提供数据)
实际参数:在方法调用的时候传入的参数。作用就是提供实际的数据。这个传入的实际数据的类型以及位置一定要和形式参数保持一致。

3.4 method call

方法调用的格式:
	方法名(...);

注意:
	1. 方法不调用不执行。
	2. 如果方法需要参数,那么小括号中就放入实际参数;如果方法不需要参数,那么小括号直接留空。
	3. 调用方法都是直接或间接在main方法中调用的。

① Calls with clear return value methods

输出调用   如:System.out.print(sum(3, 5));
赋值调用   如:int result = sum(3, 5);  System.out.print(result);
练习:
求三个整数的和
判断两个整数是否相等
/*
    需求:
        1. 求三个整数的和
        2. 判断两个整数是否相等
 */
public class Test {
    
    

    /*
        主方法
		
		自己定义的方法都需要在主方法中调用
     */
    public static void main(String[] args) {
    
    

        int result = sum(3, 5, 5);
        System.out.println("三个整数的和为:" + result);

        boolean b = isEquals(5, 5);
        System.out.println("是否相等:" + b);

    }

    /*
            需求1.求三个整数的和

            两个明确:
                返回值类型:  int
                参数列表: int a, int b, int c
     */
    public static int sum(int a, int b, int c){
    
    

//        int result = a + b + c;
//        return result;

        return a + b + c;
    }

    /*
            需求2. 判断两个整数是否相等

            两个明确:
                返回值类型:boolean
                参数列表: int a, int b
     */
    public static boolean isEquals(int a, int b){
    
    

//        if(a == b){
    
    
//            return true;
//        }else{
    
    
//            return false;
//        }

        return a == b;
    }

}

② There is no clear return value method call

没有明确返回值方法的声明:
public static void 方法名(参数列表){
	方法的内容;
	return ;  //此处return可以省略
}

注意:
	没有明确返回值的方法的return可以省略
调用方式:
	 直接调用
练习:
	1. 用方法打印出如下图形
	
	春眠不觉晓
	-----
	处处闻啼鸟
	==========
	夜来风雨声
	<<<<<<<<<<<<<<<
	花落知道少
	>>>>>>>>>>>>>>>>>>>>
	
	2. 写一个方法求两个整数的和,并用键盘录入改进
	3. 写一个方法可以打印指定行数和列数的星星,并用键盘录入改进
/*
	需求1. 写一个方法打印出如下内容
	
	春眠不觉晓
	-----
	处处闻啼鸟
	==========
	夜来风雨声
	<<<<<<<<<<<<<<<
	花落知道少
	>>>>>>>>>>>>>>>>>>>>
*/
public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("春眠不觉晓");
        printString("-", 5);
        System.out.println("处处闻啼鸟");
        printString("=", 6);
        System.out.println("夜来风雨声");
        printString("<", 2);
        System.out.println("花落知道少");
        printString(">", 3);
    }

    public static void printString(String s, int len) {
    
    

        for (int i = 0; i < len; i++) {
    
    
            System.out.print(s);
        }
        System.out.println();
    }
}
import java.util.Scanner;

/*
    需求2. 写一个方法求两个整数的和,并用键盘录入改进
 */
public class Test3 {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        System.out.println("请录入第一个整数:");
        int a = sc.nextInt();
        System.out.println("请录入第二个整数:");
        int b = sc.nextInt();

        int result = sum(a, b);
        System.out.println(result);
    }

    /*
        两个明确:
            返回值类型: int
            参数列表: int a, int b
     */
    public static int sum(int a, int b){
    
    

        return a + b;
    }

}

import java.util.Scanner;
/*
    需求3. 写一个方法可以打印指定行数和列数的星星,并用键盘录入改进
 */
public class Test4 {
    
    

    public static void main(String[] args) {
    
    

        Scanner sc = new Scanner(System.in);

        System.out.println("请录入行数:");
        int row = sc.nextInt();
        System.out.println("请录入列数:");
        int column = sc.nextInt();

        printStar(row, column);
    }

    /*
        两个明确:
            返回值类型:void
            参数列表:int row, int column
     */
    public static void printStar(int row, int column){
    
    

        for(int i = 0; i < row; i++){
    
    
            for(int j = 0; j < column; j++){
    
    

                System.out.print("*");
            }
            System.out.println();
        }
    }

}

Guess you like

Origin blog.csdn.net/ChangeNone/article/details/112250628