3.9基本程序控制
3.9.1 判断语句
1.if
2.if...else
3.if ...else if ...else...if ...else
4.switch
除了switch都要用布尔类型判断
public class Hello{
public static void main(String arg[]){
int x = 3;
if(x==3){
System.out.println("ok");
}
}
};
public class Hello{
public static void main(String arg[]){
int x = 3;
if(x==4){
System.out.println("ok");
}else{
System.out.println("not ok");
}
}
};
public class Hello{
public static void main(String arg[]){
int x = 3;
if(x==4){
System.out.println("ok");
}else if(x==3){
System.out.println("ok too");
}else{
System.out.println("not ok");
}
}
};
题目:给定一个程序,判断其等级。
小于60 差
60-75 中
75-95 良
95-100 优
public class Hello{
public static void main(String arg[]){
int x = 71;
if(x<60){
System.out.println("成绩差等");
}else if(x>=60 && x<75 ){
System.out.println("成绩中等");
}else if(x>=75 && x<95 ){
System.out.println("成绩良好");
}else{
System.out.println("成绩优秀");
}
}
};
以上if...else if 可以直接实现功能,但是java提供了switch语句,可以判
断是否满足于某个数值。
public class Hello{
public static void main(String arg[]){
int x = 1;
switch(x){
case 1:{
System.out.println("result 1");
}case 2:{
System.out.println("result 2");
}case 3:{
System.out.println("result 3");
}
}
}
};
结果是:
result 1
result 2
result 3
如果加上break语句
public class Hello{
public static void main(String arg[]){
int x = 1;
switch(x){
case 1:{
System.out.println("result 1");
break;
}case 2:{
System.out.println("result 2");
break;
}case 3:{
System.out.println("result 3");
break;
}
}
}
};
结果是
result 1
如果加上default表示如果以上条件都不存在的结果
public class Hello{
public static void main(String arg[]){
int x = 6;
switch(x){
case 1:{
System.out.println("result 1");
break;
}
case 2:{
System.out.println("result 2");
break;
}
case 3:{
System.out.println("result 3");
break;
}
default:{
System.out.println("result not exist");
}
}
}
};
结果
result not exist
swithc中只能处理数字或者字段操作,但是JDK1.5后,switch语句可以接受
枚举类型的操作,之后再详细介绍。
3.9.2循环语句(重点)
循环操作必须合理控制开始条件,循环条件,以及中间的循环体。
while , do...while, for
while
public class Hello{
public static void main(String arg[]){
int x = 1;
int sum = 0; //接收结果
while (x<= 100){
sum += x ; //进行加法操作
x++; //修改循环条件
}
System.out.println(sum);
}
};
结果5050。
public class Hello{
public static void main(String arg[]){
int x = 1;
int sum = 0; //接收结果
do{
sum += x ; //进行加法操作
x++; //修改循环条件
}
while (x<= 100);
System.out.println(sum);
}
};
结果相同。注意while后面要加分号,因为与后面是不同句子。
此语句是先执行一次再进行判断,就是说不管条件如何,都会执行一次。以
上while, do...while都是在外面定义一个循环变量,之后再里面修改了变化
。其语法的行数非常的多,可使用for循环来简化。
public class Hello{
public static void main(String arg[]){
int sum = 0;
for (int x = 0; x<=100;x++){
sum += x ; //进行加法操作
}
System.out.println(sum);
}
};
3.9.3 循环控制
可以用break 和continue进行循环控制
public class Hello{
public static void main(String arg[]){
int sum = 0;
for (int x = 0; x<=100;x++){
if(x==3){
break;
}
System.out.println(x);
}
}
};
结果
0
1
2
使用continue进行控制,表示退出一次的循环操作。
public class Hello{
public static void main(String arg[]){
int sum = 0;
for (int x = 0; x<=100;x++){
if(x==3){
continue;
}
System.out.println(x);
}
}
};
0
1
2
4
5
6
...
3.9.4实例讲解
public class Hello{
public static void main(String arg[]){
int sum = 0;
for (int x = 0; x<=100;x++){
if(x==3){
continue;
}
System.out.println(x);
}
}
};
九九乘法表
public class Hello{
public static void main(String arg[]){
for (int x =1 ; x < 10 ; x ++){
for(int y = 1 ; y<=x;y++){
System.out.print(x + "*" + y + "=" + x*y);
}
System.out.println();
}
}
}1*1=1
2*1=22*2=4
3*1=33*2=63*3=9
4*1=44*2=84*3=124*4=16
5*1=55*2=105*3=155*4=205*5=25
6*1=66*2=126*3=186*4=246*5=306*6=36
7*1=77*2=147*3=217*4=287*5=357*6=427*7=49
8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
3000以内能被3,5,7同时整除的数字
public class Hello{
public static void main(String arg[]){
for (int x =1 ; x <=3000 ; x ++){
if(x %5==0 && x%7==0 && x%3==0){
System.out.println(x);
}
}
}
}
双重循环(之后再看)
三目运算符(之后再看)
有一点不太懂,哪里加;为什么有时候SUM列出的是所有,有时候列出的是最后的值。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22117706/viewspace-626172/ ,如需转载,请注明出处,否则将追究法律责任。