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

     

    1.1 本质重点

    [1] 分支流程控制(if/if…else…/if…elseif…else/switch

    [2] 循环流程控制(while dowhile for)

    1.2 流程控制分类

    java中存在3流程控制

    顺序结构

    分支结构

    循环结构

    1.3 随机数(A)

    java如何产生随机数?

    public class Test01{
    
    public static void main(String[] args){
    
    // 产生一个随机数 范围是[0,1)
    
    double r = Math.random();
    
    System.out.println("r="+r);
    
    }
    
    }


    产生一个随机整数,范围在[10,30]? 

    [0,1) * 21 => (int)[0,21) => [0,20]+10=> [10,30]

    [0,1) * 11 => (int)[0,11) => [0,10]+10 =>[10,20]

    =>

    产生一个随机整数,范围在[m,n]

    (int)(Math.random()* (n-m+1)) + m

    [100,200] <=> (int)(Math.random()*(101)) + 100

    产生一个随机整数,范围在(m,n) <==> [m+1,n-1]

    [m,n)

    (m,n]

    // import java.lang.Math;
    
    public class Test01{
    
    public static void main(String[] args){
    
    // 产生一个随机数范围在[10,20]
    
    double r2 = (int)(Math.random()*(20-10+1)) + 10;
    
    System.out.println("r2 = " + r2);
    
    }
    
    }


    1.4 分支结构
     

    1.4.1 if 单分支

    if(表达式){
    
      语句…
    
    }


     如果表达式结果为true,就执行语句块中语句否则,跳转语句块。

    1.4.2 if 双分支

    if(表达式){
    
    语句1
    
    }else{
    
       语句2
    
    }

    1.4.3 if分支 

    if(表达式1){
    
     语句1
    
    }else if(表达式2){
    
    语句2
    
    }else if(表达式3){
    
      语句2
    
    }…
    
    else{
    
        语句n
    
    }
    import java.util.Scanner;
    
    public class Test05{
    
    public static void main(String[] args){
    
     
    
    // 需求:
    
    Scanner sc = new Scanner(System.in);
    
    System.out.println("请输入星期:");
    
    int week = sc.nextInt();
    
     
    
    if(week == 1){
    
    System.out.println("上课");
    
    }else if(week == 2){
    
    System.out.println("上课");
    
    }else if(week == 3){
    
    System.out.println("练习");
    
    }else if(week == 4){
    
    System.out.println("上课");
    
    }else if(week == 5){
    
    System.out.println("上课");
    
    }else if(week == 6){
    
    System.out.println("上课");
    
    }else{
    
    System.out.println("练习");
    
    }
    
    }
    
    }


    注意: 

    理论else if 没有数量限制,但代码的优雅性出发,建议分支不要操作4

    // 优化
    
    if(week == 3 || week == 7){
    
    System.out.println("练习");
    
    }else{
    
    System.out.println("上课");
    
    }


    能用单分支,就不用双分支; 

    能用双分支,就不用多分支。

    需求:对成绩评级

    if(score >= 90){
    
    System.out.println("优秀");
    
    }else if(score >=80){
    
    System.out.println("良好");
    
    }else if(score >= 60){
    
    System.out.println("中等");
    
    }else{
    
    System.out.println("差");
    
    }
    if(score < 60){
    
    System.out.println("差");
    
    }else if(score < 80){
    
    System.out.println("中等");
    
    }else if(score < 90){
    
    System.out.println("良好");
    
    }else{
    
    System.out.println("优秀");
    
    }

    总结:

    大于大的值,用 >

    小于小的值,用 < 

    1.4.4 if 嵌套

    实际开发过程中,很多时候需要进行多个条件的分支

    public class Test10{
    
    public static void main(String[] args){
    
     
    
    int time = 9;
    
    char gender = '男';
    
    if(time < 10){
    
    // 决赛
    
    if(gender == '男'){
    
    System.out.println("进入男子组");
    
    }else{
    
    System.out.println("进入女子组");
    
    }
    
    }else{
    
    // 淘汰
    
    System.out.println("没能进入决赛")
    
    }
    
    }
    
    }


    1.5 switch 分支
     

    switch 多值匹配分支语法:

    switch (表达式)  {
    
    case 值1 :
    
    语句序列;
    
    [break];
    
    case 值2:
    
     语句序列;
    
    [break] ;
    
         … … …      … …
    
    [default:
    
     默认语句 ;]
    
    }
    
     
    public class Test12{
    
    public static void main(String[] args){
    
    int week = 8;
    
     
    
    switch(week){
    
    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;
    
    }
    
    }
    
    }
    
    }

    表达式的值是什么类型时可以支持匹配整形、字符

    jdk1.7之后,支持字符串匹配不到迫不得已,不要用字符串匹配。

     

    特殊情况1:省略break造成错误匹配。-> 直到遇到break结束。

    public class Test15{
    
    public static void main(String[] args){
    
     
    
    char c = 'e';
    
     
    
    // aeiou 元音
    
    switch(c){
    
    case 'a':{
    
    System.out.println("元音"+c);
    
    // break;
    
    }
    
    case 'e':{
    
    System.out.println("元音"+c);
    
    // break;
    
    }
    
    case 'i':{
    
    System.out.println("元音"+c);
    
    break;
    
    }
    
    case 'o':{
    
    System.out.println("元音"+c);
    
    break;
    
    }
    
    case 'u':{
    
    System.out.println("元音"+c);
    
    break;
    
    }
    
    default:{
    
    System.out.println("辅音"+c);
    
    break;
    
    }
    
    }
    
     
    
    }
    
    }

    特殊情况2:合理地省略break

    public class Test15{
    
    public static void main(String[] args){
    
     
    
    int week = 6;
    
     
    
    switch(week){
    
    case 1:
    
    case 2:
    
    case 4:
    
    case 5:
    
    case 6:{
    
    System.out.println("上课");
    
    break;
    
    }
    
    default:{
    
    System.out.println("练习");
    
    break;
    
    }
    
    }
    
     
    
    }
    
    }
    本文作者:___mouM
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。
    版权说明:本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.
  • 相关阅读:
    Java中的数学计算函数汇总
    安卓杂记(三)利用自定义的PolyBezier()函数将一系列散点绘制成光滑曲线(一)
    安卓杂记(二)利用FrameLayout叠加多种view的方法
    安卓问题报告小记(一): Activity not started, its current task has been brought to the front
    安卓杂记(一) 获取时间总结整理
    node api
    javascript 坑
    async
    promise
    js去除数组中的重复项
  • 原文地址:https://www.cnblogs.com/aknife/p/10784481.html
Copyright © 2011-2022 走看看