• 顺序结构 基本语句


    顺序结构

    1.java的基本结构,顺序结构按照顺序一句一句的执行

    2.顺序结构是最简单的算法结构

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

    选择结构

    if单选择结构语句

    if(布尔表达式){

    //如果值为true

    }

    equals

    Scanner scanner = new Scanner(System.in);

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

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

    if...else双选择结构语句

    if(){

    //true

    }else{

    //flase

    }

    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("Hello word");

          }else{
           System.out.println("黑凤梨i");
          }
           scanner.close();
      }
    }

     

    if 多选择结构

            if(s.equals("Hello")){
               System.out.println("Hello word");
          }else if (){
               //
          }else if (){
               //
          }else if (){
               //
          }else if (){
               //
          } else{
           System.out.println("黑凤梨i");
          }
           scanner.close();
      }
    }

    swith case :多选择结构

    swith 匹配一个具体的值

    case : (冒号)穿透

        public static void main(String[] args) {
       //case穿透 //switch 匹配一个具体的值
       char gread ='C';
       switch (gread){
           case 'A':
               System.out.println("shsf");
               break;
           case 'B':
               System.out.println("awff");
               break;
           case 'C':
               System.out.println("shssvf");
               break;
               //不在条件内的 其他
           default:
               System.out.println("未知")
      }
    }
    }
    //反编译 找到Project Structure >> Project >> 找到位置
    //把 .class文件 Copy到IDEA 文件内即可运行

    while循环

    while(布尔表达式){

    }

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

    2. 我们大多情况下是会让循环停下来的,我们需要一个让表达式实效的方法来借宿循环。

    3. 正常业务编程应尽量避免死循环。

      public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
         double sum =0;
         int m = 0;
         while(scanner.hasNextDouble()==true){ // ==true可省略
             double v = scanner.nextDouble();
             m++;
             sum = sum+ v;
        }
         System.out.println("总数和为:"+ sum);
         System.out.println("总ge数为:"+ m);
         scanner.close();
      }

      do...while

       

      public class ifDemo01 {
         public static void main(String[] args) {
             int a = 0;
             //先判断 后执行
             while(a<10){
                 System.out.println(a);
                 a++;
            }
             System.out.println("_________________________");
             int i = 0;
             int sum = 0;
             //先执行,后判断
             do{
                 sum = sum + i;
                 i++;
            }while (i <=100);
             System.out.println(sum);
        }
      }

       

       

     

    重要的for循环

    for循环可以是一些循环结构变得简单

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

    快捷for循环: 100.for(IDEA)

    计算20以内的奇数偶数和

        public static void main(String[] args) {
           int oddSum = 0;//奇数
           int evenSum = 0;//偶数
           // 快捷键 20.for
           for (int i = 0; i < 20; i++) {
               if(i%2!=0){
                   oddSum+=i;
              }else{
                   evenSum+=i;
              }
          }
           System.out.println("奇数和为:"+oddSum);
           System.out.println("偶数和为:"+evenSum);

      }
    }

    练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

     

    public class forDemo04 {
       public static void main(String[] args) {
           //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
           for (int i = 0; i < 1000; i++) {
               if(i%5==0){
                   System.out.print(i+" ");
              }
               //整除的倍数几倍就几个输出
               if(i%15==0){
                   System.out.print(" ");
              }
          }


      }
    }

    3.九九乘法表

     

    public class forDemo05 {
       public static void main(String[] args) {
           /**分析:
            * 1.先打印出乎第一列,
            * 2.把固定的1 用另一个for循环包起来
            * 3.去掉重复的 i<=j ,用" "每输出完一个空
            * 4.调整样式 用System.out.println();每输出完一列换行
            *
            */
           for (int j = 1; j <=9; j++) {
               for (int i = 1; i <= j; i++) {
                   System.out.print(j+"*"+i+"="+(j*i)+" ");
              }
              System.out.println();
          }

      }
    }

     

    增强for循环(遍历数组和集合)

     

    int[] number = {1,2,3,4,5,}; //定义了一个数组

    //遍历数组

    //把number这个数组里的元素赋值给int x

    for(int x:number){

    System.out.println(x);

    }

    Text 打印三角形

     

    public class Test {
        public static void main(String[] args) {
            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();
            }
        }
    }

     

     

     

     

     

     

     

  • 相关阅读:
    JAVA内存模型(Java Memory Model ,JMM)
    多线程(一)多线程实现、线程状态、线程调度、线程同步、线程数据传递
    Java中如何实现代理机制(JDK动态代理和cglib动态代理)
    HashMap与HashTable
    SSH远程会话管理工具
    nginx 无法启动:bind() to 0.0.0.0:443 failed
    nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    php Allowed memory size of 134217728 bytes exhausted
    nginx 部署php项目 404
    RabbitMQ+PHP教程
  • 原文地址:https://www.cnblogs.com/li369/p/14030214.html
Copyright © 2020-2023  润新知