Java基础02:流程控制

Java基础02:流程控制

目录

  1. 用户交互Scanner

  2. 顺序结构

  3. 选择结构

  4. 循环结构

  5. break & continue

  6. 练习

 

Scanner对象

  • java.util.Scanner 是java5的新特性,可以通过Scanner类获取用户的输入

  • 基本语法

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

     

  • next()

    1. 一定要读取到有效的资方后才可以结束输入

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

    3. 只有输入有效字符后,才将其后面输入的空白作为分隔符或者结束符

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

  • nextLine()

    1. 以Enter为结束符,nextLine()方法返回的是输入回车前的所有字符

    2. 可以获取空格

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);
      }
       scanner.close();//io流的类,需要在使用完毕后关闭


  }
}
// 输入:hello world 输出:hello
----------
public class Demo02 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("使用next方式接收参数:");
       //判断用户有没有输入字符串
       if (scanner.hasNextLine()){
           //nextLine()接收参数
           String str =scanner.nextLine();
           System.out.println("输出的内容为:"+str);
      }
       scanner.close();


  }
}
// 输入:hello world 输出:hello world
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();

  }
}
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;
           sum=sum+x;
           System.out.println("你输入的第"+m+"个数据,然后当前结果sum="+sum);
      }
       System.out.println(m+"个数的和为:"+sum);
       System.out.println(m+"个数的平均数为:"+(sum/m));
       scanner.close();

  }
}

 

顺序结构

  • java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行

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

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

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

选择结构

  • if单选择结构

    • 判断一个东西是否可行,这样过程在程序中可以使用if语句表示

    • 语法

      if(布尔表达式){
         //如果布尔表达式为true,将执行{}内的语句
      }
      public class IfDemo02 {
       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双选择结构

    • 语法

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}
import java.util.Scanner;

public class IfDemo03 {
   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("不及格");
      }

       scanner.close();
  }
}
 

 

  • if多选择结构

    if(布尔表达式1){
       //如果布尔表达式1的值为true,执行代码
    }else if(布尔表达式2) {
       //如果布尔表达式2的值为true,执行代码
    }else if(布尔表达式3) {
       //如果布尔表达式3的值为true,执行代码
    }else{
       //如果以上布尔表达式的值为false,执行代码
    }
    public class IfDemo04 {
       public static void main(String[] args) {
           //成绩级别
           /*100 恭喜满分
            *[90,100) A
            *[80,90) B
            *[70,80) C
            *[60,70) D
            *[0,60) 不及格
            *其他不合法
            *
            **/
           System.out.println("请输入成绩");

           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();
      }
    }
  • 嵌套的if多选择结构

  • switch多选择结构

    • switch case 语句判断一个变量一系列值是否相等,每个值称为一个分支

    • switch语句的变量类型可以是:

      • byte,short,int或者char

      • 从java7开始支持String类型,同事case标签必须为字符串常量或者字面量

switch(expression){
   case value1:
       //语句
       break;
   case value2:
       //语句
       break;
   default://可选
       //语句
}
public class SwitchDemo05 {
   public static void main(String[] args) {
       //
       char grade ='A';

       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("未知等级");
               break;
      }
  }
}
public class SwitchDemo06 {
   public static void main(String[] args) {
       String name="测试";

       switch (name){
           case "大大":
               System.out.println(name);
               break;
           case "订单":
               System.out.println(name);
               break;
           case "等待":
               System.out.println(name);
               break;
           case "测试":
               System.out.println(name);
               break;
           default:
               System.out.println("未知信息");
      }
  }
}

 

循环结构

  • while循环

    • while循环为基本循环之一

    while(布尔表达式){
       //循环内容
    }
    public class WhileDemo07 {
       public static void main(String[] args) {
           //输出1-100
           int i=0;
           while (i<100){
               i++;
               System.out.println(i);
          }
      }
    }
    注意:布尔表达式需要避免永远为true
       
    public class SwitchDemo08 {
       //计算1+2+。。。+100的和 5050
       public static void main(String[] args) {
           int i=0;
           int sum=0;
           while (i<=100){
               sum=sum+i;
               i++;
          }
           System.out.println(sum);
      }
    }
  • do...while循环

    • 对于while语句,表达不满足,则不能进入循环

    • do... while语句和while语句循环相似,不同的是do... while至少执行一次

    • do... while与while区别

      • while先判断后执行。do... while先执行后判断

      • do... while总是保证循环至少执行一次

      do{
          //语句
      }while(布尔表达式)
          
      public class DoWhileDemo09 {
          public static void main(String[] args) {
              int i=0;
              int sum=0;
      
              do {
                  sum=sum+i;
                  i++;
              }while (i<=100);
              System.out.println(sum);
          }
      }
      
      public class DoWhileDemo10 {
          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);//输出0
                  a++;
              }while (a<0);
          }
      }
      /*
      ================
      0
      */
      
  • for循环

    • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构

    • for循环执行的次数是执行前就确认的

    • 语法

      for(初始化;布尔表达式;更新){
          //代码语句
      }
      public class ForDemo11 {
          public static void main(String[] args) {
      
              for(int i=1;i<100;i++){
                  System.out.println(i);
              }
              System.out.println("for循环结束");
              /*
               *最先执行初始化步骤,可以声明一种类型,但可以初始化一个或者多个循环控制变量,也可以是空语句
               * 然后,检测布尔表达式值,true,循环体被执行,false,循环终止
               *
               *
               *
               *
               *
               **/
      
          }
      }
      //-----------------------------------------
      public class ForDemo12 {
          //练习1:计算0-100之间奇数和偶数的和
          public static void main(String[] args) {
              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);
          }
      
      
      }
      //-----------------------------------------
      public class ForDemo13 {
          public static void main(String[] args) {
              //练习2;用while循环或者for循环,输出1-1000之间可以被5整除的数,并且每行输出3个
              for(int i=0;i<=1000;i++){
                  if(i%5==0){
                      System.out.print(i+"\t");//print输出不会换行
                  }
                  if(i%(5*3)==0){
                      System.out.println();//println输出后会换行
      
                  }
              }
          }
      }
      //-----------------------------------------
      public class ForDemo14 {
          public static void main(String[] args) {
              /*
               *九九乘法表
               *  1x1=1
                  1x2=2    2x2=4
                  1x3=3    2x3=6    3x3=9
                  1x4=4    2x4=8    3x4=12    4x4=16
                  1x5=5    2x5=10    3x5=15    4x5=20    5x5=25
                  1x6=6    2x6=12    3x6=18    4x6=24    5x6=30    6x6=36
                  1x7=7    2x7=14    3x7=21    4x7=28    5x7=35    6x7=42    7x7=49
                  1x8=8    2x8=16    3x8=24    4x8=32    5x8=40    6x8=48    7x8=56    8x8=64
                  1x9=9    2x9=18    3x9=27    4x9=36    5x9=45    6x9=54    7x9=63    8x9=72    9x9=81
               **/
              for (int i = 1; i <= 9; i++) {
                  for (int j = 1; j <= i;j ++) {
                      System.out.print(i+"*"+i+"="+(i*j)+"\t");
                  }
                  System.out.println();
              } 
          }
      }

      分析问题思路:

      • 1、打印第一列

      • 2、把固定的1用循环包起来

      • 3、去掉重复项

      • 4、调整样式

  • 增强for循环(先了解,主要用来遍历数组、对象等)

    • 语法

      for(声明语句:表达式){
          //代码语句
      }
    • 声明语句:声明新的局部变量,该变量的类型必须与数组元素类型匹配。期作用域限定在循环内部语句块,其值与此时数组元素的值相等

    • 表达式:表达式是要访问的数组名,或者返回值为数组的方法

      public class ForDemo15 {
          public static void main(String[] args) {
              int[] numbers={10,20,30,40,50};//数组
      
              for(int x:numbers){
                  System.out.println(x);
              }
              System.out.println("=========================");
              for (int i = 0; i < 5; i++) {
                  System.out.println(numbers[i]);
              }
          }
      }
      //两者效果一致
  • break continue

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

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

      public class BreakDemo16 {
          public static void main(String[] args) {
              int i =0;
              while (i<100){
                  i++;
                  System.out.println(i);
                  if(i==5){
                      break;
                  }
              }
              System.out.println("测试break");
          }
      }
      /*
      1
      2
      3
      4
      5
      测试break
      */
      public class ContinueDemo17 {
          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");
              }
          }
      }
      /*
      1	2	3	4	5	6	7	8	9	
      11	12	13	14	15	16	17	18	19	
      21	22	23	24	25	26	27	28	29	
      31	32	33	34	35	36	37	38	39	
      41	42	43	44	45	46	47	48	49	
      51	52	53	54	55	56	57	58	59	
      61	62	63	64	65	66	67	68	69	
      71	72	73	74	75	76	77	78	79	
      81	82	83	84	85	86	87	88	89	
      91	92	93	94	95	96	97	98	99
      */
      
      public class TestDemo18 {
          public static void main(String[] args) {
              /* 打印三角形
              -----*
              ----**+
              ---***++
              --****+++
              -*****++++
              思路
              1、分析三角形图形组成,按照实际情况划分为三个直角三角形
              2、先输出第一个三角形
              3、一个个图拼出完整图形
               */
              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-11-04 15:45  蓬莱大侠  阅读(84)  评论(0)    收藏  举报