Java基础知识点---流程控制语句

目录

流程控制

顺序结构

switch语句

if

for

while

do....while

break

案例1:打印1-100的质数

案例2:打印三角形


流程控制
  1. 顺序结构

    练习一

    package struct;
    ​
    public class ShunXunDemo {
        public static void main(String[] args) {
            System.out.println("hello1");
            System.out.println("hello2");
            System.out.println("hello3");
        }
    }
    ​
    /*
    运行结果
    hello1
    hello2
    hello3
    ​
     */
    1. switch语句
      package struct;
      ​
      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;
      ​
      ​
              }
          }
      }

      运行结果

       

      练习二

      package struct;
      ​
      public class SwitchDemo02 {
          public static void main(String[] args) {
              String name = "xiaozheng";
              //JDK7 的新特性,表达式可以是字符串
              //
      ​
              switch (name){
                  case "zxy":
                      System.out.println("zxy");
                      break;
                  case "xiaozheng":
                      System.out.println("xiaozheng");
                      break;
                  default:
                      System.out.println("找不到");
                      break;
              }
          }
      }

      运行结果

       

       

      1. if

        练习一

        package struct;
        ​
        import java.util.Scanner;
        ​
        public class IfDemo1 {
            public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                String s = scanner.nextLine();
        ​
                //equals:判断字符串是否相等
                if (s.equals("Hello")){
                    System.out.println(s);
        ​
                }
                System.out.println("End");
                scanner.close();
            }
        }

        运行结果

         

        练习二

        package struct;
        ​
        import java.util.Scanner;
        ​
        public class IfDemo3 {
            public static void main(String[] args) {
        ​
                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>=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("成绩不合格");
                }
        ​
            }
        }

        运行结果

         

        练习三

        package struct;
        ​
        import java.util.Scanner;
        ​
        public class lfDemo2 {
            public static void main(String[] args) {
                //
                Scanner scanner = new Scanner(System.in);
        ​
                System.out.println("请输入成绩:");
                int score = scanner.nextInt();
                //if双选择结构
                if (score>60){
                    System.out.println("及格");
                }else{
                    System.out.println("不及格");
                }
                scanner.close();
            }
        ​
        }

        运行结果

       

      1. for

        练习一

        package struct;
        ​
        public class ForDemo01 {
            public static void main(String[] args) {
                int a = 1; //初始化条件
        ​
                while (a <= 100) {//条件判断
                    System.out.println(a); //循环体
                    a += 2; //迭代
        ​
                }
                System.out.println("while循环结束");
        ​
                //初始化//条件判断 //迭代
                for (int i = 1; i <= 100; i++) {
                    System.out.println(i);
                }
                //快捷键100.for回车
        ​
        ​
                System.out.println("for循环结束!");
        ​
        ​
        ​
            }
        ​
        }

        练习二

        package struct;
        ​
        public class ForDemo02 {
            public static void main(String[] args) {
                //练习1:计算0到100之间的奇数和偶数的和
        ​
                int oddSum = 0;
                int evenSum = 0;
                for (int i = 0; i <= 100; i++) {
                    if (i%2!=0) {
                        oddSum+=i;
                    }else {  //奇数
                        evenSum += i;  //oddSum = oddSum + i
                    }
                    System.out.println("奇数的和:"+oddSum);
                    System.out.println("偶数的和:"+evenSum);
        ​
        ​
                    }
        ​
                }
            }

        练习三

        package struct;
        ​
        public class ForDemo03 {
            public static void main(String[] args) {
                //练习2
                //用while或for循环输出1-100之间能被5整除的数,
                //并且每行输出3个
                for (int i = 0; i <= 1000; i++) {
                    if (i%5==0){
                        System.out.print(i+"\t");
                    }
                    if (i%(5*3)==0){
                        //System.out.println();//换行
                        System.out.print("\n"); //换行
                    }
        ​
                }
                //println 输出会换行
                //print 输出不会换行
            }
        }

      练习四

      package struct;
      ​
      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(j+"*"+i+"="+(j*i)+"\t");
                  }
                  System.out.println();
              }
      ​
          }
      }
      /*
      1*1=1
      2*1=2  2*2=4
      3*1=3  3*2=6  3*3=9
      4*1=4  4*2=8  4*3=12 4*4=16
      5*1=5  5*2=10 5*3=15 5*4=20 5*5=25
      6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
      7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
      8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
      9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
      ​
       */

      练习五

      package struct;
      //增强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);
              }
          }
      }
      ​
      /*
      运行结果
      10
      20
      30
      40
      50
      =====================
      10
      20
      30
      40
      50
      ​
       */
  1. while

    练习一

    package struct;
    ​
    public class WhileDemo01 {
        public static void main(String[] args) {
            //输出1-100
    ​
            int i = 0;
            while (i<100){
                i++;
                System.out.println(i);
            }
        }
    }

练习二

package struct;
​
public class WhileDemo02 {
    public static void main(String[] args) {
        while (true){
            //等待客户端连接
            //死循环
        }
    }
}

练习三

package struct;
​
public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+。。100?
        int i = 0;
        int sum = 0;
​
        while (i<=100){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}
/*
运行结果
5050
 */
  1. do....while

    练习一

    package struct;
    ​
    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);
        }
    }
    /*
    运行结果
    5050
     */
  1. break
    package struct;
    ​
    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.print("123");
        }
    }
    1. continue

      package struct;
      ​
      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);
              }
      ​
          }
      }

案例1:打印1-100的质数
package struct;
​
public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数
        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+" ");
        }
        
    }
}
/*
运行结果
101 103 107 109 113 127 131 137 139 149 
 */
案例2:打印三角形
package struct;
​
public class TestDemo {
    //打印三角形 5行三角形
    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();
​
        }
    }
}
/*
​
     *
    ***
   *****
  *******
 *********
​
进程已结束,退出代码为 0
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java编程是一种流行的面向对象编程语言,具有广泛的应用领域,因此掌握Java编程的基础知识对于程序员来说至关重要。以下是Java编程基础知识点的汇总及习题集: 1. 数据类型和变量:Java中有基本数据类型和引用数据类型,如int、double、boolean和String等。掌握数据类型的定义和变量的声明与初始化是基础中的基础。 2. 运算符和表达式:Java中的运算符包括算术运算符、关系运算符、逻辑运算符等,了解它们的使用方法和优先级是编程的基本要求。 3. 控制流程:掌握条件语句(if-else语句、switch语句)和循环语句(for循环、while循环、do-while循环)的使用,能够正确地控制程序的执行流程。 4. 数组和集合:了解数组和集合的定义和基本操作,包括数组和集合的遍历、增删改查等操作,是进行数据处理必不可少的知识。 5. 方法和函数:掌握方法的定义和调用,了解方法的参数和返回值的使用,能够模块化地编写代码。 6. 面向对象编程:理解面向对象编程的基本概念,包括类和对象的定义、封装、继承和多态等特性。 7. 异常处理:了解异常的概念和处理方法,能够编写健壮的程序并处理异常情况。 通过对以上知识点的掌握和练习,可以帮助程序员建立起扎实的Java编程基础,为进一步深入学习和应用Java编程打下坚实的基础。通过多做习题,可以提高自己的编程能力,加深对知识点的理解。因此,建议对以上知识点进行反复练习,并不断实践和查漏补缺,才能真正掌握Java编程的基础知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值