【JAVA】我和我的第一个“对象”相遇

  • 表达式1必须是一个布尔表达式
  • 如果表达式1为真,那么执行表达式2,否则执行表达式3

自我检验

根据以下代码思考打印的结果是什么?

public class TestDemo2 {
    public static void main(String[] args) {
        boolean flg = true == true ? true : true == false ? false : false;
        System.out.println(flg);
        boolean flg2 = true == false ? true : true == false ? false : false;
        System.out.println(flg2);
    }
}

提示:全局观——broaden your perspective
在这里插入图片描述
正确答案:true,false

JAVA中的逻辑控制

其实程序和人生是一样:顺序中夹杂着循环伴随一次次选择不断成长


顺序语句

public class SequentialStatementsExample {
    public static void main(String[] args) {
        // 声明并初始化两个整型变量
        int num1 = 10;
        int num2 = 20;
        
        // 计算两个数的和
        int sum = num1 + num2;
        
        // 打印计算结果
        System.out.println("Sum: " + sum);
        
        // 修改num1的值
        num1 = 5;
        
        // 计算两个数的差
        int difference = num1 - num2;
        
        // 打印计算结果
        System.out.println("Difference: " + difference);
    }
}


选择语句

单分支

if (布尔表达式){
            //语句1
        }

双分支

        if (布尔表达式){
            //语句1
        }else{
        	//语句2
        	}

多分支

        if (布尔表达式1){
            //语句1
        }else if(布尔表达式2){
        	//语句2
        }else{
        	//语句3
        }


switch语句

    public static void main(String[] args) {
// switch语句
        int a = 10;
        switch (a){
            case 1:
                System.out.println(1);
                break;
            case 2:
                System.out.println(2);
            default:
                System.out.println("Aileen");
                break;
        }

在这里插入图片描述

面试题:不能作为Switch参数的数据类型是什么?
float double boolean long
switch和if语句最本质的区别就是:switch语句后面括号跟的必须是只能是以下类型的表达式:

  • 基本类型:byte、char、short、int不能是long类型
  • 引用类型:String常量串、枚举类型

而if后面可以是复杂的条件语句。


JAVA中读入一个整数的写法——Scanner

import java.util.Scanner;

public class TestDemo2 {


    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        //判断奇偶数
        int num = scan.nextInt();//输入一个整数
        if (num % 2 == 0){
            System.out.println(num +" 是偶数");
        }else{
            System.out.println(num +" 是奇数");
        }
    }

在这里插入图片描述

小试牛刀:判断一个年份是否是闰年

import java.util.Scanner;

public class TestDemo2 {


    public static void main(String[] args) {
        //四年一闰;百年不闰,四百年再闰
        Scanner scan = new Scanner(System.in);
        int year = scan.nextInt();
        if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0) {
            System.out.println(year + " 是闰年");
        } else {
            System.out.println(year + " 是平年");
        }

    }

在这里插入图片描述


循环语句

while循环

public class TestDemo2 {
    public static void main(String[] args) {
        while(循环条件){
            循环语句;
        }
    }

  1. 打印1-10的数字
public class TestDemo2 {
    public static void main(String[] args) {
        int i = 1;
        while(i<=10){
            System.out.println(i);
            i++;
        }
    }


  1. 打印1-10的和
public class TestDemo2 {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while(i<=10){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }


  1. 打印5的阶乘
public class TestDemo2 {

    public static void main(String[] args) {
        int i = 1;
        int j = 1;
        while(i<=5){
            j \*= i;
            i++;
        }
        System.out.println(j);
    }


  1. 打印1-5的阶乘之和
public class TestDemo2 {

    public static void main(String[] args) {
        int i = 1;
        int j = 1;
        int sum = 0;
        while(i<=5){
            j \*= i;
            sum += j;
            i++;
        }
        System.out.println(sum);
    }


  1. 判断素数
public class TestDemo2 {
    public static void main(String[] args) {
        //判断素数
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        int i = 2;
        while (i <= Math.sqrt(num)) {
            if (num % i == 0) {
                System.out.println(num + " 不是素数");
                return;//跳出循环
            }
            i++;
        }
        System.out.println(num + "是素数");
    }

  1. 打印1-100的所有素数
import java.util.Scanner;
public class TestDemo2 {
    public static void main(String[] args) {

        int i = 2;
        while (i < 100) {
            boolean isPrime = true;
// System.out.println(i);
            int k = 2;
            while (k <= Math.sqrt(i)) {
                if (i % k == 0) {
                    System.out.println(i + "不是素数");
                    isPrime = false;
                    break;
                }

                k++;
            }
        if (isPrime) {
            System.out.println(i + " 是素数");
        }
        i++;
        }

    }

}

在这里插入图片描述

  1. 编写程序数一下 1到 100 的所有整数中出现多少个含有数字9的数的个数
import java.util.Scanner;
public class TestDemo2 {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while(i <= 100) {

            if (i / 10 == 9 || i % 10 == 9) {
                sum++;
            }
            i++;
        }
        System.out.println(sum);
    }

  1. 编写程序数一下 1到 100 的所有整数中出现多少个数字9
import java.util.Scanner;
public class TestDemo2 {
    public static void main(String[] args) {
        int i = 1;
        int count = 0;
        while(i <= 100){
            //十位是9
            if(i / 10 == 9){
                count++;
            }
            //个位是9
            if(i % 10 == 9){
                count++;
            }
            i++;
        }
        System.out.println(count);
    }


for循环

public class TestDemo3 {
    public static void main(String[] args) {
        for(表达式①;表达式②;表达式③){
            表达式④;
        }
    }

#mermaid-svg-SXOnXgDSJGvy8brq {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SXOnXgDSJGvy8brq .error-icon{fill:#552222;}#mermaid-svg-SXOnXgDSJGvy8brq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-SXOnXgDSJGvy8brq .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-SXOnXgDSJGvy8brq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-SXOnXgDSJGvy8brq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-SXOnXgDSJGvy8brq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-SXOnXgDSJGvy8brq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-SXOnXgDSJGvy8brq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-SXOnXgDSJGvy8brq .marker.cross{stroke:#333333;}#mermaid-svg-SXOnXgDSJGvy8brq svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-SXOnXgDSJGvy8brq .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-SXOnXgDSJGvy8brq .cluster-label text{fill:#333;}#mermaid-svg-SXOnXgDSJGvy8brq .cluster-label span{color:#333;}#mermaid-svg-SXOnXgDSJGvy8brq .label text,#mermaid-svg-SXOnXgDSJGvy8brq span{fill:#333;color:#333;}#mermaid-svg-SXOnXgDSJGvy8brq .node rect,#mermaid-svg-SXOnXgDSJGvy8brq .node circle,#mermaid-svg-SXOnXgDSJGvy8brq .node ellipse,#mermaid-svg-SXOnXgDSJGvy8brq .node polygon,#mermaid-svg-SXOnXgDSJGvy8brq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-SXOnXgDSJGvy8brq .node .label{text-align:center;}#mermaid-svg-SXOnXgDSJGvy8brq .node.clickable{cursor:pointer;}#mermaid-svg-SXOnXgDSJGvy8brq .arrowheadPath{fill:#333333;}#mermaid-svg-SXOnXgDSJGvy8brq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-SXOnXgDSJGvy8brq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-SXOnXgDSJGvy8brq .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-SXOnXgDSJGvy8brq .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-SXOnXgDSJGvy8brq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-SXOnXgDSJGvy8brq .cluster text{fill:#333;}#mermaid-svg-SXOnXgDSJGvy8brq .cluster span{color:#333;}#mermaid-svg-SXOnXgDSJGvy8brq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-SXOnXgDSJGvy8brq :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;}

表达式①:初始化条件
表达式②:循环判断条件
表达式④:循环需要执行的业务逻辑

for循环执行顺序

#mermaid-svg-Lgi3SsjB3QTBSpf4 {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .error-icon{fill:#552222;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .marker.cross{stroke:#333333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .cluster-label text{fill:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .cluster-label span{color:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .label text,#mermaid-svg-Lgi3SsjB3QTBSpf4 span{fill:#333;color:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .node rect,#mermaid-svg-Lgi3SsjB3QTBSpf4 .node circle,#mermaid-svg-Lgi3SsjB3QTBSpf4 .node ellipse,#mermaid-svg-Lgi3SsjB3QTBSpf4 .node polygon,#mermaid-svg-Lgi3SsjB3QTBSpf4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .node .label{text-align:center;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .node.clickable{cursor:pointer;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .arrowheadPath{fill:#333333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .cluster text{fill:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 .cluster span{color:#333;}#mermaid-svg-Lgi3SsjB3QTBSpf4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Lgi3SsjB3QTBSpf4 :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;}

One

表达式3

表达式1

布尔表达式2

表达式4

你可能感兴趣的:(java,开发语言)