zoukankan      html  css  js  c++  java
  • 流程控制

    流程控制

    1.有哪些:

    • 选择结构:

      ​ if-else switch

    • 循环结构:

      ​ while do-while for

    • 跳转:

      ​ break continue return

    2.选择结构:

    (1).if单分支,if-else;
    a.if单分支:

    ​ 流程图:

    ![if单分支]

    public class IfDemo{
    	public static void main (String[] args){
    		//int i = (int)(Math.random()*6);
    		//单分支判断,Math.random()产生数据的范围是[0.0,1.0)
    		//得到0-5之间的随机数;
    		//if(i>3){
    			//System.out.println("值大于3");
    		//}
    		//System.out.println("number:"+i);
    		
    		
    		double i = 6 * Math.random();
    		double j = 6 * Math.random();
    		double k = 6 * Math.random();
    		int count = (int) (i + j + k);
    		if(count > 15){
    			System.out.println("今天手气很不错");
    		}
    		if(count >= 10 && count <= 15){
    			System.out.println("今天手气一般");
    		}
    		if(count < 10){
    			System.out.println("今天手气不怎么样");
    		}
    		System.out.println("得了" + count + "分");
    	}
    }
    
    b.if-else:

    ​ 流程图:

    多重if-else

    public class IFDemo {
        public static void main(String[] args) {
    //        根据预算金额选购车辆
    //        预算>100万 奔驰s级
    //        预算>50万 奔驰5系
    //        预算>10万 奥迪A4l
    //        预算<10万 捷安特
            int money = 100; //单位: 万
    
            if (money > 100) {
                System.out.println("奔驰s级");
            } else if (money > 50) {
                System.out.println("奔驰5系");
            } else if (money > 10) {
                System.out.println("奥迪A4l");
            } else {
                System.out.println("捷安特");
            }
    
            System.out.println("程序结束");
        }
    
    }
    
    (2).switch:

    根据表达式值的不同执行许多不 同的操作

    import java.util.Scanner;
    /*
    	利用while循环嵌套switch结构,当输入正确成绩时,会循环继续;当输入成绩不合法时,会结束循环.
    */
    public class SwitchDemo {
        public static void main(String[] args) {
              boolean Flag = true;
            while (Flag){
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入你的成绩: ");
            int score = sc.nextInt();
            //2.根据成绩判断学生的等级:
            switch (score / 10) {
                case 10:
                case 9:
                    System.out.println("A级");
                    break;
                case 8:
                    System.out.println("B级");
                    break;
                case 7:
                    System.out.println("C级");
                    break;
                case 6:
                    System.out.println("D级");
                    break;
                case 5:
                case 4:
                case 3:
                case 2:
                case 1:
                case 0:
                    System.out.println("E级");
                    break;
            default:
                System.out.println("成绩错误");
                return;
             }
            }
        }
    }
    

    switch语句会根据表达式的值从相匹配的执行,与任一case不匹配,则进入default语句,default为可选字句;

    switch和多重if选择结构:

    相同点:都是用来处理多分支条件的结构

    不同点:

    switch:只能处理等值条件判断的情况,而且条件必须是整型变量或字符变量或字符串(jdk 1.7之后)

    多重if:没有switch选择结构的限制,适合某个变量处于连续区间的情况.

    3.循环结构
    (1).while循环:

    ​ 初始化变量
    ​ while(循环条件) //布尔值
    ​ 功能代码块(循环体)
    ​ 迭代

    ​ 流程图:

    public class WhileDemo {
        //while(布尔表达式){
        // 逻辑代码(循环操作)}
        //输出100遍hello world
        public static void main(String[] args){
            int i = 1; //初始部分:用以判断的变量
            while (i <= 100){//循环条件:决定是否继续循环的依据
                System.out.println("Hello World"); //循环操作:单词执行的逻辑代码或任务
                i++; //迭代:控制循环条件改变的增量
            }
            System.out.println("程序结束");
        }
    }
    
    public class NewDemo{
    	public static void main(String[] args){
            //1000以内被5整数得数,每行输出三个
    		int i = 0;
    		int count = 0;
    		while(i<1000){
    			if(i%5==0){
    				System.out.print(i+"	");
    				count++;
    			}
    			i++;
    			if(count>=3){
    				System.out.println();
    				count = 0;
    			}
    		}
    	}
    }
    
    (2)do-while 循环:

    ​ 流程图:

    class TestDoWhile{
       public static void main(String[] args){
           //循环操作:学生写代码,教师输入评语
            Scanner input = new Scanner(System.in);
            char answer;
           do {
               System.out.println("抄写一遍....");
               System.out.println("请教师输入评语: ");
               answer = input.next().charAt(0);
           }while(answer != 'y');
           System.out.println("程序结束!");
       }
    }
    

    do-while 先执行一次循环体,在进行布尔逻辑判断;while先执行逻辑判断,在进行循环

    (3.)for循环:

    ​ 流程图:

    public class forDemo {
        public static void main(String [] args){
            int sum = 0;
            for(int i = 1; i <= 100; i++){
                sum += i;
               System.out.println(sum);
            }
        }
    }
    
    public class TestFor{
        public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        //计算5位同学的平均分
    
        double sum= 0.0;
        for(int i= 1;i<=5;i++){
            //循环控制台录入分数
            System.out.println("请输入第"+ i +"位同学的成绩: ");
            double score = input.nextDouble();
    
            //累加综合
            sum = sum + score;
        }
        double avg = sum / 5;
        System.out.println("平均分: "+ avg);
        }
    }
    

    for循环优点:

    a.代码简洁:

    b.变量初始化的时候,for循环的作用域仅仅是当前for循环结构;

    while循环的作用域是从变量的定义开始到整个方法结束.

    4.跳转:

    (1)break:用于强行退出循;包含多层循环嵌套时,break只能跳出内层循环;

    (2).continue:用于终止某次循环过程,即跳出循环体尚未执行的语句

    (3).return:从当前方法退出,返回到调用该方法的语句出,病从该语句的下条语句处执行程序

    本文来自博客园,作者:小幸福Y,转载请注明原文链接:https://www.cnblogs.com/ljinw/p/14033262.html

  • 相关阅读:
    flock对文件锁定读写操作的问题 简单
    hdu 2899 Strange Fuction(二分)
    hdu 2199 Can you solve this equation? (二分)
    poj 3080 Blue Jeans (KMP)
    poj 2823 Sliding Window (单调队列)
    poj 2001 Shortest Prefixes (trie)
    poj 2503 Babelfish (trie)
    poj 1936 All in All
    hdu 3507 Print Article (DP, Monotone Queue)
    fzu 1894 志愿者选拔 (单调队列)
  • 原文地址:https://www.cnblogs.com/ljinw/p/14033262.html
Copyright © 2011-2022 走看看