流程控制
作者:互联网
1. 顺序
2. 分支
(1)if ...else...
if () {}
if() {} else {}
if () {} else if() {} ... else{}
如果多个表达式之间没有交集,那么多个else if 之间顺序无关
如果多个表达式之间有包含关系,范围小的因该声明在范围大的前面
if else "就近原则" else找距离其最近的if配对
//if-else实现三个数的比较
if (num1 >= num2) {
if (num3 >= num1) {
System.out.println(num3 + "," + num1 + "," + num2 );
}else if (num3 <= nmu2) {
System.out.println(num1 + "," + num2 + "," + num3 );
}else {
System.out.println(num1 + "," + num3 + "," + num2 );
}
}else {//num2 > num1
if (num3 >= num2) {
System.out.println(num3 + "," + num2 + "," + num1 );
}else if (num3 <= nmu1) {
System.out.println(num2 + "," + num1 + "," + num3 );
}else {
System.out.println(num2 + "," + num3 + "," + num1 );
}
}
产生一定范围的随机数的公式 [a,b]:(int)(Math.random()*(b-a+1) )+a
(2)switch-case
-
根据switch表达式中的值,匹配每个case中的常量,一旦匹配成功,执行相应的语句;若没有break,则继续向下执行。
-
switch中的表达式只能是:byte,short,char,int,枚举类型(Jdk5.0新增),String类型(JDK7.0新增)
-
default 和 break都是可选的
switch(表达式){
case 常量1:
语句一;
//break ;
. . .
default :
语句 n ;
// break;
}
//键盘输入年月日,判断改天为该年的第多少天
Scanner s = new Scanner(System.in);
System.out.print("请输入年份:");
int year = s.nextInt();
System.out.print("请输入月份:");
int month = s.nextInt();
System.out.print("请输入日期:");
int day = s.nextInt();
int totle = 0;
switch(month){
case 12:
totle += 31;
case 11:
totle += 30;
case 10:
totle += 31;
case 9:
totle += 30;
case 8:
totle += 31;
case 7:
totle += 31;
case 6:
totle += 30;
case 5:
totle += 31;
case 4:
totle += 30;
case 3:
totle += 31;
case 2:
if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)) {
totle += 29;
}else {
totle += 28;
}
case 1:
totle += 31;
}
System.out.println(year+"年"+month+"月"+day+"日是该年的第"+totle+"天。");
3. 循环
循环4要素
1初始化条件
2循环条件 --->boolean类型
3循环体
4迭代条件
break :跳出当前循环,用于switch-case和循环
continue :跳出本次循环,用于循环
break和continue之后不可声明语句
label : 跳出指定循环
return; 结束当前方法
(1)for
for(1 ; 2 ; 4){
3;
}
1-2-3-4-2-3-4-. . . -2
(2)while
1;
while(2){
3;
4;
}
1-2-3-4-2-3-4- . . .-2
(3)do while
1;
do{
3;
4;
}while(2)
1-3-4-2-3-4-2- . . . -3-4-2
for(;;) { } 效果等同于while (true) {}
(4)嵌套循环
/
/打印
// *
// * *
// * * *
// * * * *
//* * * * *
// * * * *
// * * *
// * *
// *
//上半部分
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for(int k = 1;k <= i ;k++){
System.out.print("* ");
}
System.out.println();
}
//下半部分
for (int i = 1 ; i <= 4; i++ ) {
for (int j = 1 ;j <= i ; j++ ) {
System.out.print(" ");
}
for (int k =0 ;k <= 4-i;k++) {
System.out.print("* ");
}
System.out.println();
}
//打印九九乘法表
for (int i = 1 ; i <= 9 ; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println();
}
//100以内的所有的质数(素数:只能被一和它本身整除的数),最小的质数是2
for (int i = 2;i <= 100 ; i++ ) {
int num = 0;
for (int j = 2;j <= Math.sqrt(i) ;j++) {
if (i % j == 0) {
num++;
break;
}
}
if(num == 0) {
System.out.println(i);
}
}
//质数的优化
int count = 0;
long start = System.currentTimeMillis();
label: for (int i = 2;i <= 100000 ; i++ ) {
for (int j = 2;j <= Math.sqrt(i) ;j++) {
if (i % j == 0) {
continue label;
}
}
count ++;
}
long end = System.currentTimeMillis();
System.out.println(end-start);
System.out.println(count);
标签:case,控制,totle,int,流程,System,else,out 来源: https://www.cnblogs.com/zhangkaihong3918/p/15724079.html