黑马程序员 Java自学总结三 Java语句

------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------

总结内容来源于黑马毕老师的java基础教学视频


Java语句有3中结构:判断结构、选择结构、循环结构.

一 判断结构---if 语句

格式(1)    if (条件表达式)

          {执行语句;}

格式(2)    if (条件表达式)

            {执行语句;}

          else 

          {执行语句;}

格式(3)    if (条件表达式)                      

              {执行语句1}

          else  if

              {执行语句2}

          else  if

              {执行语句3}

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

          else

              {执行语句;}

[java]  view plain copy
  1. //3,4,5月为春季.6,7,8为夏季.9,10,11为秋季.12,1,2为冬季  
  2. class Ifdemo  
  3. {  
  4.      public static void main(String[] args)  
  5.      {  
  6.           int mon = 4;  
  7.          if (6>mon&&mon>2)  
  8.          {  
  9.                System.out.println("春季");           
  10.          }  
  11.           else if (mon>5&&mon<9)  
  12.           {  
  13.                System.out.println("夏季");  
  14.           }  
  15.           else if (mon>8&&mon<12)  
  16.           {  
  17.                System.out.println("秋季");  
  18.           }  
  19.           else if (mon==1||mon==2||mon==12)  
  20.           {  
  21.                System.out.println("冬季");  
  22.           }  
  23.           else  
  24.                System.out.println("你输入的是什么啊?");          
  25.      }  
  26. }  


二 选择结构---switch 语句 

格式:switch(表达式)

     {

          case 变量可能值: 

          执行语句一;

          break;

          case 变量可能值: 

          执行语句二;

          break;

           case 变量可能值: 

            执行语句三;

            break;

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

           default :

             执行语句;

      }

[java]  view plain copy
  1. class  SwitchTest  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.           int m = 14;  
  6.           switch (m)  
  7.           {  
  8.                default:  
  9.                     System.out.println("你输入的无效!");  
  10.                     break;  
  11.                case 3 :  
  12.                case 4 :  
  13.                case 5 :  
  14.                     System.out.println(m+"春季");  
  15.                     break;  
  16.   
  17.                case 6 :  
  18.                case 7 :  
  19.                case 8 :  
  20.                     System.out.println(m+"夏季");  
  21.                     break;  
  22.   
  23.                case 9 :  
  24.                case 10 :  
  25.                case 11 :  
  26.                     System.out.println(m+"秋季");  
  27.                     break;  
  28.   
  29.                case 12 :  
  30.                case 1 :  
  31.                case 2 :  
  32.                     System.out.println(m+"冬季");  
  33.                     break;           
  34.           }            
  35.      }  
  36. }  

switch语句特点:

1. 选择的类型只有四种:byteintshortchar 

2. casedefault之间没有顺序,先执行第一个case,没有匹配的case,执行default

3. 结束switch语句有两种情况:一是遇到break,二是执行到switch语句结束;

4. 如果匹配的case或者default没有对应的break,那么程序会继续向下执行可以执行到的语句,直到遇到break或者switch语句结束。

if语句和switch语句的选择: 

如果判断具体数值不多,而且是switch可以选择的类型,建议使用switch,其效率稍高,其他情况,如判断区间,表达式结果为boolean类型判断,选择ifif使用范围广泛。

 

三 循环结构

while 循环 

格式:

定义初始化表达式;

while(条件表达式)

{

    循环体(执行语句)

}

 

[java]  view plain copy
  1. //循环语句while用法  
  2. class  WhileDemo  
  3. {  
  4.      public static void main(String[] args)  
  5.      {  
  6.           //定义一个变量  
  7.           int x = 1;  
  8.           //while循环语句(条件语句)  
  9.           while (x<=30)  
  10.           {                 
  11.                System.out.println("x="+x);  
  12.                x += 2;  
  13.           }            
  14.      }  
  15. }  

do while 循环

格式:

定义初始化表达式;

do

{

    循环体(执行语句)

}

while(条件表达式) 

[java]  view plain copy
  1. class  DoWhileDemo  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.           int x = 1;  
  6.           do  
  7.           {  
  8.                System.out.println("Do: x = "+x);  
  9.                x++;  
  10.           }  
  11.           while (x < 3);           
  12.      }  
  13. }  


whiledo while区别:

while:先判断后执行,do while:先执行一次,后判断是否继续执行.

总结:do while不管满足条件与否,至少执行一次.

for 循环

格式:

for(定义初始化表达式;循环条件表达式;循环后的操作表达式)

{

         循环体(执行语句)

}

[java]  view plain copy
  1. class ForDemo  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.              for (int x=1;x<3 ;x++)  
  6.             {  
  7.                     System.out.println("Hello World!");  
  8.             }  
  9.      }  
  10. }  


注意:

for while的区别是----for定义的条件控制变量在循环后就不存在了(优化了内存),  而while的条件控制变量循环后仍存在.

无限循环最简单的两种形式:for(;;) {} ,while(true){}


for嵌套循环语句

练习一 for循环嵌套语句打印直角三角形

*****

****

***

**

*

打印输出上面的形状,有多行多列,就用到了for嵌套循环语句

[java]  view plain copy
  1. class ForFor  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.           ;  
  6.            for (int x=1;x<=5 ;x++)  
  7.           {  
  8.                 
  9.                for (int y=x;y<=5 ;y++)  
  10.                {  
  11.                     System.out.print("*");  
  12.                }  
  13.                System.out.println("");  
  14.           }  
  15.       }  
  16. }  
练习二 for循环嵌套语句打印倒直角三角形

*

**

***

****

*****

[java]  view plain copy
  1. class ForFor  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.           for (int x=1;x<=5 ;x++ )  
  6.           {  
  7.                for (int y=1;y<=x ;y++ )  
  8.                {  
  9.                     System.out.print("*");  
  10.                }  
  11.                System.out.println();  
  12.            }  
  13.      }  
  14. }    
练习三 for循环嵌套语句打印倒数字直角三角形

1

12

123

1234

12345

[java]  view plain copy
  1. class ForFor  
  2. {  
  3.      public static void main(String[] args)  
  4.      {        
  5.           for (int x=1;x<=5 ;x++ )  
  6.           {  
  7.               for (int y=1;y<=x ;y++ )  
  8.               {  
  9.                     System.out.print(y);  
  10.               }  
  11.                System.out.println();  
  12.            }  
  13.      }  
  14. }  

练习四 for循环嵌套语句打印九九乘法表

九九乘法表

1*1=1

1*2=2 2*2=4

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

九九乘法表顾名思义,99

[java]  view plain copy
  1. class ForFor  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.            for (int x=1;x<=9 ;x++ )  
  6.            {  
  7.                    for (int y=1;y<=x ;y++ )  
  8.                    {  
  9.                          System.out.print(x+"*"+y+"="+x*y+" ");                    
  10.                    }  
  11.                    System.out.println();  
  12.            }  
  13.       }  
  14. }  

练习五 for循环嵌套语句打印正三角形

----*

---* *

--* * *

-* * * *

* * * * *

确定行数,5,外循环范围是5,列数也是5(不算空格)

[java]  view plain copy
  1. class ForFor1  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.           for (int x=1;x<=5 ;x++ )  
  6.           {  
  7.                for (int y=x;y<=4 ;y++ )  
  8.                {  
  9.                     System.out.print("-");  
  10.                }  
  11.                for (int z=1;z<=x ;z++ )  
  12.                {  
  13.                     System.out.print("* ");  
  14.                }  
  15.                System.out.println();  
  16.           }  
  17.      }  
  18. }  
练习六 for循环语句拓展与作用机制

[java]  view plain copy
  1. class Fortest  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.           int x = 1;  
  6.           for (System.out.print("a");x<3;System.out.print("c"))  
  7.           {  
  8.                System.out.print("d");  
  9.                x++;  
  10.           }  
  11.      }  
  12. }  
  13. //结果为adcdc  
  14. //结果验证了for循环的作用机理<strong>  
  15. </strong>  
练习七 for循环嵌套语句练习:计数器

[java]  view plain copy
  1. //1-100所有7的倍数的个数  
  2. /*思路  7的倍数 
  3. 第一个          7/7=1 
  4. 第二个          14/7=2 
  5. 第三个          21/7=3 
  6. 第四个          28/7=4 
  7. 第五个          35/7=5 
  8. 第六个          49/7=6      无论最大的数是多大,除以7之后都会取整数,所以100除以7的整数一定是7的倍数 
  9. */  
  10. class ForTest3  
  11. {  
  12.      public static void main(String[] args)  
  13.      {  
  14.           //第一种方法  
  15.           int x=0;  
  16.           for (int y=0;y<100;y++)  
  17.           {  
  18.                x=y/7;  
  19.           }  
  20.           System.out.println("x="+x);  
  21.           //第二种方法 循环for语句中套用条件语句if(计数器)  
  22.           int count = 0;  
  23.           for (int m=1;m<=100 ;m++ )  
  24.           {  
  25.                if (m%7==0)  
  26.                {  
  27.                     count++;  
  28.                }  
  29.           }  
  30.           System.out.println("count="+count);  
  31.      }  
  32. }  
练习八 forwhile循环语句计算1-10的累加

[java]  view plain copy
  1. //累加思想练习  
  2. //1到10的所有数的和  
  3. /*思路:分析出切入点 
  4.        0+1 
  5.           1+2 
  6.             3+3 
  7.                6+4 
  8. */  
  9. class ForTest2  
  10. {  
  11.      public static void main(String[] args)  
  12.      {             
  13.             //定义一个不断变化的和  
  14.             int x=0;  
  15.             //定义一个不断变大的被加的数字  
  16.             int y=1;  
  17.             //定义一个while循环  
  18.             while (y<=10)  
  19.             {  
  20.                x=x+y;  
  21.                y++;           
  22.             }  
  23.                System.out.println("x="+x);             
  24.            
  25.             //用for循环继续写一次  
  26.             int m =0;  
  27.             for (int n=1; n<=10;n++ )  
  28.             {  
  29.                  m=m+n;  
  30.             }  
  31.              System.out.println("m="+m);           
  32.      }  
  33. }<strong>  
  34. </strong>  

breakcontinue

1.作用范围:

       break,作用于选择和循环语句。

       continue,只作用于循环语句。

2.breakcontinue单独执行时,下面不能有其他语句,因为执行不到。

可以通过给循环定义标签的方法,来决定breakcontinue是作用于那个循环,见下面代码

[java]  view plain copy
  1. class OtherDemo  
  2. {  
  3.      public static void main(String[] args)  
  4.      {  
  5.         //break:跳出,用在选择结构和循环结构          
  6.         w:for (int x=0;x<3 ;x++ )  
  7.         {  
  8.             q:for (int y=0;y<4 ;y++ )  
  9.             {  
  10.                     System.out.println(x);  
  11.                     break w;  
  12.              }                 
  13.          }  
  14.          //continue:继续,只作用在循环结构  
  15.           for (int x=1;x<=10 ;x++ )  
  16.           {  
  17.                if (x%2==1)  
  18.                {  
  19.                     continue;  
  20.                }  
  21.                System.out.println(x);  
  22.            }  
  23.      }  
  24. }  
------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值