------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------
总结内容来源于黑马毕老师的java基础教学视频
Java语句有3中结构:判断结构、选择结构、循环结构.
一 判断结构---if 语句
格式(1) if (条件表达式)
{执行语句;}
格式(2) if (条件表达式)
{执行语句;}
else
{执行语句;}
格式(3) if (条件表达式)
{执行语句1;}
else if
{执行语句2;}
else if
{执行语句3;}
.....................
else
{执行语句;}
- //3,4,5月为春季.6,7,8为夏季.9,10,11为秋季.12,1,2为冬季
- class Ifdemo
- {
- public static void main(String[] args)
- {
- int mon = 4;
- if (6>mon&&mon>2)
- {
- System.out.println("春季");
- }
- else if (mon>5&&mon<9)
- {
- System.out.println("夏季");
- }
- else if (mon>8&&mon<12)
- {
- System.out.println("秋季");
- }
- else if (mon==1||mon==2||mon==12)
- {
- System.out.println("冬季");
- }
- else
- System.out.println("你输入的是什么啊?");
- }
- }
二 选择结构---switch 语句
格式:switch(表达式)
{
case 变量可能值1 :
执行语句一;
break;
case 变量可能值2 :
执行语句二;
break;
case 变量可能值3 :
执行语句三;
break;
......................
default :
执行语句;
}
- class SwitchTest
- {
- public static void main(String[] args)
- {
- int m = 14;
- switch (m)
- {
- default:
- System.out.println("你输入的无效!");
- break;
- case 3 :
- case 4 :
- case 5 :
- System.out.println(m+"春季");
- break;
- case 6 :
- case 7 :
- case 8 :
- System.out.println(m+"夏季");
- break;
- case 9 :
- case 10 :
- case 11 :
- System.out.println(m+"秋季");
- break;
- case 12 :
- case 1 :
- case 2 :
- System.out.println(m+"冬季");
- break;
- }
- }
- }
switch语句特点:
1. 选择的类型只有四种:byte,int,short,char ;
2. case与default之间没有顺序,先执行第一个case,没有匹配的case,执行default;
3. 结束switch语句有两种情况:一是遇到break,二是执行到switch语句结束;
4. 如果匹配的case或者default没有对应的break,那么程序会继续向下执行可以执行到的语句,直到遇到break或者switch语句结束。
if语句和switch语句的选择:
如果判断具体数值不多,而且是switch可以选择的类型,建议使用switch,其效率稍高,其他情况,如判断区间,表达式结果为boolean类型判断,选择if,if使用范围广泛。
三 循环结构
while 循环
格式:
定义初始化表达式;
while(条件表达式)
{
循环体(执行语句);
}
- //循环语句while用法
- class WhileDemo
- {
- public static void main(String[] args)
- {
- //定义一个变量
- int x = 1;
- //while循环语句(条件语句)
- while (x<=30)
- {
- System.out.println("x="+x);
- x += 2;
- }
- }
- }
do while 循环
格式:
定义初始化表达式;
do
{
循环体(执行语句);
}
while(条件表达式)
- class DoWhileDemo
- {
- public static void main(String[] args)
- {
- int x = 1;
- do
- {
- System.out.println("Do: x = "+x);
- x++;
- }
- while (x < 3);
- }
- }
while和do while区别:
while:先判断后执行,do while:先执行一次,后判断是否继续执行.
总结:do while不管满足条件与否,至少执行一次.
for 循环
格式:
for(定义初始化表达式;循环条件表达式;循环后的操作表达式)
{
循环体(执行语句);
}
- class ForDemo
- {
- public static void main(String[] args)
- {
- for (int x=1;x<3 ;x++)
- {
- System.out.println("Hello World!");
- }
- }
- }
注意:
for 与while的区别是----for定义的条件控制变量在循环后就不存在了(优化了内存), 而while的条件控制变量循环后仍存在.
无限循环最简单的两种形式:for(;;) {} ,while(true){}.
for嵌套循环语句
练习一 for循环嵌套语句打印直角三角形
*****
****
***
**
*
打印输出上面的形状,有多行多列,就用到了for嵌套循环语句
- class ForFor
- {
- public static void main(String[] args)
- {
- ;
- for (int x=1;x<=5 ;x++)
- {
- for (int y=x;y<=5 ;y++)
- {
- System.out.print("*");
- }
- System.out.println("");
- }
- }
- }
*
**
***
****
*****
- class ForFor
- {
- public static void main(String[] args)
- {
- for (int x=1;x<=5 ;x++ )
- {
- for (int y=1;y<=x ;y++ )
- {
- System.out.print("*");
- }
- System.out.println();
- }
- }
- }
1
12
123
1234
12345
- class ForFor
- {
- public static void main(String[] args)
- {
- for (int x=1;x<=5 ;x++ )
- {
- for (int y=1;y<=x ;y++ )
- {
- System.out.print(y);
- }
- System.out.println();
- }
- }
- }
练习四 for循环嵌套语句打印九九乘法表
九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
九九乘法表顾名思义,有9行9列
- class ForFor
- {
- public static void main(String[] args)
- {
- for (int x=1;x<=9 ;x++ )
- {
- for (int y=1;y<=x ;y++ )
- {
- System.out.print(x+"*"+y+"="+x*y+" ");
- }
- System.out.println();
- }
- }
- }
练习五 for循环嵌套语句打印正三角形
----*
---* *
--* * *
-* * * *
* * * * *
确定行数,5行,外循环范围是5,列数也是5(不算空格)
- class ForFor1
- {
- public static void main(String[] args)
- {
- for (int x=1;x<=5 ;x++ )
- {
- for (int y=x;y<=4 ;y++ )
- {
- System.out.print("-");
- }
- for (int z=1;z<=x ;z++ )
- {
- System.out.print("* ");
- }
- System.out.println();
- }
- }
- }
- class Fortest
- {
- public static void main(String[] args)
- {
- int x = 1;
- for (System.out.print("a");x<3;System.out.print("c"))
- {
- System.out.print("d");
- x++;
- }
- }
- }
- //结果为adcdc
- //结果验证了for循环的作用机理<strong>
- </strong>
- //1-100所有7的倍数的个数
- /*思路 7的倍数
- 第一个 7/7=1
- 第二个 14/7=2
- 第三个 21/7=3
- 第四个 28/7=4
- 第五个 35/7=5
- 第六个 49/7=6 无论最大的数是多大,除以7之后都会取整数,所以100除以7的整数一定是7的倍数
- */
- class ForTest3
- {
- public static void main(String[] args)
- {
- //第一种方法
- int x=0;
- for (int y=0;y<100;y++)
- {
- x=y/7;
- }
- System.out.println("x="+x);
- //第二种方法 循环for语句中套用条件语句if(计数器)
- int count = 0;
- for (int m=1;m<=100 ;m++ )
- {
- if (m%7==0)
- {
- count++;
- }
- }
- System.out.println("count="+count);
- }
- }
- //累加思想练习
- //1到10的所有数的和
- /*思路:分析出切入点
- 0+1
- 1+2
- 3+3
- 6+4
- */
- class ForTest2
- {
- public static void main(String[] args)
- {
- //定义一个不断变化的和
- int x=0;
- //定义一个不断变大的被加的数字
- int y=1;
- //定义一个while循环
- while (y<=10)
- {
- x=x+y;
- y++;
- }
- System.out.println("x="+x);
- //用for循环继续写一次
- int m =0;
- for (int n=1; n<=10;n++ )
- {
- m=m+n;
- }
- System.out.println("m="+m);
- }
- }<strong>
- </strong>
break和continue
1.作用范围:
break,作用于选择和循环语句。
continue,只作用于循环语句。
2.break和continue单独执行时,下面不能有其他语句,因为执行不到。
可以通过给循环定义标签的方法,来决定break和continue是作用于那个循环,见下面代码
- class OtherDemo
- {
- public static void main(String[] args)
- {
- //break:跳出,用在选择结构和循环结构
- w:for (int x=0;x<3 ;x++ )
- {
- q:for (int y=0;y<4 ;y++ )
- {
- System.out.println(x);
- break w;
- }
- }
- //continue:继续,只作用在循环结构
- for (int x=1;x<=10 ;x++ )
- {
- if (x%2==1)
- {
- continue;
- }
- System.out.println(x);
- }
- }
- }