JAVA--流程控制语句

1. 流程控制语句

功能设计里面:
 顺序结构: 每一行代码都执行完毕。  
 分支结构:
      1.条件判断
      2.条件选择    
 循环结构
     1. while
     2. do...while
     3. for    

 

2. 分支结构

==2.1 条件判断==

语法:
 if(条件表达式){// boolean
     //满足if的条件 true
     
}[else{
     //false
}]

 

public static void main(String[] args) {

       String name = "张三";
       int age = 25;
       double balance = 300000;
       boolean isHansomBoy = false;

       //介绍女朋友
       //要求: 帅 有钱
       //if...else 逻辑只有一行代码 {} 可以省略的
       if (isHansomBoy && balance > 100000) {
           System.out.println("女孩同意和" + name + "交往");
           System.out.println("hdhgdsgdsgd");
      } else {
           System.out.println("不同意");
           System.out.println("不同意");
      }
       
       System.out.println("其它代码。。。。。。");
  }

 

模拟用户登录:
 用户名
 密码
     
public static void main(String[] args) {
       
       //模拟用户登录功能
       String trueName = "admin";
       int truePass = 1234;

       String username = "administrator";
       int pass = 1234;


       if (trueName == username && truePass == pass) {
           System.out.println("用户登录成功");
           //修改用户信息
           System.out.println("用户信息修改成功。。。。。");
           System.out.println(username);
           System.out.println(pass);

      } else {
           System.out.println("用户名或者密码不符。。。");
      }


  }    

 

// 卫语句
public static void main(String[] args) {

       //模拟用户登录功能
       String trueName = "admin";
       int truePass = 1234;

       String username = "admin";
       int pass = 1234;


       //推荐卫语句进行设计功能 return
       //1. 获得失败结果

       if (username != trueName || truePass != pass) {
           System.out.println("用户名或者密码不符。。。");
           //结束程序了
           return; //在代码里面 遇见return 当前方法结束了(main结束了)
      }

       //登录成功之后的逻辑
       System.out.println("用户登录成功");
       //修改用户信息
       System.out.println("用户信息修改成功。。。。。");
       System.out.println(username);
       System.out.println(pass);
}

 

==1. 卫语句==

主要是读取用户在控制台录入的数据。

public static void main(String[] args) {

       //模拟用户登录功能
       String trueName = "admin";
       int truePass = 1234;

       //用户登录的时候提交过来的2个数据
       //不同的用户 用户名和密码肯定是不一样的 应该是用户手动录入的
       //目的: 获得用户在控制台录入的数据

       //1. 创建Scanner变量 数据类型 变量名 = 数据;
       // jdk提供的工具包 : 先找到 Scanner java.util.Scanner   导包
       Scanner input = new Scanner(System.in);

       System.out.println("请录入登录的用户名:");
       //读到的数据是String : next()/nextLine();
       String username = input.nextLine();

       System.out.println("请录入登录的密码:");
       int pass = input.nextInt();

       //推荐卫语句进行设计功能 return
       //1. 获得失败结果

//       System.out.println(username == trueName);//false 2个地址值不一样的
//       System.out.println(truePass != pass);//false

       if (!username.equals(trueName) || truePass != pass) {
           System.out.println("用户名或者密码不符。。。");
           //结束程序了
           return; //在代码里面 遇见return 当前方法结束了(main结束了)
      }

       //登录成功之后的逻辑
       System.out.println("用户登录成功");
       //修改用户信息
       System.out.println("用户信息修改成功。。。。。");
       System.out.println(username);
       System.out.println(pass);
}

 

==2. 字符串比较==

 public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       String str1 = new String("admin");//只要是new出来的 内存肯定在堆内存
       String str2 = "admin";
       System.out.println(str1);
       System.out.println(str2);


//       System.out.println(str1 == str2);//false
       //equals(字符串的变量) 比较的是字符串的数据
       //只要是字符串比较 肯定是使用equals()
       System.out.println(str1.equals(str2));


       String s1 = "hello";
       String s2 = "hello";

       String s3 = new String("hello");

       System.out.println(s1 == s2);//true
       System.out.println(s1 == s3);//false
  }

 

 

 

3. 多重if

if(){
   
}else if(){
   
}
....
else{
   
}    
public static void main(String[] args) {


//       对学员的结业考试成绩评测
//        成绩>=90 :优秀
//        成绩>=80 :良好
//        成绩>=60 :中等
//        成绩<60   :差


       Scanner input = new Scanner(System.in);
       System.out.println("请录入学生的成绩:");
       int score = input.nextInt();

       //ctrl+alt+L 排版

//       if (score >= 90) {
//           System.out.println(score + "优秀");
//       }
//
//       if (score >= 80 && score < 90) {
//           System.out.println(score + "良好");
//       }
//
//       if (score >= 60 && score < 80) {
//           System.out.println(score + "中等");
//       }
//
//       if (score < 60) {
//           System.out.println(score + "差");
//       }

       //有且只执行一个if
       //概率最大的条件放到最上面if
       if (score >= 90) {
           System.out.println(score + "优秀");
      } else if (score >= 80) {
           System.out.println(score + "良好");
      } else if (score >= 60) {
           System.out.println(score + "中等");
      } else {
           System.out.println(score + "差");
      }
  }

 

 

4. if嵌套

public static void main(String[] args) {
       //求最值

       Scanner input = new Scanner(System.in);
       System.out.println("请录入第一个数据:");
       int num1 = input.nextInt();
       System.out.println("请录入第2个数据:");
       int num2 = input.nextInt();
       System.out.println("请录入第3个数据:");
       int num3 = input.nextInt();


//       int max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
//       int min = (num1 < num2) ? ((num1 < num3) ? num1 : num3) : ((num2 < num3) ? num2 : num3);

       //三元运算符能实现的功能 if...else都能实现
       //if...else 能实现的 三元不一定   尤其是区间的判断使用if..else

       int max, min;
       if (num1 > num2) {
           //num1大
           if (num1 > num3) {
               max = num1;
          } else
               max = num3;
           //num2小
           if (num2 > num3) {
               min = num3;
          } else
               min = num2;

      } else {
           //num2大
           if (num2 > num3)
               max = num2;
           else
               max = num3;

           //num1小
           if (num1 > num3) {
               min = num3;
          } else
               min = num1;
      }

       System.out.println(max);
       System.out.println(min);
  }

 

 

 

2.2. 条件选择

条件选择能实现的功能 if...else都能实现 。 条件选择一般用于等值判断。if一般区间的判断

switch(变量){//结合某个变量的数据进行判断   byte short int char String(jdk1.7+) enum
   case 数据1:
       //功能
    [break;]  
     case 数据2:
       //功能
    [ break; ]
    .....  
  [ default:
       //逻辑
       break;]
}
case之间没有顺序的。

 

//用户录入月份 判断月份属于呢个季节
switch (month) {
   case 12:
   case 1:
   case 2:
       System.out.println("冬季");
       break;
   case 3:
   case 4:
   case 5:
       System.out.println("春季");
       break;
   case 6:
   case 7:
   case 8:
       System.out.println("夏季");
       break;
   case 9:
   case 10:
   case 11:
       System.out.println("秋季");
       break;//遇见break 结束switch语句 case穿透 直到遇见下一个break
   default:
       System.out.println("录入的数据不合法");
       break;
}

System.out.println("其它代码");
input.close();

 

 

 

3. Scanner

public static void main(String[] args) {

       //获得用户在控制台录入的数据

       //1.创建Scanner变量
       Scanner input = new Scanner(System.in);

       //获得字符串
       System.out.println("str:");
       String str = input.nextLine();
       System.out.println("str:" + str);
       // nextLine() 读取一整行的数据 而且自动换行

       //int
       System.out.println("int:");
       int num = input.nextInt();
       System.out.println("num:" + num);

       //string
       System.out.println("请录入时间:");
       input.nextLine();
       String str1 = input.nextLine();//空 换行
       System.out.println("str1:" + str1);

       //next()/nextInt()/nextDouble()/nextLong() 读取光标之后的数据 不会换行

       //next() vs nextLine()
       //next() 只读取空格之前的内容

//       //long
//       System.out.println("long:");
//       long aLong = input.nextLong();
//       System.out.println(aLong);
//
//       //double
//       System.out.println("double:");
//       double aDouble = input.nextDouble();
//       System.out.println(aDouble);
         input.close();
  }

 

 

4. 循环结构

1. while

while(循环条件){// boolean true/false
   //循环体
}

 

  public static void main(String[] args) {

       // 循环打印10次 我喜欢java
       int count = 1;

       while (count <= 10) {//避免死循环
           System.out.println("我喜欢java----" + count);//循环体
           count++;// count=count+1;
      }


       //不可达的代码
       System.out.println("hfgd");

 

 public static void main(String[] args) {


//       为了备战,李雷锲而不舍地练习,韩梅梅严格把关…
//       “韩梅梅,怎么样,可以了吗?”
//       “不行,高音部分唱得还不是很好,钢琴还要继续练啊 !”
//       没有听到“很棒”的评价,看来革命尚未成功, 李雷并不气馁:
//       早上5点练声,上午练钢琴,下午到声乐老师家练习唱歌,晚上练习舞蹈基本功
       Scanner input = new Scanner(System.in);
       System.out.println("韩梅梅,怎么样,可以了吗?y/n");
       String answer = input.nextLine();

       while ("n".equals(answer)) {
           System.out.println("早上5点练声,上午练钢琴,下午到声乐老师家练习唱歌,晚上练习舞蹈基本功");
           System.out.println("韩梅梅,又练习一天,怎么样,可以了吗?y/n");
           answer = input.nextLine();
      }

       System.out.println("练习的很棒。。。。");
  }

 

 public static void main(String[] args) {

       //动态录入学生的个数
       //动态录入学生的成绩
       //总分 平均分

       Scanner input = new Scanner(System.in);
       System.out.println("请录入学生的个数:");
       int studentNum = input.nextInt();//10

       int num = 1;
       double totalScore = 0;
       //循环录入学生的成绩 ctrl+alt+t
       while (num <= studentNum) {
           System.out.println("请录入第" + (num) + "个学生的成绩:");
           double studentScore = input.nextDouble();
           totalScore = totalScore + studentScore;
           // totalScore += studentScore;
           num++;
      }
       System.out.println("总成绩:" + totalScore);
       System.out.println("平局分:" + (totalScore / studentNum));
  }

 

2. do...while

do{
   //循环体
}while(循环条件);

不管是否满足循环条件  肯定会至少执行一次循环体内容

 

public static void main(String[] args) {

       // 2014年苹果手机售出10万,每年增长45%,请问按此增长速度,到哪一年苹果手机的出货量将达到100万

       int year = 2014;
       int sum = 100000;
       do {
           year++;
           // sum = sum + sum * 0.45;
           sum += sum * 0.45;
      } while (sum < 1000000);

       System.out.println("哪一年苹果手机的出货量将达到100万:" + year);
       System.out.println(sum);
  }

 

 

==3. for==

for([表达式1];循环条件;[表达式2]){
   //循环体
}
public static void main(String[] args) {
//       int count = 1;
//       for (; count <= 10; ) {
//           System.out.println("我喜欢java----" + count);
//           count++;
//       }
//
//       System.out.println(count);//11

//       for(初始化变量语句;循环条件;改变变量数据语句){
//           System.out.println("我喜欢java----" + count);
//       }

       System.out.println("-----------------------------");
       for (int i = 1; i <= 10; i++) {
           System.out.println("我喜欢java----" + i);
      }
       System.out.println("hdgsgsgd");

//       for (; true; ) {
//           System.out.println("hdgsgsgd");
//       }
  }

 

public static void main(String[] args) {
       //1-n 之间所有的偶数和以及偶数的个数

       Scanner input = new Scanner(System.in);
       System.out.println("请录入n的数据:");
       int n = input.nextInt();//1-100
       int sum = 0;
       int count = 0;

       for (int num = 1; num <= n; num++) {
           if (num % 2 == 0) {//偶数条件
               count++;
               sum += num;
          }
      }

       System.out.println("所有的偶数和:" + sum);
       System.out.println("偶数的个数:" + count);
       input.close();

  }

 

4. 循环嵌套

// 打印九九乘法表    业务开发--->
public class ForDemo2 {

   public static void main(String[] args) {

       for (int num1 = 1; num1 <= 9; num1++) {//控制行数
           for (int num2 = 1; num2 <= num1; num2++) {
               System.out.print(num2 + "*" + num1 + "=" + (num1 * num2) + "\t");
          }
           System.out.println();//换行的作用
      }
  }
}    

 

5. 流程控制关键字

1. break

switch:
  遇见break 就等同于就结束了switch语句。
循环:
  遇见break  就结束了当前的循环语句。

 

//模拟用户登录  3次机会
public class BreakDemo {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       String trueName = "admin";
       int truePass = 1234;
       
       int count = 3;
       for (; count >= 1; ) {
           count--;
           System.out.println("请录入登录的用户名:");
           String username = input.next();

           System.out.println("请录入登录的密码:");
           int pass = input.nextInt();

           if (!username.equals(trueName) || truePass != pass) {
               if (count == 0) {
                   System.out.println("3次机会使用完毕,以及返回主页面");
                   return;
              }
               System.out.println("用户名或者密码不符,还剩下" + count + "次机会");
          } else {
               break;
          }
      }
       //成功
       System.out.println("登录成功,欢迎你:" + trueName);
       //打印九九乘法表
       System.out.println("打印九九乘法表");
  }
}

 

 

2. continue

只能与循环语句一起使用。 遇见continue 跳过本次循环 继续执行其它循环。

//1-100 数字里面含4的数字
public static void main(String[] args) {


    //统计数字里面含4个数字个数
    //统计1-100数字之和(排除带4的数字)
    int sum = 0;
    int count = 0;
    int sum1 = 0;
    for (int i = 1; i <= 100; i++) {
        if (i % 10 == 4 || i / 10 == 4) {//14 24   40
            System.out.println(i);
            count++;
            sum1 += i;
            continue;
        }

        sum += i;
    }

    System.out.println("统计1-100数字之和:" + sum);
    System.out.println("含4个数字个数:" + count);
    System.out.println(sum1);

}
 

 

 

posted @ 2022-10-15 10:49  学JAVA的旅行者  阅读(147)  评论(0)    收藏  举报