Java流程控制学习笔记

Java 流程控制

用户交互Scanner

Scanner 对象

  • java.util.Scanner是java5的新特征,我们可以通过Scanner类来获取用户的输入。

  • 基本语法:

Scanner s =new Scanner(System.in);
  • 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入数据。

    1. next():

      • 一定要读取到有效字符后才可以结束输入

      • 对输入有效字符之前遇到的空白,next()方法会自动将其去掉

      • 只有输入有效字符后才将后面输入的空表作为分隔符或者结束符

      • next()不能得到带有空格的字符串

      package com.ronghui.scanner;

      import java.util.Scanner;

      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();
        }
      }

       

    2. nextLine():

      • 易Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。

      • 可以获取空白。

      package com.ronghui.scanner;

      import java.util.Scanner;

      public class Demo02 {
         public static void main(String[] args) {
             //从键盘接收数据
             Scanner scanner = new Scanner(System.in);
             System.out.println("使用nextLine方式接受:");
             //判断是否还有输入
             if (scanner.hasNextLine()){
                 String str= scanner.nextLine();//等待用户输入
                 System.out.println("输入的内容为:"+str);
            }
             scanner.close();
        }
      }
      1. 补充说明上面的程序中if可以去掉。

        package com.ronghui.scanner;

        import java.util.Scanner;

        public class Demo03 {
           public static void main(String[] args) {
               //从键盘接收数据
               Scanner scanner = new Scanner(System.in);
               System.out.println("请输入数据:");

               String str= scanner.nextLine();//等待用户输入
               System.out.println("输入的内容为:"+str);
               scanner.close();
          }
        }
        package com.ronghui.scanner;

        import java.util.Scanner;

        public class Demo04 {
           public static void main(String[] args) {
               Scanner scanner = new Scanner(System.in);

               //从键盘接受数据
               int i=0;
               float f=0.0f;
               System.out.println("请输入整数:");

               //如果。。。那么。。。
               if (scanner.hasNextInt()){
                   i = scanner.nextInt();
                   System.out.println("整数数据:"+i);
              }else{
                   System.out.println("输入的不是整数数据");
              }

               System.out.println("请输入小数:");

               //如果。。。那么。。。
               if (scanner.hasNextFloat()){
                   f = scanner.nextFloat();
                   System.out.println("小数数据:"+f);
              }else{
                   System.out.println("输入的不是小数数据");
              }



               scanner.close();
          }
        }
        package com.ronghui.scanner;

        import java.util.Scanner;

        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的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句的执行

  • 顺序结构是最简单的算法结构

  • 语句与语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,他是任何一个算法都离不开的一种基本算法结构。

    package com.ronghui.struct;

    public class Demo01 {
       //顺序结构
       public static void main(String[] args) {
           System.out.println("hello1");
           System.out.println("hello2");
           System.out.println("hello3");
           System.out.println("hello4");
           System.out.println("hello5");
      }
    }

     

选择结构

  • if单选择结构

    package com.ronghui.struct;

    import javax.print.DocFlavor;
    import java.util.Scanner;

    public class IfDemo01 {
       public static void main(String[] args) {
           Scanner scanner = new Scanner(System.in);
           System.out.println("请输入内容:");
           String s=scanner.nextLine();
    //判断字符串是否相等的
           if (s.equals("Hello")){
               System.out.println(s);
          }

           System.out.println("End");

           scanner.close();
      }
    }

     

  • if双选择结构

    package com.ronghui.struct;
    
    import java.util.Scanner;
    
    public class IfDemo02 {
        public static void main(String[] args) {
            //考试分数大于60就是及格,小于60就是不及格
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入成绩:");
            int score = scanner.nextInt();
            if(score>=60){
                System.out.println("及格");
            }else{
                System.out.println("不及格");
            }

    08

  • if多选择结构

    package com.ronghui.struct;
    
    import java.util.Scanner;
    
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            /**
             * if语句至多有一个else语句,在else if语句之后
             * if语句中可以有若干个 else if语句,必须在else语句之前。
             * 一个else if语句为true,其他的else if和else语句都将跳过执行
             */
            System.out.println("请输入成绩:");
            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();
        }
    }
  • 嵌套的if结构

    意思就是if语句里面能再次只用if语句。

  • switch多选择结构

    package com.ronghui.struct;
    
    public class SwitchDemo02 {
        public static void main(String[] args) {
            String name ="欧荣辉";
            //JDK7新特性,表达式结果可以是字符串
    //字符的本质还是数字
            //反编译
    
            switch (name){
                case "余欣":
                    System.out.println("余欣");
                    break;
                case"欧荣辉":
                    System.out.println("欧荣辉");
                    break;
                default:
                    System.out.println("弄啥了");
    
            }
        }
    }
    package com.ronghui.struct;
    
    public class SwitchDemo01 {
        public static void main(String[] args) {
            //case穿透  switch 匹配一个具体的值
            char grade='C';
            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("未知等级");
    
            }
        }
    }

     

循环结构

while循环

大多数情况会让循环停下来,需要让一个表达式失效的方式来结束循环。

package com.ronghui.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+。。+100=?
        int i=0;
        int sum=0;
        while (i<100){
            ++i;
            sum=sum+i;
        }
        System.out.println(sum);

    }
}

避免死循环

do。。。while循环

package com.ronghui.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        do{
          sum=sum+i;
          i++;

        }while(i<=100);
        System.out.println(sum);
    }
}
package com.ronghui.struct;

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);
    }
}

 

for循环

for循环,使一些循环结构变得更简单。

for循环是知识迭代的一种通用结构,是最有效、最灵活的循环结构

package com.ronghui.struct;

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=1;i<=100;i++){//初始化;条件判断;迭代
            System.out.println(i);

        }
        System.out.println("for循环结束!");
    }
}

for循环注意点:

最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。然后检测到布尔表达式的值,如果为true,新欢被执行,如果为false,循环终止,开始执行循环体后边的语句。

执行一次循环后,更新循环控制变量(迭代因子控制变量的增减)。

再次检测布尔值表达式。循环上面的过程。

练习1:

package com.ronghui.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        //计算0到100之间奇数和偶数的和
        int oddSum=0;
        int evenSum=0;
        for (int i = 0; i <=100; i++) {
            if(i%2!=0){//奇数
                oddSum+=i;
            }else{//偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数之和为:"+oddSum);
        System.out.println("偶数之和为:"+evenSum);

    }
}

练习2:

package com.ronghui.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //用while或这for循环输出1-1000之间能被5整除的数,并且每行输出3个
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if(i%15==0){
                System.out.println();
            }
            //println 输出完会换行
            //print输出完不会换行
        }
    }
}

练习3:打印99乘法表

package com.ronghui.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
        //1.首先打印第一列,输出1*1~1*9
        //2.把固定的1再用循环包起来,把1变成变量
        //3.使用i<=j来去掉重复项
        //4.调整样式
    }
}

for 。。。增强:

package com.ronghui.struct;

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);
        }
    }
}

 

break&continue

  • break在任何循环语句的主题部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。break语句也在switch语句中使用

    package com.ronghui.struct;
    
    public class BreakDemo01 {
        public static void main(String[] args) {
            int i=0; 
            while (i<100){
                i++;
                System.out.println(i);
                if (i==30){
                    break;
                }
            }
        }
    }

     

  • continue语句用在循环语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行的循环判断。

    package com.ronghui.struct;
    
    public class ContinueDemo01 {
        public static void main(String[] args) {
            int i=0;
            while(i<100){
                i++;
                if (i%10==0){
                    System.out.println();
                    continue;
                }
                System.out.print(i+"\t");
            }
        }
    }

     

练习

package com.ronghui.struct;

import java.awt.*;

public class TestDemo01 {
    public static void main(String[] args) {
        //打印三角形 5行
        for (int i = 1; i <= 5; i++) {
            for (int j=5;j>=i;j--){
                System.out.print(" ");
            }
            for (int j=1;j<=i;j++){
                System.out.print("*");
            }
            for (int j=1;j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

 

 

posted @ 2020-08-12 10:38  欧荣辉  阅读(288)  评论(0)    收藏  举报