java learning summary _04

 

1, a cyclic structure

2, method

Loop structure

Outline

1, for some, the same or similar statement to be executed repeatedly using a certain format to be done to simplify the code.

2, achieved statement:

  • [used] for statement

  • while statement [common]

  • do ... while statement

for statement

1, format

for (initialization statement 1; 2 cycle conditional expression; increment variable initialization 3) {

Loop statement; 4

}

It happens when executing the simulation :( from the left)

1

2 false for the end of the statement

2true -4-3-2true-4-3-2true-4-3-2false- end

2, the implementation process:

  • 1, an initialization statement
  • 2, calculation loop conditional expression
  • 3, if the calculation is false, the for loop ends
  • 4, if the calculation is true, the block is executed statements
  • 5, an initialization variable increment
  • 6, return to step 2

3, Description:

  • 1, initialization statement, declare a variable for recording the number of cycles
  • 2, the conditional expression loop, loop control condition
  • 3, variable initialization increment, the variable initialization statement declared, changes, generally in the direction of circulation changes can not be executed
  • 4, the contents of the loop statement, the one that you want to repeat the

The sample code

Precautions for statement format
  • 1, initialization statement, declare a variable for recording the number of cycles, performed only once.
  • 2, the conditional expression must be a boolean operation result
  • 3, loop statements, prior to initialize a variable increment, execution
  • 4, variable initialization increment, may be incremented, may be decremented, step may be increased from a
  • 5, before the opening brace, do not have a semicolon, otherwise the cycle can not control for braces statement.
  • 6, loop statements, if only one sentence, then braces can be omitted. Recommended that all the time plus braces

And vice :( usage)

  • * For the three parameters through the bad requires attention to the problem,
    * a first initial statement, the initial data may be initially more variables,
    * determining a second expression can be a multi-use relational operators conditional connection,
    * of self-energizing three expressions: a plurality of simultaneously or may be incremented or expression modulo operation
    * meet the above three parameters required for each expression can be a

The sample code

class Demo_3 {

Notes // for statement

public static void main(String[] args) {

// initialize variables is performed only once

for (int i = 1, j = 6; i <j / * type must be boolean * /; i = i + 3, j = j - 1) / *; * / {

// If the loop statement only one statement, we can omit the braces

// loop statement

System.out.println (i + "" + j); // loop statement is executed before incrementing the variable initialization

}

}

}

Exercises for loop

Exercise 1

Digital Printing 1-5

Exercise 2

Digital Printing 5-1

Exercise 3

Calculation and 1 + 2 + 3 + ... + 100

Exercise 4

And calculating the even 1-100

Exercise 5

Enumerate all the "four-leaf number rose", and counts the number of the number of four-leaf Rose

The number of four-leaf roses: a four-digit number, each bit of digital and fourth power, that numbers alone

Nested loop

1, in a major cycle, the contents of each loop (loop statement) is a complex operation, the operation is repeated, requires a large loop statement, a small loop defined, called cycle nested ( when printing rectangular or other shaped pattern, if two layers are used for nesting, the outer rows represents the number of bad circulation, the inner loop indicates the number of columns )

2, format:

for (initialization statement 1 (1); 1 cycle of the conditional expression (2); original variable is incremented by one (3)) {

for (initialization statement 2 (4); Cycle 2 the conditional expression (5); original variable increment 2 (6)) {

Loop statements 7

}

}  

Analog statement running processes as follows:

1-> 2false end

1 -> 2true -> 4 -> 5 false end of the inner loop -> 3-> 2 false until the end of cycle 2

1 -> 2true -> 4 -> 5 true -> 7 -> 6 -> 5 true (Back 7) false-> 3 -> 2 (true) back 4, the end 2 is false

3, the implementation process

  • 1, sentence 1 Initialization
  • 2, the conditional expression 1 cycle, the calculation result is false, the end
  • 3, a circulation condition expression evaluates to true, initialization statement 2,
  • 4、计算循环条件表达式2的结果,false,内层循环结束,初始变量的自增1

   循环条件表达式1,true,初始化语句2,循环条件表达式2,true,循环体语句,初始变量自增2,循环条件表达式2,false,

代码示例

class Demo_2 {

public static void main(String[] args) {

//嵌套循环

/*

for(初始化语句1;循环条件表达式1;初始化变量自增1){

for(初始化语句2;循环条件表达式2;初始变量自增2){

循环体语句;

}

}

*/

for(int i = 1;i < 6 ;i++){//外层循环循环一次,内层循环是要执行完

for(int j = 1;j < 4;j++){

System.out.println("i = " + i +";j = " + j);

}

}

}

}

练习1

使用嵌套循环,打印四行五列星星矩形

*****

*****

*****

*****

提示:打印不换行,使用print方法,不要加ln

class Demo_7 {

public static void main(String[] args) {

/*

使用嵌套循环,打印四行五列星星矩形

*****

*****

*****

*****

提示:打印不换行,使用print方法,不要加ln

*/

//外层循环控制行数,内层循环控制列数

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

//内层循环控制列数

for(int j = 1;j < 6;j++){

System.out.print("*");

}

//换行

System.out.println();

}

}

}

练习2

使用嵌套循环,打印5行5列的直角三角形

*

**

***

****

class Demo_8 {

public static void main(String[] args) {

/*

使用嵌套循环,打印5行5列的直角三角形

*         1行 打印1列,内层循环1次         

**   2行 打印2列,内层循环2次

***   3行 打印3列,内层循环3次

****

*****

*/

for(int i = 1;i < 6;i++){

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

System.out.print("*");

}

System.out.println();

}

}

}

练习3

打印99乘法表

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

........................................

1*9=9 2*9=18 3*9=27 .......... 8*9=72 9*9=81

class Demo_9 {

public static void main(String[] args) {

/*

打印99乘法表

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

........................................

1*9=9 2*9=18 3*9=27 .......... 8*9=72 9*9=81

*/

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();

}

}

}

while语句

1、格式:

初始化语句

while(条件表达式) {

循环体语句;

初始化变量的自增;

}

2、执行流程:

  • 1、初始化语句
  • 2、计算条件表达式,如果为false,循环直接结束
  • 3、如果为true,执行循环体语句
  • 4、执行初始化变量的自增
  • 5、回到第二步

3、注意事项:

  • 1、条件表达式必须为boolean类型
  • 2、初始化变量的自增,不要忘记

代码示例

class Demo_10 {

public static void main(String[] args) {

//while语句

/*

初始化语句

while(条件表达式){

循环体语句;

初始变量的自增;

}

*/

int i = 1;

while(i < 6){

System.out.println("爱你三千遍");

//初始变量的自增

// i++;//i = i * 2;

System.out.println(i++);

}

}

}

练习

使用while循环,统计1-100范围内,有多少数字可以被7整除

class Demo_11 {

public static void main(String[] args) {

//使用while循环,统计1-100范围内,有多少数字可以被7整除

int i = 1;

//定义一个辅助变量用来统计被7整除的个数

int count = 0;

while(i <= 100){

//判断是否可以被7整数

if(i % 7 == 0){

count++;

}

//初始变量的自增

i++;

}

System.out.println(count);

}

}

do...while语句

1、格式:

初始化语句;

do {

循环体语句;

初始化变量的自增;

} while (循环条件表达式);

2、执行流程:

  • 1、初始化语句
  • 2、循环体语句
  • 3、初始化变量的自增
  • 4、计算循环条件表达式的值,如果为false,循环结束
  • 5、如果为true,回到第二步

3、注意事项:

  • 1、循环条件表达式必须是boolean类型
  • 2、while循环条件后面,要有一个分号

代码示例

class Demo_12 {

public static void main(String[] args) {

//do while 语句

//初始化变量

int i = 1;

do{

System.out.println("do while虚幻");

//初始化变量的自增

++i;

}while(i < 5);

//常用的循环语句,for语句,while语句

}

}

三种循环语句的区别for, while,do..while

1、do while语句和其他两种语句的区别:

  • do while语句至少可以执行一次,另外两种有可能一次都执行不了

2、while语句和for的区别:

  • 1、代码层面:while语句声明的初始化变量,在while结束之后,还能继续使用;for中声明的初始化变量,在for结束之后,就无法使用了。
  • 2、设计层面:while语句一般用于描述相对模糊的范围,其他的条件;for用于描述相对准确,知道循环次数的循环
死循环

1、死循环:无限循环。循环永远都不停止。

2、格式:

1、for语句的死循环:

  for ( ; ; ) {

循环体语句;

}

2、while语句的死循环:【常用】

  while (true) {

循环体语句;

}

注意:

//破坏循环的方法:使条件变为false
//方式:1.直接使判断表达式条件变为false 2.通过修改自增表达式(不一定使用a++这种),使判断表达式失效即为false 3.使用break,continue,return,system.exit(0)结束循环/方法/虚拟机

3、死循环的作用:

  • 1、在格式上使用了死循环,但是在循环体中,可能出现了需要循环结束的情况,准备了一些跳出、终止循环的跳转语句。
  • 2、在服务器的设计中,希望服务器永远给我们服务下去,所以需要使用死循环。

代码示例

import java.util.Scanner;

class Demo_14 {

public static void main(String[] args) {

//死循环

//for语句的死循环

/* for(;;){

System.out.println("死循环");

}

*/

//System.out.println("我能执行到吗,好好想想");

//while语句的死循环

//while (true){

// System.out.println(true);

// }

Scanner sc = new Scanner(System.in);

System.out.println("选择你心仪的战队");

while(true){

int a = sc.nextInt();

if(a == 1){

System.out.println("I like RNG");

break;

}else if(a == 2){

System.out.println("I like IG");

break;

}else if(a == 3){

System.out.println("I like FPX");

break;

}else if(a == 4){

System.out.println("I like SKT");

break;

}else if(a == 5){

System.out.println("I like DWG");

break;

}else{//不加else有可能一个语句体都不执行,加上后要执行一个语句体

System.out.println("你输入的战队不存在,请重新输入");

}}

}}

跳转语句

1、跳转语句:在循环的循环体语句中,结束循环,控制循环,使用的语句。

2、continue语句:

  • 结束本次循环,继续下次循环

3、break语句:

  • 结束当前层次的循环

4、return语句:

  • 结束当前的方法

5、System.exit(0);

  • 用于结束虚拟机

代码示例

class Demo_15 {

public static void main(String[] args) {

//跳转语句

for(int i = 1;i<=10;i++){

//continue

if(i == 5){

continue;//结束本次循环,继续下次循环

}

System.out.println(i);

}

System.out.println("----------==================");

// break语句

for(int i = 1;i<=10;i++){

if( i == 5){

break;//  把整个循环结束了

}

System.out.println(i);

}

System.out.println("------------------------------------");

//return

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

for(int j = 1;j < 8;j++){

if( j == 5){

return;//  直接把方法结束了

//break; //结束的是内层循环,外层循环不受影响

//System.exit(0);//把虚拟机结束了

}

System.out.println("i = "+i +";j="+j);

}}}}

方法概述:

1、具有某种特定功能的代码段

2、某段代码经常使用,所以使用大括号,将这段代码包起来,起个名字。以后就使用这个名字来代替这段代码。

3、好处:

  • 1、提高了代码的复用性
  • 2、提高了代码的封装性,大括号中的内容,其他调用者看不到也无法直接访问
  • 3、简化了软件设计的思维难度
方法的定义

1、方法定义的格式

修饰符 返回值类型 方法名称 (参数列表) {

方法体语句;

return语句;//使用void时,return后面不接东西,表示结束这个方法,其他返回值类型,return返回对应类型数据作为这个方法的一个结果输出

}

2、详细解释:

  • 1、修饰符:现在全都写成public static(静态方法)

  注:静态方法在访问本类的成员时,只允许访问静态成员(即静态成员变量和静态方法),而不允许访问实例成员变量和实例方法;实例方法则无此限制。

  • 2、返回值类型:方法具有功能,有可能会有一些产出,就需要将数据返回给调用者。调用者需要知道生产出来的数据的数据类型。(int double,float,char,String void等)
  • 3、方法名称:给这段代码起的名字。只要是一个合法的标识符即可。第一个单词的首字母小写,从第二个单词开始首字母大写。动词、动宾结构。(小驼峰写法)
  • 4、参数列表:这段代码要完成功能,可能会需要一些资源。在参数列表中,需要定义一些变量,内存中表示为容器,在调用本方法的时候,会由外界传入数据,存储到这些变量容器中。使用变量符号,表示那些将来可能传入的数据。
  • 5、方法体语句:真正要完成该方法功能的执行逻辑。
  • 6、return语句:最终生产出来的结果,要返回给调用者,使用return语句返回。如果没有任何生产内容,就可以写一个return;,用于表示方法结束。
方法的调用

1、格式:直接书写方法名称即可(在不使用面向对象编程的时候)

方法名称(实际参数);

2、方法调用的三种形式:

  • 1、直接调用:表示某些内容的执行,而没有生产结果的情况
  • 2、输出调用:方法的返回值,需要打印。如果这个结果只打印一次,不做其他的操作。
  • 3、赋值调用:方法的返回值,使用某个变量来接收。如果这个结果需要反复使用。

3、方法调用总体特点:

方法不调用,就不执行。

代码示例

class Demo_17 {

public static void main(String[] args) {

//方法的调用

//直接调用

method_1();

method_2(/*实际参数*/5,8);

//输出调用

method_3(1,2);//sum

System.out.println(method_3(3,4));

//赋值调用

int a = method_3(2,3);

System.out.println(a);

}

//无参数列表,无返回值

public static void method_1(){

System.out.println("method_1 方法被调用了");

}

//有参数列表的,无返回值

public static void method_2(/*定义诺干个变量*/int i ,int j){

System.out.println("method_2 方法被调用了"+ "i= "+i +";j="+ j );

}

//有参数列表,有返回值的

public static int method_3(/*定义诺干个变量*/int i ,int j){

System.out.println("method_3");

int sum = i + j;

return sum;

}

}

方法注意事项

1、方法定义:

  • 1、方法不能嵌套定义,方法都是定义在主方法的下面。
  • 2、方法的先后没有区别
  • 3、方法的定义是平级关系
  • 4、方法可以嵌套调用,甚至可以自己调用自己

2、参数列表:

  • 1、形式参数:在定义方法时使用,需要加上数据类型的参数,也就是对于变量进行声明。各个变量之间,使用逗号分隔。
  • 2、实际参数:在方法调用时使用,不能加上数据类型的参数,也就是对于变量进行赋值。各个实际参数之间,也使用逗号分隔。顺序必须和定义的方法的形式参数的顺序一致。

3、return语句:

  • 1、语句表达方法结束了;表示方法的产出内容。
  • 2、如果方法没有具体的返回内容,可以写成return; 此时的return语句可以省略。返回值类型必须写成void。
  • 3、如果方法有具体的返回内容,那么return后面就需要加上返回的那个数据。返回值类型必须和return语句后面的数据的数据类型一致。
  • 4、return语句返回数据,返回给调用者。谁来调用当前方法,当前方法就将数据返回给谁。
方法在内存中的理解:main方法运行时,创建main方法内存块,main方法调用到test_1(),test_1()创建内存进栈,test_1()运行调用test_2(),test_2()创建内存空间同时进栈,当test_2()运行完所有代码,this出栈,此时test_1()运行完最后的代码,也this出栈,最后main代码运行完也,this出栈,程序结束,栈空

wps1

方法的重载

1、重载:Overload,超载

2、方法的重载:(只有参数的数据类型,个数,参数的位置这三个改变,才算方法重载)

  • 在同一个类中,方法名相同,参数列表不同,与返回值类型无关

3、说明:

  • 1、在同一个类中。不同无关类中,不可能发生方法的重载的。
  • 2、方法名称相同:一模一样。
  • 3、参数列表不同:参数类型不同;参数个数不同;参数类型的顺序不同;
  • 参数的名称不同(不算)
  • 4、与返回值类型无关。方法是否是重载,与返回值类型没有关系。
  • 如果方法的名称和参数列表都一样,即使返回值类型不同,也不算重载,属于方法 的重复定义。

4、方法重载的好处:

  • 1、没有重载的时候,相同逻辑相同功能的代码,需要定义很多个方法名称。调用者在记忆方法名称的时候,就有很大的负担。
  • 2、有重载之后,相同功能逻辑的方法,只需要记忆一个名字。

5、当前学习过的常见重载:

println方法,任意类型的数据,都可以放到这个方法中打印

在System.out类型中,将各种数据类型的打印方法,都定义出来了,都起了println方法的名称

代码示例

class Demo_20 {

public static void main(String[] args) {

//方法的重载

int a = 1;

int b = 2;

System.out.println(sum(a,b));

double m = 1.2;

double n = 1.3;

System.out.println(sum(1.2,1.3));

//重载

System.out.println(1);//PrintStream

System.out.println(1.2);

System.out.println("asdf");

}

//在同一个类中,方法名字一样,参数列表不同(参数的数据类型不一样,参数的顺序不一样,参数的个数不一样)与返回值类型无关

public static int sum(int a,int b){

return a + b;

}

public static double sum(double a,double b){

return a + b;

}

/*public static double sum(double aa,double bb){

return aa + bb;不是方法的重载

}*/

public static double sum(double a,int b){

return a + b;

}

public static double sum(int a,double b){

return a + b;

}

public static int sum(int a,int b,int c){

return a + b + c;

}

}

方法练习

练习1

定义一个方法getMax,可以获取两个整数的较大值

在主方法中,键盘录入两个整数,调用方法获取两者最大值

练习2

定义一个方法isEqual,可以判断两个小数是否相等

在主方法中,键盘录入两个整数,自动提升为小数,调用方法比较两者是否相等

练习3

定义一个方法,可以打印指定行数和列数的矩形

在主方法中,键盘录入两个整数,作为行数和列数,调用方法打印对应规模的矩形

Guess you like

Origin www.cnblogs.com/pwj-StudyCode/p/11774044.html