Java流程控制

Scanner

我们可以通过Scanner类来获取用户的输入

基本语法:

Scanner s = new Scanner(System.in);

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

  • next():

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

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

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

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

  • nextLine():

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

  2. 可以获得空白

 

If选择结构

package com.huan.processControl;

import java.util.Scanner;

//if 单选择结构
public class IfDemo01 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);

       System.out.println("请输入内容: ");
       String s = scanner.nextLine();

       //equals 判断字符串是否相等
       if (s.equals("Hello")){
           System.out.println(s);
      }

       System.out.println("End");

       scanner.close();
  }
}
package com.huan.processControl;

import java.util.Scanner;

//if双选择结构
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("不及格");
      }

       scanner.close();
  }
}
package com.huan.processControl;

import java.util.Scanner;

//if多选择结构
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 == 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>=60){
          System.out.println("C");
      } else if (score<60 && score>=0){
          System.out.println("D");
      } else {
          System.out.println("成绩不合法");
      }

      scanner.close();
  }
}

 

Switch选择结构

package com.huan.processControl;

public class SwitchDemo01 {
   public static void main(String[] args) {
       //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("未知等级");
      }
  }
}
package com.huan.processControl;

public class SwitchDemo02 {
   public static void main(String[] args) {
       String name = "晓焕";
       //GDK7新特性,表达式结果可以是字符串!!!
       //字符的本质还是数字

       switch (name) {
           case "晓焕":
               System.out.println("晓焕");
               break;
           case "金风":
               System.out.println("金风");
               break;
           default:
               System.out.println("...");
      }
  }
}

 

While循环

  • While是最基本的循环,它的结构为:

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

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

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

  • 循环条件一直为true就会造成无限循环(死循环),我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死崩溃

package com.huan.processControl;

public class WhileDemo01 {

   public static void main(String[] args) {

       //输出1~100

       int i = 0;

       while (i < 100) {
           i++;
           System.out.println(i);
      }
  }
}
package com.huan.processControl;

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

       int i = 1;
       int sum = 0;

       while (i <= 100) {
           sum = sum + i;
           i++;
      }

       System.out.println(sum);

  }
}

 

Do while循环

  • 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要计时不满足条件,也至少执行一次

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

do {
   //代码结构
} while (buerbds)
  • while和do...while的区别:

    • while先判断后执行,do...while是先执行后循环

    • do...while总是保证循环体会被至少执行一次!这是他们的主要差别

package com.huan.processControl;

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

 

For循环

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

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

for (初始化 ; 布尔表达式 ; 更新 ) {
   //代码语句
}
package com.huan.processControl;

public class ForDemo01 {
   public static void main(String[] args) {
       //初始化值//条件判断//迭代
       for (int i = 1; i <=100; i++) {
           System.out.println(i);
      }
  }
}
package com.huan.processControl;

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);
  }
}
package com.huan.processControl;

public class ForDemo03 {
   //输出1~1000之间能被5整除的数,并且每行输出3个
   public static void main(String[] args) {
       for (int i = 0; i <= 1000; i++) {
           if (i % 5 == 0) {
               System.out.print(i + "\t");
          }
           if (i % (5 * 3) == 0) {
               System.out.println();
          }
      }
  }
}
/*
关于for循环有以下几点说明:
最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环停止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代英子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程。
*/
package com.huan.processControl;

public class ForDemo04 {
   //增强的For循环
   public static void main(String[] args) {
       int[] numbers = {10,20,30,40,50};    //定义了一个数组

       //遍历数组的元素
       for (int x:numbers) {
           System.out.println(x);
      }

       //也可以写为
       for (int i = 0;i < 5; i++) {
           System.out.println(numbers[i]);
      }
  }
}
package com.huan.processControl;

public class TextDemo01 {

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

  }
}

 

break、continue

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

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

posted @   huan小张  阅读(51)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示