java流程控制(依然还是没有图片,不过不大事)

java流程控制

用户交互scanner

b177ccd7161d3d34e90c5d46cd29afe

使用next方式接收:

package com.maru.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();     } }  

使用nextline方式接收:if可以去掉

package com.maru.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();     } }  

选择结构

  • if单选择结构

    6aa5658322754bbcf1dee001708e496

  • if双选择结构

    c79d3c67e83d5c3178355d11c6b91a9

  • if多选择结构

    757f89768a0d01f31c30fd22097c579

  • 嵌套的if结构

    f3f5461bc854b49edf4a577cde579d6

  • switch多选择结构

    fe1b0dec97016428f75ffe78905b7fc

循环结构

  • while循环

    81fa1b4c7842de7adb33a44a2427a4d

    package com.maru.struct;  public class whileDemo01 {     public static void main(String[] args) {          //输出1~100          int i = 0;          while (i < 100) {             i++;             System.out.println(i);         }       } }  
    package com.maru.struct;  public class whileDemo03 {     public static void main(String[] args) {         //计算1到100的和          int i = 0;         int sum = 0;          while (i<=100){             sum = sum + i;             i++;          }         System.out.println(sum);     }    }    
  • do...while循环

232e37c1391616b2f787bc94af49ade

区别:

package com.maru.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循环

e1464d7566e9ea089ab6d8b951a3ed7

package com.maru.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循环结束);         System.out.println(==================================);         //初始化//条件判断//迭代         for (int i = 1;i <= 100;i++){             System.out.println(i);         }          System.out.println(for循环结束);     } }  

练习1:

package com.maru.struct;  public class ForDemo02 {     public static void main(String[] args) {         //计算1到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(100以内奇数的和+oddSum);         System.out.println(100以内偶数的和+evenSum);     } }  

练习2:

package com.maru.struct;  public class ForDemo03 {     public static void main(String[] args) {         //1000以内能被5整除的数,并且每行输出3个         System.out.println(1000以内能被5整除的数);         int j = 0;         for (int i = 0; i <= 1000; i++) {             if (i%5==0){                 System.out.print(i+\t);                 j++;                 if (j==3){                     System.out.print(\n);                     //System.out.println();                     j = 0;                 }             }          }     } }  

练习3:

package com.maru.struct;  public class ForDemo04 {     public static void main(String[] args) {         for (int i = 1; i <= 9; i++) {             for (int j = 1; j <= i; j++) {                 System.out.print(j+x+i+=+(i*j)+\t);             }             System.out.println();         }     } } 
  • 增强for循环

c2b3d7bcad311a9aa4cc3abdb892c80

package com.maru.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

3af5308c42f5342f6cce37f30bc4a4b

break

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

continue

package com.maru.struct;  public class ContinueDemo {     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);         }     } }  
  • 练习:打印一个三角形