黑马程序员 Java基本语句

来源:互联网 发布:张志君 java 编辑:程序博客网 时间:2024/05/19 11:48

 ------- android培训、java培训、期待与您交流! ----------

 

if语句,在if语句主体中要处理的语句只有一个是可省略左右大括号。

if...else语句,同if语句当语句主题只有一句时可省略左右大括号。

if...else if...else语句含有更多的条件判断语句。

三元运算符:格式:(条件表达式)?表达式1:表达式2

三元运算符为if...else的简写形式,优点是可以简化if...else的代码,弊端是因为是一个运算符所以运算完必须要有一个结果。

实例:依用户指定,打印月份的季节:

 

public class Test {public static void main(String[] args) {int x=4;if(x==3||x==4||x==5)System.out.println("春");else if(x==6||x==7||x==8)System.out.println("夏");else if(x==9||x==10||x==11)System.out.println("秋");else if(x==11||x==12||x==1)System.out.println("冬");}}


 

public class Test {public static void main(String[] args) {int x=4;if(x>12||x<1)System.out.println("月份不存在");if(x>=3&&x<=8)System.out.println("春");else if(x>=6&&x<=8)System.out.println("夏");else if(x>=9&&x<=11)System.out.println("秋");elseSystem.out.println("冬");

多重选择--switch语句:

1.switch(表达式)中的表达式只接收byte,short,int,char类型。

2.各case与default无顺序,平等。

3.switch结束为break或”}“。

public class Test {public static void main(String[] args) {int x=4;switch(x){default:System.out.println("d");case 3:System.out.println("a");case 6:System.out.println("b");break;case 2:System.out.println("c");break;}}}


结果为  d  a  b  。

这里注意,当无break或”}“则不再判断case的值直接向下执行。再以上面判断季节为例:

public class Test {public static void main(String[] args) {int x=4;switch(x){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;case 12:case 1:case 2:System.out.println("冬");break;default:System.out.println("NO");}}}


这里再对if else与switch比较:if能判断区间以及布尔型,switch只能具体的值。

循环语句

while循环,要注意的是在第一次进入while循环前,必须先对循环控制变量(或表达式)赋起始值。

for循环格式:

for(初始化表达式;判断条件;循环后的操作表达式){

执行语句;

}

以下是两个例子(for循环错误)

for(int x=0;x<3;x++){System.out.println("x="+x);}System.out.println("x="+x);
int y=0;while(y<3){System.out.println("y="+y);}System.out.println("y="+y);

这里要说明的是在for循环中是编译出错的,因为x只在for循环题中存在,而while循环中由于之前在内存中定义所以没有问题,这是变量的作用域的问题。

1.变量有自己的作用域,对于for来讲,如果将用于控制循环的增量定义在for语句中,那么该变量只在for语句内有效,for语句执行完毕该变量在内存中被释放。

2.for和while可以互换如果需要定义循环增量用for更合适因为for不占用内存。

for(int y=0;y<3;y++){}
int y=0;for(;y<3;){    y++;}

以上两个方式是等价的。

循环注意:一定要明确哪些需要参与循环,哪些不需要。

例,打印以下造型

* * * *

* * *

* *

*

第一种:

public class Test01{public static void main(String[] args) {int z=5;for(int x=0;x<5;x++){for(int y=0;y<z;y++){System.out.print("*");}System.out.println();z--;}}}


第二种:

public class Test02{public static void main(String[] args) {int z=0;for(int x=0;x<5;x++){for(int y=z;y<5;y++){System.out.print("*");}System.out.println();z++;}}}


第三种:

public class Test03{public static void main(String[] args) {for(int x=0;x<5;x++){for(int y=x;y<5;y++){System.out.print("*");}System.out.println();}}}


第四种:

public class Test04{public static void main(String[] args) {for(int x=5;x>0;x--){for(int y=0;y<x;y++){System.out.print("*");}System.out.println();}}}


再例,打印九九乘法表:

public class Test99{public static void main(String[] args) {for(int x=1;x<=9;x++){for(int y=1;y<x;y++){System.out.print(y+"*"+x+"="+y*x+"\t");}System.out.println();}}}




 

原创粉丝点击