53_Java_流程控制

举报
alexsully 发表于 2021/05/27 00:02:07 2021/05/27
【摘要】 if else switch case for while do while 嵌套循环 for(){ for () {} }

分支结构

根据条件,选择性地执行某段代码 有if…else和switch-case两种分支语句

if else if else

public class IfTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        if(score == 100){
            System.out.println("a");
        }else if(score > 80 &&  score <= 99){
            System.out.println("b");
        }else{
            System.out.println("c");
        }
    }
}

switch case 

*
2.说明:
① 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。
  当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字
② break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构
③ switch结构中的表达式,只能是如下的6种数据类型之一:
   byte 、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)

④ case 之后只能声明常量。不能声明范围。
⑤ break关键字是可选的。
⑥ default:相当于if-else结构中的else.   default结构是可选的,而且位置是灵活的。
*/

public class SwithchCase {
    public static void main(String[] args) {
        int number = 2;
        switch(number){
            case 0:
                System.out.println("zero");
                break;
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            default:
                System.out.println("other");
        } }}


循环结构

根据循环条件,重复性的执行某段代码 有while、do…while、for三种循环语句

For循环结构的使用
一、循环结构的4个要素
  1 初始化条件  ; 2 循环条件  --->是boolean类型
  3 循环体;   4 迭代条件

二、for循环的结构

for(1;2;4){
    3
}
执行过程:1 - 2 - 3 - 4 - 2 - 3 - 4 - ... - 2

public class ForTest {
    public static void main(String[] args) {
        int sum = 0;//记录所有偶数的和
        int count = 0;//记录偶数的个数
        for(int i = 1;i <= 100;i++){
            if(i % 2 == 0){
                System.out.println(i);
                sum += i;
                count++;
            }
        }
        System.out.println("总和为:" + sum);
        System.out.println("个数为:" + count);
    }
}

While 循环的使用

一、循环结构的4个要素
  1 初始化条件  2 循环条件  --->是boolean类型
  3 循环体      4 迭代条件

二、while循环的结构
1
while(2){
    3;
    4;
}

执行过程:1 - 2 - 3 - 4 - 2 - 3 - 4 - ... - 2

public class WhileTest {
    public static void main(String[] args) {
        int i =1;
        while (i <=100){
            if(i % 2 == 0){
                System.out.println(i);
            }
            i++;
        }
        System.out.println(i); //101 
    }
}

do-while循环的使用
一、循环结构的4个要素
1 初始化条件 2 循环条件  --->是boolean类型
3 循环体 4 迭代条件
二、do-while循环结构:
1
do{
    3;
    4;}while(2);

执行过程:1 - 3 - 4 - 2 - 3 - 4- ... - 2
说明:
1.do-while循环至少会执行一次循环体!
2.开发中,使用for和while更多一些;较少使用do-while

public class DoWhileTest {
    public static void main(String[] args) {
        int num = 1;
        int sum = 0;//记录总和
        int count = 0;//记录个数
        do{
            if(num % 2 == 0){
//                System.out.println(num);
                sum += num;
                count++;
            }
            num++;
        }while(num <= 100);
        System.out.println(sum); //50
        System.out.println(count); //2550
    }
}

嵌套循环的使用
1.嵌套循环:将一个循环结构A声明在另一个循环结构B的循环体中,就构成了嵌套循环
2. 外层循环:循环结构B; 内层循环:循环结构A
3. 说明
   1 内层循环结构遍历一遍,只相当于外层循环循环体执行了一次
   2 假设外层循环需要执行m次,内层循环需要执行n次。此时内层循环的循环体一共执行了m * n次
4. 技巧: 外层循环控制行数,内层循环控制列数

 

break和continue关键字的使用
                  使用范围            循环中使用的作用(不同点)        相同点
break:            switch-case            
                 循环结构中            结束当前循环                    关键字后面不能声明执行语句    

continue:        循环结构中            结束当次循环                    关键字后面不能声明执行语句


/*
100000以内的所有质数的输出。实现方式二
质数:素数,只能被1和它本身整除的自然数。-->从2开始,到这个数-1结束为止,都不能被这个数本身整除。

对PrimeNumberTest.java文件中质数输出问题的优化
*/
class PrimeNumberTest2 {
	public static void main(String[] args) {
		int count = 0;//记录质数的个数
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long start = System.currentTimeMillis();
		label:for(int i = 2;i <= 100000;i++){//遍历100000以内的自然数
			for(int j = 2;j <= Math.sqrt(i);j++){//j:被i去除	
				if(i % j == 0){ //i被j除尽
					continue label;
				}	
			}
			//能执行到此步骤的,都是质数
			count++;
		}
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long end = System.currentTimeMillis();
		System.out.println("质数的个数为:" + count);
		System.out.println("所花费的时间为:" + (end - start));//17110 - 优化一:break:1546 - 优化二:13

	}
}
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。