java流程控制

Scanner对象

  • Scanner类用来获取用户的输入

  • 基本语法:

Scanner scanner = new Scanner(System.in)
  • next

public class Demo06 {

   public static void main(String[] args) {
       //创建一个扫描器对象,用于接收键盘数据
       Scanner scanner = new Scanner(System.in);
//next方发对输入有效字符之前遇到空白,都会被其自动去掉,不能得到带有空格的字符串
       System.out.println("使用next方法接收");

       //判断用户有没有输入字符串
       if (scanner.hasNext()){
           String str =  scanner.next(); //程序会等待用户输入完毕
           System.out.println("输出的内容为:"+str);
      }
       //凡是属于IO流的类如果不关闭会一直占用资源,要养成良好习惯用完就关闭
       scanner.close();
  }
  • nextLine

public class Demo07 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
//nextLine方法返回的是输入回车之前的所有字符,可以获得空白
       System.out.println("使用nextLine方式接收");

       if (scanner.hasNextLine()){
            String str = scanner.nextLine();
           System.out.println("输出的内容为:"+str);
      }
       scanner.close();
  }

 

顺序结构

  • 他是任何一个算法都离不开的一种基本算法结构

 

选择结构

if单选择结构
  • 语法

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

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

}
public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       //考试分数大于六十分的及格,小于六十分的不及格
       System.out.println("请输入你的成绩");
       double score  = scanner.nextDouble();

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

       scanner.close();
  }
多选择结构
  • 语法

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

}else if{
   //如果布尔表达式3 的值为true将执行的语句
}else{
   //如果以上布尔表达式都不为true执行语句
}
public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入你的成绩");
       double score = scanner.nextDouble();

       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(布尔表达式 1){
   ////如果布尔表达式 1的值为true执行代码
   if(布尔表达式 2){
       ////如果布尔表达式 2的值为true执行代码
  }
}
switch多选择结构
switch(expression){
   case value:
  //语句
  break; //可选
   case value:
  //语句
  break; //可选
       //你可以有任意数量的case语句
   default: //可选
       //语句
}
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循环

只要布尔表达式为true,循环就会一直执行下去

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

  • 语句

whiel(布尔表达式){
   //循环内容
}
public static void main(String[] args) {
       //输出1~100

       int i = 0;
int sum = 0;
 
       while (i<100){
           i++;
           System.out.println(sum);
      }
  }
do...while循环

while先判断后执行,do...while先执行后判断

do...while至少会执行一次

  • 语法

do{
   //代码与语句
}while(布尔表达式);
public static void main(String[] args) {
       //输出1~100

       int i = 0;
       int sum = 0;

       do{
           sum = sum+i;
           i++;
      }while (i<=100);
           System.out.println(sum);
      }
for循环

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

for循环执行的次数是在执行前就确定的

for(初始化;布尔表达式;更新){
   //代码语句
}
public static void main(String[] args) {
       int i =0;  //初始化值

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

       //初始化 //条件判断 //迭代
       for (int a=0;a<=100;a++){
           System.out.println(a);
      }
       System.out.println("for循环结束");

       /*
       关于for循环有一下几点

       最先执行初始化步骤。可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以是空语句。
       然后,检测布尔表达式的值,如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
       再次检测布尔表达式,循环执行上面的过程。
        */
  }
增强型for循环
  • 语法

for(声明语句:表达式){
   //代码语句
}
public static void main(String[] args) {
       int[] numbers = {10,20,30,40,50}; //定义一个数组

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

  }
break continue

break用于强行退出循环

continue用于终止某次循环过程

 
posted @ 2022-05-31 23:44  萧十一  阅读(24)  评论(0)    收藏  举报