黑马程序员_java 基本语句的理解

来源:互联网 发布:中国的网络发展历程 编辑:程序博客网 时间:2024/06/05 02:26

一,程序流程控制


1.判断结构

if语句

三种格式

1.1.if(条件表达式){

执行语句

}

1.2.if(条件表达式){

执行语句

}else{

执行语句

}

1.3

if(条件表达式){

执行语句

}else if(条件表达式){

执行语句

}

……

else

{

执行语句

}

[java] view plaincopy
  1. class IfDemo   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         int x = 1;  
  6.   
  7.         if(x>1)  
  8.         {  
  9.             System.out.println("yes");  
  10.         }  
  11.         else  
  12.         {  
  13.             System.out.println("a");  
  14.         }  
  15.           
  16.         /* 
  17.         if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2; 
  18.          
  19.          
  20.         */  
  21.         int a = 9,b;  
  22.         b = (a>1)?100:200;  
  23.   
  24.         if(a>1)  
  25.             b = 100;  
  26.         else  
  27.             b = 200;  
  28.   
  29.   
  30.         int n = 3;  
  31.   
  32.         if(n>1)  
  33.             System.out.println("a");  
  34.         else if(n>2)  
  35.             System.out.println("b");  
  36.         else if(n>3)  
  37.             System.out.println("c");  
  38.         else  
  39.             System.out.println("d");  
  40.   
  41.         /* 
  42.         if(n>1) 
  43.             System.out.println("a"); 
  44.         if(n>2) 
  45.             System.out.println("b"); 
  46.         if(n>3) 
  47.             System.out.println("c"); 
  48.         else 
  49.             System.out.println("d"); 
  50.         */  
  51.         System.out.println("over");  
  52.     }  
  53. }  


2.选择结构

switch语句

格式:switch(表达式){

 case 数值1:

执行语句;

break;

case 数值2:

执行语句;

break;

case 数值3:

执行语句;

break;

……

default :

执行语句;

break;


}

[java] view plaincopy
  1. class SwitchDemo   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.   
  6.         int x = 3;  
  7.         /* 
  8.         switch(x)//byte short int char 
  9.         { 
  10.             default: 
  11.                 System.out.println("d"); 
  12.                 //break; 
  13.             case 4: 
  14.                 System.out.println("a"); 
  15.                 //break; 
  16.             case 6: 
  17.                 System.out.println("b"); 
  18.                 break; 
  19.             case 2: 
  20.                 System.out.println("c"); 
  21.                 break; 
  22.              
  23.              
  24.         } 
  25.         */  
  26.   
  27.         /* 
  28.         int a=4,b =2; 
  29.  
  30.         char ch = '+'; 
  31.  
  32.         switch(ch) 
  33.         { 
  34.             case '-': 
  35.                 System.out.println(a-b); 
  36.             break; 
  37.             case '+': 
  38.                 System.out.println(a+b); 
  39.             break; 
  40.             case '*': 
  41.                 System.out.println(a*b); 
  42.             break; 
  43.             case '/': 
  44.                 System.out.println(a/b); 
  45.             break; 
  46.             default: 
  47.                 System.out.println("feifa"); 
  48.  
  49.         } 
  50.         */  
  51.         System.out.println("Hello World!");  
  52.     }  
  53. }  
if和switch语句很像。具体什么场景,应用哪个语句呢?如果判断的具体数值不多,而是符合byte short int char这四种类型建议使用switch语句
两种语句都可用的情况建议选用switch语句,因为效率稍高,其他情况,对区间判断,对结果为boolean类型判断,使用if的使用范围更广。

3.循环结构

使用语句:while ,do while,for

while语句格式:while(条件表达式){

                                       执行语句;

                                        }

do while语句格式:do{

                                     执行 语句;

                                     }while(条件表达式);

do while特点是条件无论是否满足循环体至少被执行一次。

[java] view plaincopy
  1. class WhileDemo   
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.           
  6.         /* 
  7.         定义初始化表达式; 
  8.         while(条件表达式) 
  9.         { 
  10.             循环体(执行语句); 
  11.         } 
  12.          
  13.         int x = 1; 
  14.         while(x<3) 
  15.         { 
  16.             System.out.println("x="+x); 
  17.             x++; 
  18.         } 
  19.         int x = 1; 
  20.         do 
  21.         { 
  22.             System.out.println("do : x="+x); 
  23.             x++; 
  24.         } 
  25.         while (x<3); 
  26.         */  
  27.         int y = 1;  
  28.         while(y<3)  
  29.         {  
  30.             System.out.println("y="+y);  
  31.             y++;  
  32.         }  
  33.   
  34.           
  35.         /* 
  36.         while:先判断条件,只有条件满足才执行循环体。 
  37.         do while: 先执行循环体,在判断条件,条件满足,再继续执行循环体。 
  38.         简单一句话:do while:无论条件是否满足,循环体至少执行一次。 
  39.  
  40.         */  
  41.     }  
  42. }  
for(初始化表达式:循环条件表达式:循环后的操作表达式){

执行语句

};

[java] view plaincopy
  1. class ForDemo   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.   
  6.         for(int x = 0; x<3 ; x++)  
  7.         {  
  8.             System.out.println("x="+x);  
  9.   
  10.   
  11.         }  
  12.         //System.out.println("x===="+x);  
  13.           
  14.         int y=0;  
  15.         while(y<3)  
  16.         {  
  17.             System.out.println("y="+y);  
  18.             y++;  
  19.         }  
  20.         System.out.println("y===="+y);  
  21.   
  22.   
  23.         /* 
  24.         1,变量有自己的作用域。对于for来讲:如果将用于控制循环的增量定义在for语句中。那么该变量只在for语句内有效。 
  25.         for语句执行完毕。该变量在内存中被释放。 
  26.  
  27.  
  28.         2,for和while可以进行互换。如果需要定义循环增量。用for更为合适。 
  29.  
  30.  
  31.         */  
  32.     }  
  33. }  

[java] view plaincopy
  1. //语句嵌套形式。其实就是语句中还有语句。  
  2. //循环嵌套。  
  3. class ForForDemo   
  4. {  
  5.     public static void main(String[] args)   
  6.     {  
  7.         for(int x=0; x<3; x++)//  
  8.         {  
  9.             for(int y=0; y<4; y++)  
  10.             {  
  11.                 System.out.print("*");  
  12.             }  
  13.             System.out.println();//只有一个功能就是换行。  
  14.         }  
  15.         System.out.println("-------------------");  
  16.   
  17.         /* 
  18.          
  19.         ***** 
  20.         **** 
  21.         *** 
  22.         ** 
  23.         * 
  24.         发现图形有很多行,每一个行有很多列。 
  25.         要使用嵌套循环。原理:形象说法:大圈套小圈。 
  26.          
  27.  
  28.         */  
  29.           
  30.         //int z = 5;  
  31.         for (int x=0; x<5 ;x++ )//x<5:因为外循环控制行数。一共5行。  
  32.         {  
  33.             for (int y=x; y<5 ;y++)  
  34.             {  
  35.                 System.out.print("*");  
  36.             }  
  37.             System.out.println();  
  38.             //z++;  
  39.         }  
  40.     }  
  41. }  
  42.   
  43. /* 
  44.  
  45. **** 
  46. **** 
  47. **** 
  48. 对于打印长方形:外循环控制的行数。内循环控制的是每一行的列数。也就是一行中元素的个数。 
  49.  
  50. ***** 
  51. **** 
  52. *** 
  53. ** 
  54. * 
  55.  
  56.  
  57.  
  58.  
  59.  
  60. */  



4.顺序结构

其他流程控制语句

break(跳出),continue(继续)

break语句:应用范围:选择结构和循环结构

continue语句:应用于循环结构

注:这两个语句离开应用范围,存在是没有意义的

这两个语句单独存在下面都不可以有语句,因为执行不到

continue语句是结束本次循环继续下次循环

标号的出现,,可以让这两个语句作用于指定的范围

class OtherDemo 
{
public static void main(String[] args) 
{
//break:
w:for(int x=0; x<3; x++)
{
for(int y=0; y<4; y++)
{
System.out.println("x="+x);
break w;
}
}


//continue:只能作用于循环结构。继续循环。特点:结束本次循环,继续下一次循环。


for(int x=1; x<=10; x++)
{
if(x%2==1)
continue;
System.out.println("x="+x);

}


w:for(int x=0; x<3; x++)
{
for(int y=0; y<4; y++)
{
System.out.println("x="+x);
continue w;
}
}


/*
记住:
1,break和continue语句作用的范围。
2,break和continue单独存在时,下面可以有任何语句。因为都执行不到。
*/


// break;
// continue;
}
}

二,函数

1.1.函数的定义:

什么是函数:函数就是定义在类中的具有特定功能的一段独立小程序。函数也成为方法

1.2.函数的格式:

修饰符 返回值类型  函数名(参数类型 参数值){

    执行语句;

   return 返回值;

}

1.3.返回值类型:函数运行后的结果的数据类型

1.4.参数类型:是形参的数据类型

1.5形式参数:是一个变量,用于存储调用函数时传递给函数的实际参数

[java] view plaincopy
  1. class FunctionDemo   
  2. {  
  3.       
  4.   
  5.     public static void main(String[] args)   
  6.     {  
  7.         /* 
  8.         int x = 4; 
  9.         System.out.println(x*3+5); 
  10.  
  11.         x = 6; 
  12.  
  13.         System.out.println(x*3+5); 
  14.         */  
  15.         //int y = 4*3+5;  
  16.         //int z = 6*3+5;  
  17.   
  18.         //int x = getResult(4);  
  19.         //System.out.println("x="+x);  
  20.         //int y = getResult(6);  
  21.   
  22.         getResult(5);  
  23.   
  24.     }  
  25.   
  26.     //发现以上的运算,因为获取不同数据的运算结果,代码出现了重复。  
  27.     //为了提高代码的复用性。对代码进行抽取。  
  28.     //将这个部分定义成一个独立的功能。方便与日后使用。  
  29.     //java中对功能的定义是通过函数的形式来体现的。  
  30.   
  31.     //需要定义功能,完成一个整数的*3+5的运算,  
  32.   
  33.     //1,先明确函数定义的格式。  
  34.     /* 
  35.  
  36.     修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,) 
  37.     { 
  38.             执行语句; 
  39.             return 返回值; 
  40.     } 
  41.      
  42.     //当函数运算后,没有具体的返回值时,这是返回值类型用一个特殊的关键字来标识。 
  43.     //该关键字就是void。void:代表的是函数没有具体返回值的情况。 
  44.     //当函数的返回值类型是void时,函数中的return语句可以省略不写。 
  45.     */  
  46.     public static void getResult(int num)  
  47.     {  
  48.         System.out.println(num * 3 + 5);  
  49.         return;//可以省略  
  50.     }  
  51.       
  52.   
  53.   
  54. }  


函数的特点:

函数的应用:

函数的重载:

什么时候用重载?
当定义的功能相同,但参与运算的未知内容不同。
那么,这时就定义一个函数名称以表示起功能,方便阅读,而通过参数列表的不同来区分多个同名函数。

[java] view plaincopy
  1. class FunctionOverload   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.   
  6. //      add(4,5);  
  7. //      add(4,5,6);  
  8.         print99();  
  9.   
  10.     }  
  11.     public static void print99(int num)  
  12.     {  
  13.         for(int x=1; x<=num; x++)  
  14.         {  
  15.             for(int y=1; y<=x; y++)  
  16.             {  
  17.                 System.out.print(y+"*"+x+"="+y*x+"\t");  
  18.             }  
  19.             System.out.println();  
  20.         }  
  21.     }  
  22.   
  23.     //打印99乘法表  
  24.     public static void print99()  
  25.     {  
  26.         print99(9);  
  27.     }  
  28.   
  29.     //定义一个加法运算,获取两个整数的和。  
  30.     public static int add(int x,int y)  
  31.     {  
  32.         return x+y;  
  33.     }  
  34.   
  35.     //定义一个加法,获取三个整数的和。  
  36.     public static int add(int x,int y,int z)  
  37.     {  
  38.         return add(x,y)+z;  
  39.     }  
  40. }  
  41.   
  42.   
  43. /* 
  44.  
  45. void show(int a,char b,double c){} 
  46.  
  47. a. 
  48. void show(int x,char y,double z){}//没有,因为和原函数一样。 
  49.  
  50. b. 
  51. int show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。 
  52. c. 
  53.  
  54. void show(int a,double c,char b){}//重载,因为参数类型不同。注意:重载和返回值类型没关系。 
  55.  
  56. d. 
  57. boolean show(int c,char b){}//重载了,因为参数个数不同。 
  58.  
  59. e. 
  60. void show(double c){}//重载了,因为参数个数不同。 
  61.  
  62. f. 
  63. double show(int x,char y,double z){}//没有,这个函数不可以和给定函数同时存在与一个类中。 
  64.  
  65.  
  66. */  
0 0
原创粉丝点击