java流程控制
一、选择结构
1.if结构(可以嵌套)
例如:
int rult=10;
if(rult%5==0){
System.out.print("\n"); //\n代表换行
嵌套的结构:
if(){
if(){
System.out.print();
}
if(){
System.out.print();
}
2.if-else(如果(if)满足(条件)…执行第一个大的花括号{}内的语句,否则执行第二个{}内的语句…)
if(条件){
System.out.print();
}else{
}
3.if-else if(可以一直往下写)
if(){
System.out.print();
}else if{
}else if{
}…
4.switch结构
(注意:break是结束当前程序,break后的代码不会执行,所以当你在break后写语句时,开发工具会报错,且break只用于switch和for中)
例1.正常的switch结构—根据年龄输出是什么年级段的人
package flows;
/**
* @author ljw
*
* 练习四: 随机产生一个1-100内的数,代表人的年龄,
* 根据年龄输出是什么年级段的人 青少年 [10 - 20)
* 壮年 [20 - 30)
* 中年 [30 - 50) 老年 [50 - 80)
* @2020年10月22日
*/
public class YearClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int year = ((int) (Math.random() * 100) + 1) / 10;
System.out.println(year * 10);
switch (year) {
case 1:
System.out.println("青少年");
break;
case 2:
System.out.println("壮年");
break;
case 3:
System.out.println("中年");
break;
case 4:
System.out.println("中年");
break;
case 5:
System.out.println("老年");
break;
case 6:
System.out.println("老年");
break;
case 7:
System.out.println("老年");
break;
default:
System.out.println("超过要求");
break;
}
}
}
例2:case穿透—判断 春夏秋冬
(注意:不加break时,程序会一直往下执行,这就是case穿透的原理)
package Hworks;
/**
* @author ljw
*
* @2020年10月22日
* 判断 春夏秋冬
*/
public class PintSeaSon {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int month =(int)(Math.random()*12+1);
System.out.println("月份是"+month);
switch (month){
case 11:
case 12:
case 1:
System.out.println("冬天");
break;
case 2:
case 3:
case 4:
System.out.println("春天");
break;
case 5:
case 6:
case 7:
System.out.println("夏天");
break;
case 8:
case 9:
case 10:
System.out.println("秋天");
break;
}
}
}
二、循环结构
1.while循环
例1:计算1000以内奇数及偶数的和,并输出。
package flows;
/**
* 作业1:计算1000以内奇数及偶数的和,并输出。
* @author ljw
*
* @2020年10月22日
*/
public class WhiteDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int one =1;
int oneSum =0;
int two =2;
int twoSum =0;
while(one<1000){
oneSum += one;
one=one+2;//奇数+2=奇数
}
System.out.println("奇数和="+oneSum);
while(two<=1000){
twoSum += two;
two=two+2;//偶数+2=偶数
}
System.out.println("偶数和="+twoSum);
}
}
2.do-while循环
一般用于先考试,后输入成绩这样的场景,先执行后判断的场景。特点是先执行一次,再判断。无论后面的条件符不符合都会执行一遍。
例如
/**
- @author ljw
*使用do-while实现:输出摄氏温度与华氏温度的对照表,
*要求它从摄氏温度0度到250度,每隔20度为一项。
*转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32
0 32
20 68 - @2020年10月24日
*/
package WeekWork;
public class Temperature07 {
public static void main(String[] args) {
int tem =0; //温度
do {
System.out.print(tem);
System.out.print(" "+(int)(tem * 9 / 5.0 + 32));
tem +=20;
System.out.println();
}while(tem<=250);
}
}
3.for循环
结构:
for(初始化条件;判断条件;迭代){
执行语句;
}
执行顺序:
(1)初始化的条件(只执行一遍)
(2)执行语句 //执行了n+1次
(3)迭代 //执行了n+1次
(4)判断条件 //执行了n次
(2)执行语句
(3)迭代
(4)判断条件
…
然后循环 ,当判断条件不满足时,跳出循环。
切记:for循环外的i比for循环内的i,多+1。
例如:
package WeekWork;
/**
- @author ljw
*学校2009年培养学生900人,
*每年增长25%,请问按此速度增长,到哪一年培训学生人数将达到1万人。
* @2020年10月24日
*/
public class AddMan06 {
public static void main(String[] args) {
double sum =900;
int i =0;
for( i=1;i<900;i++) {
sum =sum*1.25 ;
System.out.println(sum);
if(10000<=sum) {
break;//结束循环,跳出当前的for循环
}
}
System.out.println(i+2009+"年培训人数达到一万");
}
}