流程控制语句(一)选择结构

孤人 提交于 2020-02-11 18:03:12

流程控制语句

1 顺序结构: 代码从主函数开始逐行向下运行也是一种宏观的代码运行结构。

2 选择结构 :  ①if语句           ②switch语句

3 循环结构:  ①for循环          ②while循环

选择结构

1.   if语句    :生活中的很多事情都是在满足一定条件下发生的,同样,程序中的“某操作语句”也是在满足一定逻辑条件下才执行的,这种语句称作条件语句,或称为“if 语句”。

显然,if 语句是一种分支结构,当条件满足时,有“执行该操作语句”和“跳过执行该操作语句”的两条分支。                                                                                 

基本格式

if(条件表达式){

      当条件为true时执行的代码;

}else

      当条件为false时执行的代码;

}

2.if-else 语句的执行流程:首先判断关键词if后括号内条件表达式的值,如果该表达式的值为逻辑真(非 0),则执行 if 体(语句 A),而不执行 else 体(语句 B),然后继续执行 if-else 之后的其他语句;否则,若该表达式的值为逻辑假(0),则不执行该 if 体(语句 A),而执行 else 体(语句 B),然后继续执行 if-else 之后的其他语句

 

if(条件1){

      条件1为true时执行的代码;

      if(条件2){

          条件2为true时执行的代码;

      }else{

          条件2为false时执行的代码;

      }

}else{

      条件1为false时执行的代码;

}

3.if-else-if形式

if(条件1){

 

}else if(条件2){

 

}else if(条件3){

 

}else{

 

}

 

4.switch语句  :

Switch用在编程中,它经常跟case一起使用,是一个判断选择代码。其功能就是控制流程流转的。

直线翻译:switch语句,即“切换”语句;case即“情况”。

switch语句的语法如下(switch,case,break和default是关键字):

switch(变量){

    case 值1:

          执行语句1;

          break;

    case 值2:

          执行语句2;

          break;

    case 值3:

          执行语句3;

          break;

    ........

    deafult:

          执行语句n;

          break;

}

5.if与switch的区别:

1.if它既可以对一个单一的值进行判断,也可以对一个区间的值进行判断

switch只能判断单一的值

2.switch所要求的变量的数据类型:byte short int char String 枚举

3.在 switch 语句中条件只求值一次并用来和每个 case 语句比较。在 elseif 语句中条件会再次求值。如果条件比一个简单的比较要复杂得多或者在一个很多次的循环中,那么用 switch 语句可能会快一些。

3.练习

1.if-else

 

import java.util.Scanner;

public class zy33{

    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        System.out.print("今天是周");

        int today = scanner.nextInt();

        System.out.print("未来的几天");

        int future = scanner.nextInt();    

        int futureDay = (today+future)%7;    //周几加后几天对7取余

        if((today+future)%7==0){                //只把特殊的周天指出,就不用switch写出

            System.out.print("今天是周天");

        }else{

            System.out.println("今天是周"+futureDay);

        }

    }

}

 

2.if-else-if-

/*

数据:a b c delt r1 r2

步骤:

1.提示用户输入abc三个参数

2.计算delt=b*b-4*a*c

3.判断delt的值

    3.1 delt>0

        输出两个解

    3.2 delt==0

        输出一个解

    3.3 delt<0

        无实数解

*/

import java.util.Scanner;

class  Demo03_01{

    public static void main(String[] args){

        //1.

        Scanner scanner=new Scanner(System.in);

        System.out.print("请输入a,b,c:");

        double a=scanner.nextDouble();

        double b=scanner.nextDouble();

        double c=scanner.nextDouble();

        //2.

        double delt=b*b-4*a*c;

        //3.

        if(delt>0){

            double r1=(-b+Math.sqrt(delt))/(2*a);

            double r2=(-b-Math.sqrt(delt))/(2*a);

            System.out.printf("r1=%.2f,r2=%.2f",r1,r2);

        }else if(delt==0){

            double r=(-b-Math.sqrt(delt))/(2*a);

            System.out.printf("r=%.2f",r);

        }else{

            System.out.println("无实数解!");

        }

    }

}

3.switch

 

 import java.util.Scanner;

class Demo03_07{

    public static void main(String[] args){

        Scanner scanner=new Scanner(System.in);

        //1.先输入年份

        System.out.print("请输入年份:");

        int year=scanner.nextInt();

        //2.输入月份 1月 2月分别用13 14代替 同时year-1

        System.out.print("请输入月份:");

        int month=scanner.nextInt();

        //3.输入日期

        System.out.print("请输入日期:");

        int day=scanner.nextInt();

 

        //4.对特殊的1月和2月做处理

        if(month==1||month==2){

            month+=12;

            year-=1;

        }

        //5.套公式

        int h=(day+26*(month+1)/10+year%100+year%100/4+year/100/4+5*year/100)%7;

        

        switch(h){

            case 0:

                System.out.println("是周六");

                break;

            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;

        }

    }

}

 

 

 

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!