顺序结构 选择结构 循环结构

查找使用IDEA生成JavaDoc 文档

  1. 打开IDEA,点击tools,点击Generate JavaDoc,选择要生成的类

  2. 修改存放路径

  3. 在local中输入zh_CN zh_CN文件夹下是用于显示简体中文时,该软件所需调用的文件

  4. 在Command line and arguments输入 -encoding UTF-8 -charset UTF-8

  5. 进入我们的存放路径,找到index.html即可显示生成的JavaDoc文件

Java流程控制

Scanner对象->用户交互

  1. 可以通过Scanner类来获取用户的输入

  2. Scanner s = new Scanner(System.in);
    
  3. 通过Scanner类中的next()与nextLine()方法获取输入的字符串,在读取前一般要使用hasNext()与hasnextLine()判断是否还有输入的数据

  4. next():

    • 一定要读取到有效字符后才可以结束输入
    • 对输入有效字符前遇到的空白,next()方法会自动将其去掉
    • 只输入有效字符后才能将其后面输入的空白作为分隔符或者结束符
    • next()不能得到带有空格的字符串
  5. nextLine():

    • 以Enter作为结束符,nextLine()返回的是输入回车之前的所有字符

    • 可以获得空白

  6. import java.util.Scanner;
    
    //用户交互--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();
        }
    
  7. import java.util.Scanner;
    
    //用户交互--scanner
    public class Demo02 {
        public static void main(String[] args) {
            //从键盘接收数据
            Scanner scanner = new Scanner(System.in);
            System.out.println("用nextLine方法接收:");
            //判断是否还有输入
            if(scanner.hasNextLine()){
                String str = scanner.nextLine();
                System.out.println("输出为:"+str);
            }
            scanner.close();
        }
    
  8. import java.util.Scanner;
    
    //用户交互--scanner
    public class Demo03 {
        public static void main(String[] args) {
            //从键盘接收数据
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入数据:");
            //判断是否还有输入
            String str = scanner.nextLine();
            System.out.println("输出为:"+str);
            scanner.close();
        }
    
  9. import java.util.Scanner;
    
    //用户交互--scanner
    public class Demo04 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            //从键盘接收数据
            int i = 0;
            float f = 0.0f;
            System.out.println("请输入整数:");
            //如果...那么
            if (scanner.hasNextInt()){
                i = scanner.nextInt();
                System.out.println("整数数据:"+i);
            }else{
                System.out.println("输入的不是整数数据!");
            }
            System.out.println("请输入小数:");
            //如果...那么
            if (scanner.hasNextFloat()){
                f = scanner.nextFloat();
                System.out.println("小数数据:"+f);
            }else{
                System.out.println("输入的不是小数数据!");
            }
            scanner.close();
        }
    
  10.   import java.util.Scanner;
    
      //用户交互--scanner
      public class Demo05 {
      public static void main(String[] args) {
           //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过             输入非数字来结束输入并输出执行结果
           Scanner scanner = new Scanner(System.in);
           //和
           double sum = 0;
           //计算输入了几个数字
           int m = 0;
           System.out.println("请输入要计算的数:");
           //通过循环判断是否还有输入,并且在里面对每一次进行求和和统计
           while(scanner.hasNextDouble()){
               double x = scanner.nextDouble();
               m = m+1;
               sum = sum + x;
             System.out.println("你输入了第"+m+"个数据,然后当前结果                      sum="+sum);
           }
           System.out.println(m+"个数的和为:"+sum);
           System.out.println(m+"个数的平均数为:"+(sum/m));
           scanner.close();
       }
    

顺序结构

  1. Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行

  2. 是最简单的算法结构

  3. 是任何算法都离不开的一种基本算法结构

  4. //顺序结构
    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");
        }
    

选择结构

  1. if单选择结构

    • if(布尔表达式){
          //布尔表达式为true将执行的语句
      }
      
      
  2. //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();
        }
    
  3. if双选择结构

    • if(布尔表达式){
          //如果布尔表达式的值为true
      }
      else{
         //如果布尔表达式的中为false 
      }
      
  4. import java.util.Scanner;
    
    //if双选择结构
    public class IfDemo02 {
        public static void main(String[] args) {
            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();
        }
    
  5. if多选择结构

    • if(布尔表达式1){
          //如果布尔表达式1的值为true执行代码
      }else if(布尔表达式2){
          //如果布尔表达式2的值为true执行代码
      }else if(布尔表达式3){
          //如果布尔表达式3的值为true执行代码
      }else{
      	//如果以上布尔表达式都不为true执行代码    
      }
      
  6. //if多选择结构
    public class IfDemo03 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            /*
            if语句至多有一个else语句,else语句在所有的else if语句之后
            if语句可以有若干个else if语句,他们必须在else语句之前
            一旦其中一个else语句检测为true,其他的else if以及else语句都将跳过执行
            */
            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>=70){
                System.out.println("C");
            }else if(score<70&&score>=60){
                System.out.println("D");
            }else if(score<60&&score>=00){
                System.out.println("不及格");
            }else{
                System.out.println("成绩不合法");
            }
            scanner.close();
        }
    
  7. 嵌套的if结构

    • if(布尔表达式1){
          //如果布尔表达式1的值为true执行代码
          if(布尔表达式2){
          //如果布尔表达式2的值为true执行代码    
          }
      }
      
  8. switch多选择结构

    • 判断一个变量与一系列值中的某个值是否相等,每个值称为一个分支

    • switch语句中的变量类型可以是:(1)byte、short、int或者char(2)从Java SE 7开始(3)switch支持字符串String类型(4)case标签必须为字符串常量或字面量

    • switch(expression){
              case value:
              //语句
              break;//可选
              case value:
              //语句
              break;//可选
              //可以有任意数量的case语句
              default://可选
                  //语句
      }
      
    • //switch多选择结构
      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;
                  case 'E':
                      System.out.println("挂科");
                      break;
                  default:
                      System.out.println("未知等级");
              }
          }
      
    • import java.util.Scanner;
      
      //Switch多选择结构
      public class SwitchDemo02 {
          public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              System.out.println("请输入季节:");
              String str = scanner.nextLine();
              //JDK7的新特性,表达式结果可以是字符串
              //字符的本质还是数字
      
              //反编译  java--class(字节码)--反编译(IDEA)
              switch (str){
                  case "春季":
                      System.out.println("春天来了");
                      break;
                  case "夏季":
                      System.out.println("夏来了");
                      break;
                  case "秋季":
                      System.out.println("秋天来了");
                      break;
                  case "冬季":
                      System.out.println("冬天来了");
                      break;
                  default:
                      System.out.println("输入错误!");
              }
          }
      

循环结构

  1. while循环

    • 最基本的循环

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

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

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

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

    • 如果不满足条件,则不能进入循环

    • //循环--while
      public class WhileDemo01 {
          public static void main(String[] args) {
              //输出1~100
              int i= 0;
              while(i<100){
                  i++;
                  System.out.println(i);
              }
          }
      
    • //死循环,千万别尝试
      public class WhileDemo02 {
          public static void main(String[] args) {
              //死循环
              while(true){
                  //等待客户连接
                  //定时检查
                  //....
              }
          }
      
    • //While循环
      public class WhileDemo03 {
          public static void main(String[] args) {
              //计算1+2+3+...+100
              int i = 0;
              int sum = 0;
              while(i<=100){
                  sum+=i;
                  i++;
              }
              System.out.println(sum);
          }
      
  2. do......while循环

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

    • do{
          //代码语句
      }while(布尔表达式);
      
    • While和do-While的区别:(1)while先判断后执行,do-while先执行后判断(2)do-while总是保证循环体至少被执行一次

    • //do...while循环
      public class DoWhileDemo01 {
          public static void main(String[] args) {
              //计算1+2+3+...+100
              int i = 0;
              int sum = 0;
              do{
                  sum+=i;
                  i++;
              }while(i<=100);
              System.out.println(sum);
          }
      
    • //do...while   分辨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);
              System.out.println(a);
          }
      
  3. for循环

    • 执行的次数在执行前就确定

    • for(初始化;布尔表达式;更新){
          //代码语句
      }
      
    • 支持迭代的通用结构,最有效,最灵活的循环结构

    • //练习1:计算0~100之间的奇数和偶数的和
      public class ForDemo02 {
          public static void main(String[] args) {
              int i = 1;
              int oddsum = 0;//奇数和
              int evensum = 0;//偶数和
              //奇数和
              for(i=0;i<=100;i++){
                  if(i%2!=0){
                      oddsum+=i; //奇数和
                  }else{
                      evensum+=i;//偶数和
                  }
              }
              System.out.println("奇数的和为:"+oddsum);
              System.out.println("偶数的和为:"+evensum);
          }
      
      
    • //练习2:用while或for循环输出1~1000之间能被5整除的数,并且每行输出三个
      //print  输出完不会换行
      //println  输出完会换行
      public class ForDemo03 {
          public static void main(String[] args) {
              //while循环
              int i = 1;
              int s = 0;  //被5整除的数的数目
              while(i<=1000){
                  if(i%5==0){
                      System.out.print(i+" ");
                      s++;
                      if(s%3==0){
                          System.out.println();
                      }
                  }
                  i++;
              }
              System.out.println("========================================");
              //for循环
              int i1 = 1;
              int s1= 0;  //被5整除的数的数目
              for(i1=1;i1<=1000;i1++){
                  if(i1%5==0){
                      System.out.print(i1+" ");
                      s1++;
                      if(s1%3==0){
                          System.out.println();
                      }
                  }
      
              }
          }
      
    • //练习3:打印九九乘法表
      /*
      1x1=1
      1x2=2 2x2=4
      1x3=3 2x3=6 3x3=9
      1x4=4 2x4=8 3x4=12 4x4=16
      1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
      1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
      1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
      1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
      1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
      */
      public class ForDemo04 {
          public static void main(String[] args) {
              //1.我们先打印第一列
              //2.我们把固定的1再用一个循环包起来
              //3.去掉重复,i<= j
              //4.调整样式
              for (int j =1; j <=9; j++) {
                  for(int i=1;i<=j;i++){
                      System.out.print(i+"x"+j+"="+(i*j)+" ");
                  }
                  System.out.println();
              }
          }
      
  4. 增强for循环

    • Java5引入的一种数组或集合的增强型for循环

    • 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);
              }
          }
      
  5. break和continue

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

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

    • //break
      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
      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);
              }
              //break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
              //continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
          }
      
  6. 关于goto关键字

    • 是Java的一个保留字,但是并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键词身上能看出一些goto的影子-------带标签的break和continue

    • 标签是指后面跟一个冒号的标识符,例如:label:

    • Java中唯一用到标签的地方是在循环语句之前,在循环之前设置标签的唯一理由是:希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但使用标签会中断到存在标签的地方

    • //goto关键字
      public class LabelDemo {
          public static void main(String[] args) {
              //打印101~150之间所有的质数
              //指数:大于1的自然数中,除了1和他本身以外不再有其他因数的自然数
              int count = 0;
              //不建议使用
              outer:for(int i=101;i<150;i++){
                  for(int j=2;j<i/2;j++){
                      if(i%j==0){
                          continue outer;
                      }
                  }
                  System.out.print(i+" ");
              }
          }
      

posted on 2024-03-26 17:55  ⪩||⪨書生  阅读(18)  评论(0)    收藏  举报

导航