Fork me on GitHub

三大结构(顺序、选择、循环)


结构

顺序结构

public class ShunXuDemo {
   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单选择结构

语句

if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}
public class IfDemo01 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入内容:");

       scanner.hasNextLine();

       if(scanner.equals("hello")){
           System.out.println(scanner);
      }

       System.out.println("End");

       scanner.close();
  }
}

 

if双选择结构

语法:

if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}else{
   //如果布尔表达式为flase将执行的语句
}
public class IfDemo02 {
   public static void main(String[] args) {
       //考试分数大于60就是及格,小于60就是不及格
       Scanner scanner = new Scanner(System.in);

       System.out.println("请输入成绩:");
       float score = scanner.nextFloat();

       if (score>=60){
           System.out.println("及格!");
      }else {
           System.out.println("不及格!");
      }
       
       scanner.close();
  }
}

 

if多选择结构

语法

if(布尔表达式){
   //如果布尔表达式1的值为true将执行的语句
}else if{
   //如果布尔表达式2的值为true将执行的语句
}else if{
   //如果布尔表达式3的值为true将执行的语句
}else{
   //如果以上布尔表达式都不满足将执行的语句
}
public class IfDemo03 {
   public static void main(String[] args) {
       //考试分数大于60就是及格,小于60就是不及格
       Scanner scanner = new Scanner(System.in);

       System.out.println("请输入成绩:");
       float score = scanner.nextFloat();

       if (score==100){
           System.out.println("满分!");
      }else if(score>=90&&score<100){
           System.out.println("优秀!");
      }else if(score>=80&&score<90){
           System.out.println("良好!");
      }else if(score>=70&&score<80){
           System.out.println("一般!");
      }else if(score>=60&&score<70){
           System.out.println("及格!");
      }else if(score>=0&&score<60){
           System.out.println("不及格!");
      }else{
           System.out.println("成绩不合法!");
      }

       scanner.close();
  }
}

 

嵌套的if结构

语法

if(布尔表达式 1){
   //如果布尔表达式1的值为true将执行的语句
   if(布尔表达式 2){
       //如果布尔表达式2的值为true将执行的语句
  }
}

switch多选择结构

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

switch (expression){
   case value:
       //语句
       break;//可选
   case value:
       //语句
       break;//可选
   //可选择任意数量的case语句
   default:  //可选
       //语句
       
}

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

  • byte、short、int 或 char

  • 从Java SE7 开始

  • switch 支持字符串 String 类型了

  • 同时 case 标签必须为字符串常量或字面量

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;
           default:
               System.out.println("未知");
      }
  }
}

 

循环结构

while 循环

while 循环是最基本的循环,他的结构为:

while(布尔表达式){
   //循环内容
}
  • 只要布尔表达式为true,循环就是一直执行下去

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

  • 少部分情况需要循环一直执行,比如服务器的请求相应监听等。

  • 循环条件为true就会造成无线循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!

public class WhileDemo02 {
   public static void main(String[] args) {
       //计算1+2+3+...+100=?

       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(布尔表达式);
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);
  }
}

 

While 和 do...While 的区别

  • while 先判断后执行。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);
  }
}

 

for 循环

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

for 循环执行的次数时在执行前就确定的。语法格式如下:

for(初始化;布尔表达式;更新){
   //代码语句
}
public class ForDemo01 {
   public static void main(String[] args) {
       int a = 1;//初始化条件

       while (a<=100){//条件判断
           System.out.println(a);//循环体
           a = a + 2 ;//迭代
      }
       System.out.println("while循环结束!");

       //   初始化   判断条件 迭代
       for (int i = 1;i<=100;i++){
           System.out.println(i);
      }
       System.out.println("for循环结束!");
       
  }
}
public class ForDemo02 {
   public static void main(String[] args) {
       //练习1:计算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("奇数的和:"+oddSum);
       System.out.println("偶数的和:"+evenSum);
  }
}
public class ForDemo03 {
   public static void main(String[] args) {
       //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
       for (int i = 1; i < 1000; i++) {
           if (i%5==0){
               System.out.print(i+"\t");
          }
           if (i%(3*5)==0){
               System.out.println();
               //System.out.print("\n");
          }
      }
  }
}
public class ForDemo04 {
   public static void main(String[] args) {
       //打印九九乘法表
       for (int i = 1; i < 10; i++) {
           for (int i1 = 1; i1 <= i; i1++) {
               System.out.print(i1+"*"+i+"="+(i*i1)+"\t");
          }
           System.out.println();
      }
  }
}

 

增强 for 循环

Java5 引入了一种主要用于数组或集合的增强型 for 循环

Java 增强 for 循环 语法格式如下

for (声明语句 : 表达式){
   // 代码句子
}

//声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
//表达式:表达式是要访问的数组名,或者时返回值为数组的方法。
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);
      }
  }
}
 

 

 

posted @ 2022-06-03 11:43  944964684  阅读(322)  评论(0)    收藏  举报
1