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

    java流程控制

    一、用户交互Scanner

    scanner:实现程序与人的交互(可以获取用户的输入)

    基本语法:

    Scanner scanner = new Scanner(System.in);
    

    通过scanner类的next()与nextline()方法获取输入字符串,在读取前我们需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

    注:

    1. 用完scanner后一定要关闭,这样可以节省一些资源,养成好习惯

    2. next():

      1. 一定要读取到有效字符后才可以结束输入
      2. 对输入有效字符之前遇到空白,next()方法会自动将其去掉。
      3. 只有输入有效字符后才将其后面输入的空白作为分隔符或结束符
      4. next()不能得到带有空格的字符串
    3. nextline():

      1. 以回车为结束符,即返回的是回车之前的所有字符。
      2. 可以获得空白。
    //Demoo1
    public class Demo01 {
        public static void main(String[] args) {
            //创建一个扫描器对象,用于接收键盘数据
             Scanner scanner = new Scanner(System.in);
    
            System.out.println("使用next方式接收:");
    
            //判断用户有没有输入字符串
            if (scanner.hasNext()){
                //使用next方式接收
                String str = scanner.next();//程序会等待用户输入完毕在执行
                System.out.println("输出的内容为:"+str);
            }
    
            //范式属于IO流的类如果不关闭会已知占用资源,要养成好习惯用完就关掉
            scanner.close();
        }
    }
    //Demo02
    public class Demo02 {
        public static void main(String[] args) {
            //从键盘接收数据
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入数据:");
    
            //判断是否还有输入:
            if (scanner.hasNextLine()){
                String tre = scanner.nextLine();
                System.out.println("输入的内容:"+tre);
            }
    
            scanner.close();
        }
    }
    //Demo05
    public class Demo05 {
        public static void main(String[] args) {
            //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结果输入并输出执行结果
            Scanner scanner = new Scanner(System.in);
    
            //和
            double sum = 0;
            //计算输入了多少个数
            int m = 0;
    
            //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
            while(scanner.hasNextDouble()){
                double x = scanner.nextDouble();
                m = m + 1;//m++
                sum = sum + x;
                System.out.println("你输入了第"+m+"个数据,当前结果sum为:"+sum);
    
            }
            System.out.println(m+"个数的和为"+ sum);
            System.out.println(m+"个数的平均值为"+ (sum/m) );
    
            scanner.close();
        }
    
    }
    

    二、顺序结构

    顺序结构是最简单的算法结构,也是任何一个算法离不开的一种基础结构;JAVA最基本结果就是顺序结果,除非特别指明,否则就按照顺序一句一句执行。

    三、选择结构

    • if选择结构

      if判断语句可以单个用也可以嵌套用;

      public class IfDemo03 {
          public static void main(String[] args) {
              Scanner  scanner = new Scanner(System.in);
              int score = scanner.nextInt();
      
              if (score==100){
                  System.out.println("恭喜满分");
              }else if (score <100 && score>90){
                  System.out.println("A级");
              }else if (score <90 && score>80){
                  System.out.println("B级");
              }else if (score <80 && score>70){
                  System.out.println("C级");
              }else if (score <70 && score>60){
                  System.out.println("D级");
              }else if (score <60 && score>0){
                  System.out.println("不及格");
              }else{
                  System.out.println("成绩不合法");
              }
              scanner.close();
          }
      }
      
    • switch选择结构

      判断一个变量与一系列之中某个值是否相等,每一个值称为一个分支;

      注:

      1. switch语句中的变量类型可以是:byte short int 或者char,从JAVASE7开始可以是String类型(本质还是数字);

      2. case标签必须为字符串常量或字面量;

      3. 没写一个case,别忘了要加break,阻止case的穿透能力

        //Demo01
        public class SwtichDemo01 {
            public static void main(String[] args) {
                //case 穿透  //switch匹配一个具体的值
               char grade = 'F';
        
               //小心case的穿透效果,所以写完case一定要加break
               switch (grade){
                   case 'A':
                       System.out.println("优秀");
                       break;
                   case 'B':
                       System.out.println("良好");
                       break;
                   case 'C':
                       System.out.println("及格");
                       break;
                   case 'D':
                       System.out.println("再接再厉");
                       break;
                   case 'E':
                       System.out.println("挂科");
                       break;
                   default:
                       System.out.println("未知等级");
        
        
               }
        
            }
        }
        //Demo02
        public class SwitchDemo02 {
            public static void main(String[] args) {
                String name = "小猪";
                //JDK7的新特性,表达式结果可以是字符串
                //字符的本质还是数字
        
                //反编译   java---class(字节码文件)---反编译(IDEA)
                switch (name){
                    case "小猪":
                        System.out.println("小猪");
                        break;
                    case "鬼谷":
                        System.out.println("鬼谷");
                        break;
                    default:
                        System.out.println("弄啥嘞");
                }
            }
        }
        

    四、循环结构

    • while循环

      定义形式:

      while(布尔表达式){
          //循环主题
      }
      

      注:

      1. 布尔值为true,循环就会一直执行下去
      2. 我们大多数情况是会让循环停止下来的,所以我们需要一个让表达式失效的方式来结束循环。
      3. 少部分情况需要循环一直执行,比如服务器的请求响应、监听等
      4. 循环条件一直为true就会造成无限循环【死循环】,正常业务的时候应该尽量避免出现死循环,以提高程序性能或减少系统卡死崩溃机会
      public class WhileDemo03 {
          public static void main(String[] args) {
           //  计算1+2+3+4+。。+100=?
              //输出1到100
              int a = 0;
              while (a<100){
                  a++;
                  if (a==1){
                      System.out.println(a);
                  }else {
                      System.out.println("+"+a);
                  }
              }
      
      
              //求1加到100的和
              int i = 0;
              int sum = 0;
              while(i<100){
                  i++;
                  sum = sum +i;
              }
              System.out.println("结果为:"+sum);
          }
      }
      
    • do。。。。while循环

      特点:至少会执行一次

      定义:do{

      ​ //代码语句

      ​ }while(布尔表达式)

      public class DoWhileDemo02 {
          public static void main(String[] args) {
              int a = 0;
              while(a<0){
                  System.out.println(a);
                  a++;
              }
              System.out.println("=============");
              do {
                  System.out.println(a);
                  a++;
              }while(a<0);
      
          }
      }
      

    注:while和do....while的区别:

    1. while先判断后执行,do...while是先执行后判断
    2. do...while总是保证循环体会被至少执行一次,
    • for循环

      ①:最有效、最灵活的循环结构

      ②:循环次数在执行前就确定的

      语法形式:

      ​ for(初始化 ; 布尔表达式 ; 更新){

      ​ //代码语句

      ​ }

      说明:

      1. 最先执行初始化步骤,可以声明一种类型
      2. 但可初始化一个或多个循环变量,也可以是空语句
      3. 都是空语句则为死循环
      public class ForDemo01 {
          public static void main(String[] args) {
              int a = 1;//初始化条件
      
              while(a <= 100){//条件判断
                  System.out.println(a);//循环体
                  a+=2;//迭代
              }
              System.out.println("while循环结束!");
      
              //(初始化;条件判断;迭代)
              for (int i = 0;i<=100;i++){
                  System.out.println(i);
              }
              System.out.println("for循环结束!");
      
      
              //死循环
              for ( ; ;){
      
              }
          }
      }
      
    • 增强for循环(JAVA5 引入)

      主要用于数组集合

      定义形式:
          for(声明语句 : 表达式){
              //代码句子
          }
      
      
      //增强for——Demo
      public class ForDemo05 {
          public static void main(String[] args) {
              int[]  numbers ={10,20,30,40,50};//定义一个数组
      
              for (int i = 0; i < 5; i++) {
                  System.out.println(numbers[i]);
              }
              System.out.println("======================================");
      
              //遍历数组的元素
              for (int x:numbers){
                  System.out.println(x);
              }
          }
      }
      
    • 练习

      ​ 练习1:计算0到100之间的奇数和偶数和
      ​ 练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
      ​ 练习3:打印九九乘法表

      public class LianXi {
          public static void main(String[] args) {
      
      
              //练习1:计算0带100之间的奇数和偶数和
              //方法1:
              int a = 0;
              int b = 0;
              int n = 0;
              int sum1 = 0;
              int sum2 = 0;
              //奇数和
              while (n < 50){
                  a= 2*n+1;
                  sum1 = sum1 + a;
                  System.out.println(a);
                  n++;
              }
              System.out.println(sum1);
              //偶数和
              for (int i = 0; i < 50; i++) {
                  b = 2*i;
                  sum2 = sum2 + b;
                  System.out.println(b);
              }
              System.out.println(sum2);
      
              //方法2:
              int oddSum = 0;
              int evenSum = 0;
      
              for (int c = 0; c < 100; c++) {
                  if (c%2!=0){//奇数
                      oddSum+=c;//oddSum=oddSum+c
                  }else{//偶数
                      evenSum+=c;
                  }
              }
              System.out.println("奇数的和:"+oddSum);
              System.out.println("偶数的和:"+evenSum);
      
          }
      }
      
      
      
      public class LianXi02 {
          public static void main(String[] args) {
      
              //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
              //方法一:
              int d = 0;
              for (int i = 0; i <= 1000; i++) {
                  if (i%5 == 0){
                      if (d==2){
                          System.out.println(i);
                          d=0;
                      }else{
                          System.out.print(i);
                          System.out.print(" ");
                          d++;
                      }
                  }
              }
      
              //方法2:
              for (int i = 0; i <=1000; i++){
                  if (i%5==0){
                      System.out.print(i+"	");
                  }
                  if (i%(5*3)==0){//每行
                      System.out.println();
                      //System.out.println("
      ");
                  }
              }
      
              //println输出完会换行
              //println输出完不会换行
          }
      }
      
      
      
      public class LianXi03 {
          //练习3:打印九九乘法表
          public static void main(String[] args) {
              //1.我们先打印第一列,这个大家应该都会
              //2.我们把固定的1再用一个循环包起来
              //3.去掉重复项,i<=j
              //4.调整样式
      
      
              for (int i = 1; i <=9; i++) {
                  for (int a = 1; a <= i; a++) {
                      System.out.print(i+"*"+a+"="+(i*a)+"	");
                  }
                  System.out.println();
              }
          }
      }
      
      

    五、break continue

    break:强行退出循环,不执行剩下语句(在switch中有阻止case穿透作用)

    continue:终止某次循环,接着去做判断看是否执行下一次循环

    六、练习

    //打印三角形 5行:

    public class TestDemo {
        //打印三角形  5行:
        public static void main(String[] args) {
            int cont =0;
            for (int i = 0; i < 5; i++) {//5行
                for (int j =5; j>i;j--){
                    System.out.print(" ");
                }
                for (int a =0;a<i;a++){
                    System.out.print("*");
                }
                for (int b =0;b<=i;b++){
                    System.out.print("*");
                }
                System.out.println();
            }
    
        }
    }
    
  • 相关阅读:
    CentOS安装使用.netcore极简教程(免费提供学习服务器)
    新生命团队netcore服务器免费开放计划
    线程池ThreadPool及Task调度死锁分析
    NetCore版RPC框架NewLife.ApiServer
    NewLife.Net——管道处理器解决粘包
    NewLife.Net——网络压测单机2266万tps
    NewLife.Net——构建可靠的网络服务
    NewLife.Net——开始网络编程
    搬家
    借助Redis做秒杀和限流的思考
  • 原文地址:https://www.cnblogs.com/liustudy/p/14021531.html
Copyright © 2011-2022 走看看