java复习笔记3

第三章   流程控制与数组

一、程序的三种结构

基本上所有编程语言中,程序都有三种结构:顺序结构、分支(选择)结构、循环结构Java也不例外。

顺序结构:自上而下的逐行执行代码。

选择结构:根据逻辑判断,有选择的执行其中某部分代码。

循环结构:根据逻辑判断,重复执行某个代码块。

 

二、顺序结构程序设计

一般情况下代码块中没有用ifswitchforwhile等的,都是顺序结构

       案例:

importjava.util.*;

public classTest2{

        public static void main(String[] args){

                 int x,gw,sw,bw,sum;

                 Scanner s = newScanner(System.in);

                 System.out.print("请输入一个三位整数:");

                 x = s.nextInt();

                 gw = x%10;                // 123%10=3

                 sw = (x%100)/10;        // 123%100=23  23/10=2

                 bw = x/100;                // 123/100=1

                 sum = gw+sw+bw;

                 System.out.println(x+"的各位数和="+sum);

         }

}

 

三、分支(选择)结构程序设计

if分支

          ◆单分支

                    语法:

                            if(表达式){

                                     代码块;

}

 

说明:

        如果表达式的值为true,则执行代码块,否则不执行。

 

案例:

public class Test1{

        public static voidmain(String[] args){

                 int a = 80;

                 if(a>=60){

                         System.out.println("恭喜你,成绩合格!");

                 }

        }

}

 

◆二路分支(双分支)

                    语法:

                 if(表达式){

                         代码块1;

}else{

        代码块2

}

 

说明:

如果表达式的值为true,则执行代码块1

如果表达式的值为false,则执行代码块2.

 

案例:

publicclass Test2{

        public static void main(String[] args){

                 int a = 50;

                 if(a>=60){

                         System.out.println("恭喜你,成绩合格!");

                 }else{

                         System.out.println("很抱歉,准备补考!");

                 }

        }

}

补充:三目运算符

表达式1?表达式2:表达式3;

案例:

publicclass Test1{

        public static void main(String[] args){

                 int a = 40;

                 int b = 20;

                 int max;

                 /*

                 if(a>b){

                         max = a;

                 }else{

                         max = b;

                 }

                 */

                 max = a>b?a:b;

                 System.out.println(max);

        }

}

◆多路分支

语法:

                 if(表达式1){

                         代码块1;

}else if(表达式2){

        代码块2

}else if(表达式3){

        代码块3

}

…….

…….//中间可以有n else if

……..

else{//最后这个else可有可无

        代码块n;

}

 

 

说明:

如果表达式1的值为true,则执行代码块1,如果表达1的值为false,则判断表达式2

如果表达式2的值为true,则执行代码块2,如果表达式2的值为false,则判断表达式3

如果表达式3的值为true,则执行代码块3,如果表达式3的值为false,则判断表达式4

                                  …….

                                  …….

                                  如果都不成立,则进入else中执行代码块n

案例:

publicclass Test3{

    public static void main(String[] args){

             int a = 75;

             if(a>=80){

                     System.out.println("成绩优秀,继续保持!");

             }else if(a>=70){

                     System.out.println("成绩良好,再进一步!");

             }else if(a>=60){

                     System.out.println("成绩合格,仍须努力");

             }else{

                     System.out.println("成绩不合格,好好加油!");

             }

    }

}

switch分支

          switch是多分支语句

 

语法:

switch(表达式){

                 case 1:代码块1

                                   break;     //break不是必须的,根据情况而定

                 case 2:代码块2

                                   break;

                 ……

                 …...

                 ……

                 default:代码块n; //default可有可无

}

 

注意: JDK6 中表达式的类型只能为 byte short int char

         JDK7中表达式的类型只能为 byte short int char String

说明:

            根据switch后面表达式的值,去依次和case后面的值匹配,遇到匹配的就从这个case进入,执行后面的代码块,不再判断后面的casedefault标签的值是否匹配,他会一直往下执行,直到遇到break才会停止。

            如果没有遇到匹配的,就执行break后面的代码块。

案例:

   //从键盘输入星期几,输出对应的英文单词

                         importjava.util.*;

public classTest1{

        public static void main(String[] args){

                 int week;

                 Scanner s = newScanner(System.in);

                 System.out.print("请输入星期几(1--7):");

                 week = s.nextInt();

                 switch(week){

                         case 1:

System.out.println("星期一   Monday");

                                  break;

                         case 2:

                                  System.out.println("星期二    Tuesday");

                                  break;

                         case 3:

                                  System.out.println("星期三    Wednesday");

                                  break;

                         case 4:

                                  System.out.println("星期四    Thursday");

                                  break;

                         case 5:

                                  System.out.println("星期五    Friday");

                                  break;

                         case 6:

                                  System.out.println("星期六    Saturday");

                                  break;

                         case 7:

                                  System.out.println("星期天    Sunday");

                                  break;

                         default:

                                  System.out.println("你的输入有误,只有1--7");

                 }

        }

}

 

//从键盘输入月份,输出对应的季节

importjava.util.*;

public classTest2{

        public static void main(String[] args){

                 int month;

                 Scanner s = newScanner(System.in);

                 System.out.print("请输入月份:");

                 month = s.nextInt();

                 switch(month){

                         case 12:

                         case 1:

                         case 2:

                                  System.out.println("白白皑皑!");

                                  break;

                         case 3:

                         case 4:

                         case 5:

                                  System.out.println("春暖花开!");

                                  break;

                         case 6:

                         case 7:

                         case 8:

                                  System.out.println("夏日炎炎!");

                                  break;

                         case 9:

                         case 10:

                         case 11:

                                  System.out.println("秋高气爽!");

                                  break;

                         default:

                                  System.out.println("你的输入有误,只有1--12月");

                 }

        }

}

 

 

四、循环结构程序设计

while循环

语法:

        while(循环条件){

                 代码块/循环体;

}

 

说明:

首先判断循环条件是否为true,如果为true则进入循环体执行,执行完成后自动回到前面再次判断循环条件,如果为true则进入循环体执行,执行完成后又回到前面再次判断,依次循环,直到循环条件为false则循环结束。

案例:

        输出1—100的数据

public class Test1{

        publicstatic void main(String[] args){

                 inta = 1;

                 while(a<=100){

                         System.out.println(a);

                         a++;

                 }

        }

}

 

求出1+2+3+……+100=?

public class Test2{

        publicstatic void main(String[] args){

                 inti = 1;

                 intsum = 0;

                 while(i<=100){

                         sum= sum+i;

                         i++;

                 }

                 System.out.println(sum);

        0}

}

 

 

do…while循环

语法:

        do{

                 代码块/循环体;

}while(循环条件);

说明:

首先进入循环体执行,执行完成后判断循环条件是否成立,如果条件成立回到前面再次执行循环体,执行完成又判断循环条件是否处理,如果条件成立回到前面执行循环体,依次循环,直到循环条件不成立,则循环截止。

案例:

输出1—100的数据

public class Test3{

        public static voidmain(String[] args){

                 int i = 1;

                 do{

                         System.out.println(i);

                         i++;

                 }while(i<=100);

        }

}

 

求出1+2+3+……+100=?

public class Test3{

        publicstatic void main(String[] args){

                 inti = 1;

                 intsum = 0;

                 do{

                         sum=sum+i;

                         i++;

                 }while(i<=100);

                 System.out.println("1+2+3+...+100="+sum);

        }

}

 

for循环

语法:

        for(表达式1;表达式2;表达式3){

                 循环体;

}

说明:

        表达式1:初始化变量

        表达式2:循环条件

        表达式3:循环增量

 

        首先执行表达式1 –》判断表达式2,如果表达式2成立,则执行循环体,循环体执行完成后,执行表达式3。执行完3,又判断表达式2是否成立….依次循环,直到表达式2不成立循环截止。

案例:

输出1—100的数据

public class Test1{

        public static voidmain(String[] args){

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

                         System.out.println(i);

                 }

        }

}

 

求出1+2+3+……+100=?

public class Test1{

        publicstatic void main(String[] args){

                 intsum = 0;

                 for(inti = 1;i<=100;i++){

                         sum=sum+i;

                 }

                 System.out.println("1+2+3+...+100="+sum);

        }

}

 

while do…while的区别

public class Test1{

        publicstatic void main(String[] args){

                 inti = 101;

                 while(i<=100){

                         System.out.println(i);

                         i++;

                 }

        }

}

运行结果:

        没有输出

 

 

public class Test1{

        publicstatic void main(String[] args){

                 inti = 101;

                 do{

                         System.out.println(i);

                         i++;

                 }while(i<=100);

        }

}

运行结果:

        101

 

总结:while是先判断,再执行  do…while先执行,后判断

        do…while至少执行一次。

 

五、分支、循环嵌套

在实际应用中,往往单独使用分支结构或则循环结构不能解决所有问题,很多情况下面需要使用分支和循环结构的嵌套。

★分支中嵌套分支结构

案例一: 从键盘输入3个整数,输出其中最大的一个

import java.util.*;

public class Test1{

   public static void main(String[] args){

            int a,b,c;

            Scanner s = new Scanner(System.in);

            System.out.print("请输入一个整数:");

            a = s.nextInt();

            System.out.print("请输入一个整数:");

            b = s.nextInt();

            System.out.print("请输入一个整数:");

            c = s.nextInt();

            if(a>b){

                    if(a>c){

                            System.out.println(a);

                    }else{

                            System.out.println(c);

                    }

            }else{

                    if(b>c){

                            System.out.println(b);

                    }else{

                            System.out.println(c);

                    }

            }

   }

}

 

★循环中嵌套分支结构

案例一: 输出1--100中能被3整除的数

public class Test1{

        publicstatic void main(String[] args){

                 for(inti=1;i<=100;i++){

                         if(i%3==0){

                                  System.out.println(i);

                         }

                 }

        }

}

 

案例二:输出1--100中能被3整除,又能被5整除的数

public class Test1{

    publicstatic void main(String[] args){

             for(inti=1;i<=100;i++){//做1--100的循环

                     if(i%3==0&&i%5==0){//做判断 能被3整除

                             System.out.println(i);

                     }

             }

    }

}


}

 

案例五:求出1—100中能被3整除的数据的和

public class Test1{

    publicstatic void main(String[] args){

             intsum=0;

             for(inti = 1;i<=100;i++){

                     if(i%3==0){

                             sum= sum+i;

                     }

             }

             System.out.println(sum);

    }

}

 

★循环中嵌套循环结构

案例一:

publicclass Test1{

        public static void main(String[] args){

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

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

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

                         }

                 }

        }

}

 

案例二:编写程序 输出如下如下

public class Test1{

        publicstatic void main(String[] args){

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

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

                                  System.out.print("*");

                         }

                         System.out.println();

                 }

        }

}

 

扩展:打印九九乘法表

public class Test1{

        publicstatic void main(String[] args){

                 for(inti=1;i<=9;i++){

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

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

                         }

                         System.out.println();

                 }

        }

}

 

案例三:编写程序 输出如下如下

public class Test1{

        publicstatic void main(String[] args){

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

                         for(intj=1;j<=6-i;j++){

                                  System.out.print("*");

                         }

                         System.out.println();

                 }

        }

}

 

案例四:编写程序 输出如下图形

 

public class Test1{

        public static void main(String[] args){

                 //上半部分

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

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

                                  System.out.print("*");

                         }

                         System.out.println();

                 }

                 //下半部分

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

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

                                  System.out.print("*");

                         }

                         System.out.println();

                 }

        }

}

 

案例五: 编写程序 输出如下图形

public class Test1{

        public static void main(String[] args){

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

                         //空格

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

                                  System.out.print(" ");

                         }

                         //*号

                         for(int k=1;k<=2*i-1;k++){

                                  System.out.print("*");

                         }

                         //换行

                         System.out.println();

                 }

        }

}

 

案例六: 编写程序 输出如下图形

public class Test1{

        public static void main(String[] args){

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

                         //空格

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

                                  System.out.print(" ");

                         }

                         //*号

                         for(int k=1;k<=11-2*i;k++){

                                  System.out.print("*");

                         }

                         //换行

                         System.out.println();

                 }

        }

}

 

案例七: 编写程序 输出如下图形

    

public class Test1{

        public static void main(String[] args){

                 //上半部分

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

                         //空格

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

                                  System.out.print(" ");

                         }

                         //*号

                         for(int k=1;k<=2*i-1;k++){

                                  System.out.print("*");

                         }

                         //换行

                         System.out.println();

                 }

                 //下半部分

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

                         //空格

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

                                  System.out.print(" ");

                         }

                         //*号

                         for(int k=1;k<=9-2*i;k++){

                                  System.out.print("*");

                         }

                         //换行

                         System.out.println();

                 }

        }

}

 

 

六、循环的控制 break、continue

break语句除了可以用在switch语句中,还可以用在循环体中。在循环体中遇见break语句,立即结束循环,跳到循环外,执行循环结构后面的语句break语句用于结束整个循环。

continue语句用于结束本次循环,即在循环体中遇见continue语句,则循环体中continue语句后面的语句不会执行,接着进行下一次循环的判定。

 

案例一:

public class Test1{

        public static void main(String[] args){

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

                         if(i==3){

                                  break;

                         }

                         System.out.println(i);

                 }

        }

}

运行结果:

 

案例二:

public class Test1{

        public static void main(String[] args){

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

                         if(i==3){

                                  continue;

                         }

                         System.out.println(i);

                 }

        }

}

 

七、数组、一维数组

 ------------------可参考java复习笔记2中3(2)---------------------------

初始值:

byteshortintlong------------------>  0

floatdouble              -------------------->0.0

char                           -------------------->‘\u0000’

boolean                      -------------------->false

 

引用类型:数组、类、接口-------------------->  null

★访问数组元素

                         数组中每个元素都有一个下标,下标从0开始:访问数组元素可以根据数组下标。

                         eg:

                                  int[]a = {55,66,85,32,75};

                                  55------------ a[0]

                                  66------------ a[1]

                                  85------------ a[2]

                                  32------------ a[3]

                                  75------------ a[4]

 

                         ◆单独输出某个元素

                                  System.out.println(a[2]);

 

                         ◆输出所有元素(使用循环)

publicclass Test1{

    public static void main(String[] args){

             int[] a = {55,23,89,64,85};

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

                     System.out.println(a[i]);

             }

    }

}

 

思考:如果数组很长,怎么办?是否需要自己一个一个数出数组元素的个数,再确定循环次数?不用,数组有一个属性length记录了数组的长度,可以利用length属性来确定循环次数。

publicclass Test1{

    public static void main(String[] args){

             int[] a = {55,23,89,64,85};

                                          System.out.println(a.length);//输出 5

    }

}

 

//对前面循环输出数组元素改进

publicclass Test1{

    public static void main(String[] args){

             int[] a = {55,23,89,64,85};

             for(int i=0;i<=a.length-1;i++){

                     System.out.println(a[i]);

             }

    }

}

 

//在JDK1.5之后 循环输出数组也可以只是用foreach(foreach在JDK1.5开始引入)

publicclass Test1{

    public static void main(String[] args){

             int[] a = {55,23,89,64,85};

             //for循环输出

             for(int i=0;i<=a.length-1;i++){

                     System.out.println(a[i]);

             }

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

             //foreach输出

             for(int x:a){

                     System.out.println(x);

             }

    }

}

 

★数组的应用

       ◆求多个数据的和

public class Test1{

    publicstatic void main(String[] args){

             int[]a = {55,23,89,64,85};

             intsum=0;

             for(inti=0;i<=a.length-1;i++){

                     sum= sum+a[i];

             }

             System.out.println("总分="+sum);

    }

}

 

        ◆求多个数据的平均值

public class Test1{

    publicstatic void main(String[] args){

             int[]a = {55,23,89,64,85};

             intsum=0;

             doubleavg;

             for(inti=0;i<=a.length-1;i++){

                     sum= sum+a[i];

             }

             avg= sum/(a.length*1.0);

             System.out.println("平均分="+avg);

    }

}

 

        ◆求多个数据中的最大值、最小值

public class Test1{

    publicstatic void main(String[] args){

             int[]a = {55,23,89,64,85};

             intmax = a[0];//假设第一个最大

             for(inti=0;i<=a.length-1;i++){

                     if(a[i]>max){

                             max= a[i];

                     }

             }

             System.out.println("最大值="+max);

    }

}

 

public class Test1{

    publicstatic void main(String[] args){

             int[]a = {55,23,89,64,85};

             intmin = a[0];//假设第一个最小

             for(inti=0;i<=a.length-1;i++){

                     if(a[i]

                             min= a[i];

                     }

             }

             System.out.println("最小值="+min);

    }

}

 

◆统计及格和不及格人数

public class Test1{

    publicstatic void main(String[] args){

             intcount1=0;//及格人数

             intcount2=0;//不及格人数

             int[]a = {55,23,89,64,85};

             for(inti=0;i<=a.length-1;i++){

                     if(a[i]>=60){

                             count1++;

                     }else{

                             count2++;

                     }

             }

             System.out.println("及格人数="+count1+",不及格人数="+count2);

    }

}

 

◆查找是否有xx分的同学

import java.util.*;

public class Test1{

    publicstatic void main(String[] args){

             Scanners = new Scanner(System.in);

             intx ;

             booleanf = false;//f表示是否存在要查找的值

             int[]a = {55,23,89,64,85};

             System.out.print("请输入你要查找的分数:");

             x= s.nextInt();

             for(inti=0;i<=a.length-1;i++){

                     if(a[i]==x){

                             f= true;

                     }

             }

             if(f){

                     System.out.println("有!");

             }else{

                     System.out.println("无!");

             }

    }

}

 

◆对分数从高到低排序、从低到高排序(冒泡排序)

    //从大到小

public class Test1{

    publicstatic void main(String[] args){

             int[]a = {55,23,89,64,85};

             //从大到小 冒泡排序

             for(inti = 0;i

                     for(intj=0;j

                             if(a[j]

                                      //交换

                                      intx = a[j];

                                      a[j]= a[j+1];

                                      a[j+1]= x;

                             }

                     }

             }

             //输出数组

             for(inti=0;i<=a.length-1;i++){

                     System.out.print(a[i]+"  ");

             }

             System.out.println();

    }

}

 

 

//从小到大

public class Test1{

    publicstatic void main(String[] args){

             int[]a = {55,23,89,64,85};

             //从小到大 冒泡排序

             for(inti = 0;i

                     for(int j=0;j

                             if(a[j]>a[j+1]){

                                      //交换

                                      intx = a[j];

                                      a[j]= a[j+1];

                                      a[j+1]= x;

                             }

                     }

             }

             //输出数组

             for(inti=0;i<=a.length-1;i++){

                     System.out.print(a[i]+"  ");

             }

             System.out.println();

    }

}

 

 

★数组内存分析

:基本数据类型变量值,引用类型变量的地址值

:引用类型的真实值

案例一:

public class TestArray1{

publicstatic void main(String[] args){

int[]a = {89,75,64,32,78};

System.out.println(a[3]);

}

}

案例二:

public class TestArray1{

    publicstatic void main(String[] args){

             int[]a = {89,75,64,32,78};

             int[]b = {55,66,77,88};

             System.out.println(a[2]);

             System.out.println(b[2]);

    }

}

案例三:

public class TestArray1{

    publicstatic void main(String[] args){

             int[]a = {89,75,64,32,78};

             int[]b = a;

             System.out.println(a[3]);//32

             System.out.println(b[3]);//32

             a[1]= 100;

             System.out.println(a[1]);//100

             System.out.println(b[1]);//100

             b[3]= 50;

             System.out.println(a[3]);//50

             System.out.println(b[3]);//50

    }

}


 

八、二维数组、多维数组

●二维数组:

二维数组就是存放一维数组的数组,一个二维数组可以看做多个一维数组。

 

               inta = 10;

                 int[] b = {10,20,30};//b看做多个a的集合 可以把 b[0]a等价

                 int[][] c ={{10,20,30},{5,6,7}};//c可以看做b的集合可以把c[0]b等价 c[0][0]a等价

                         //         c[0]     c[1]  一维数组

                         //      c[0][0]         

★数组的定义

                类型[][]数组名;//常用       int[][] a;

                 类型数组名[][];// int a[][]

 

★数组的初始化

        ◆静态初始化

                 初始化时由程序员显示的指定每个数组元素的初始值,由系统决定数组的长度。

                 eg1:

                         int[][]a;

                         a= new int[][]{{1,2,3},{4,5,6}};

 

                 eg2:

                         int[][]a = new int[][]{{1,2,3},{4,5,6}};

                

                 eg3:

                         int[][]a = {{1,2,3},{4,5,6}};//简化写法

 

                 eg4://错误写法

                         int[][]a;

                         a= {{1,2,3},{4,5,6}};

 

        ◆动态初始化

初始化时程序员只指定数组的长度,而不指定数组中元素的值,系统自动给数组分配初始值。

eg1:

    int[][] a;

    a = new int[2][3]; //2代表a中有两个一维数组  3代表一维数组中有3个值

 

eg2:

    int[][] a = new int[2][3];

 

★访问数组元素

 

◆访问单个元素

public class Test1{

        publicstatic void main(String[] args){

                 inta = 5;

                 int[]b = {1,2,3,4,5};

                 int[][]c = {{1,2,3},{4,5,6}};

                 System.out.println(a);//5

                 System.out.println(b[2]);//3      b[2]看做和a一样

                 System.out.println(c[1][1]);//5    c[2][2]看做和b[2]一样  c[1]看做和b一样

        }

}

 

◆访问所有元素

publicclass Test1{

        public static void main(String[] args){

                 int[][] c ={{1,2,3},{4,5,6},{7,8,9},{10,11,23}};

                 //输出c 

                                  for(inti=1;i<=c.length-1;i++){

                         for(intj=0;j<=c[i].length-1;j++){

                                  System.out.print(c[i][j]+"  ");

                         }

                         System.out.println();

                 }

        }

}

 

●多维数组:

          Java中没有多维数组,所有的数组都可以看做一维数组。

public classTest1{

   public static void main(String[] args){

            int a = 10;                           //变量a

            int[] b = {10,20,30};                    //一维数组b  b中保存的是一个一个的变量

            int[][] c = {                                      //二维数组c  c中保存的是一个一个的一维数组

                            {10,20},            //c[0]

                            {30,40},            //c[1]

                            {40,50}                      //c[2]

                        };

            int[][][] d = {                               //三维数组d  d中保存的是一个一个的二维数组

                            {{10,20},{30,40}},    //d[0]

                            {{60,70},{80,90}}     //d[1]

                           };

 

            System.out.println(a);//10

            System.out.println(b[0]);//10

            System.out.println(c[0][0]);//10

            System.out.println(c[2][1]);//50

            System.out.println(d[0][0][0]);//10

            System.out.println(d[1][0][0]);//60

            System.out.println(d[1][1][1]);//90

   }

}


你可能感兴趣的:(初级java)