Java basic study notes-2

Preface

In the field of computer programming, conditional statements and control flow structures are the basic building blocks for building program logic. They allow programmers to perform different operations based on different conditions, making programs more flexible and intelligent. This article will delve into conditional statements and control flow in the Java programming language, and provide a series of practical examples and techniques to help readers better understand and apply these concepts.
Java basic study notes-1

1. if statement

ifStatement is the most basic conditional statement which allows you to execute different blocks of code based on given conditions.

import java.util.Scanner;

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入成绩:");
        int score = scanner.nextInt();
        if (score >= 90) {
    
    
            System.out.println("优秀");
        } else {
    
    
            System.out.println("其它");
        }
    }
}

In the above example, depending on the input grade, the program will output "Excellent" or "Other".

2. if-else statement

if-elseStatements allow you to execute an alternative block of code if a condition is not met.

import java.util.Scanner;

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入成绩:");
        int score = scanner.nextInt();
        if (score >= 90) {
    
    
            System.out.println("奖励");
        }
        System.out.println("程序结束");
    }
}

In the above example, if the score is greater than or equal to 90 points, "reward" will be output, otherwise the program will continue to execute subsequent code.

3. Multiple conditional statements

Java allows you to use multiple conditional statements to handle multiple conditions.

public class Demo03 {
    
    
    public static void main(String[] args) {
    
    
        int score = 88;
        if (score >= 90) {
    
    
            System.out.println("优秀");
        } else if (score >= 80) {
    
    
            System.out.println("良好");
        } else if (score >= 70) {
    
    
            System.out.println("中等");
        } else if (score >= 60) {
    
    
            System.out.println("及格");
        } else {
    
    
            System.out.println("差");
        }
    }
}

In the example above, the program will output different ratings based on different ranges of grades.

4. Simplified multiple conditional statements

You can also use simplified multiple conditional statements to reduce code complexity.

public class Demo04 {
    
    
    public static void main(String[] args) {
    
    
        int score = 98;
        if (score >= 90) System.out.println("优秀");
        else if (score >= 80) System.out.println("良好");
        else if (score >= 70) System.out.println("中等");
        else if (score >= 60) System.out.println("及格");
        else System.out.println("差");
    }
}

In the above example, we used a more concise syntax to achieve the same effect.

5. Logical operators

Logical operators allow you to combine multiple conditions within a condition.

public class Demo05 {
    
    
    public static void main(String[] args) {
    
    
        int java = 89, html = 90;
        if (java >= 90 || html >= 90) System.out.println("去动物园游玩");
        else System.out.println("在家休息");
    }
}

In the above example, we use the logical OR operator ||to determine whether a course has achieved a score of 90 or above.

6. switch statement

switchStatements allow you to execute different blocks of code based on different values.

import java.util.Scanner;

public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("> 幸运抽奖\n");
        System.out.print("请输入4位会员号:");
        int id = scanner.nextInt();
        int baiwei = id / 100 % 10;
        int luck = (int)(Math.random() * 10);
        System.out.println("幸运数字是:" + luck);
        if (baiwei == luck) System.out.println("奖励mp3一个。");
        else System.out.println("谢谢惠顾。");
    }
}

In the above example, we have used switchstatements to execute different codes based on different conditions.

7. String comparison

In Java, to compare the contents of strings you should use equals()methods.

import java.util.Scanner;

public class Demo07 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入是否是会员(y/n):");
        String yesno = scanner.next();
        if (yesno.equals("y")) {
    
    
            System.out.println("是会员");
        } else {
    
    
            System.out.println("不是会员");
        }
    }
}

In the above example, we use equals()the method to compare whether the string entered by the user is equal to "y".

8. Conditional nesting

Conditional statements can be nested to handle more complex logic.

import java.util.Scanner;

public class Demo08 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入是否是会员(y/n):");
        String yesno = scanner.next();
        System.out.println("请输入购物金额:");
        int money = scanner.nextInt();
        double zk = 1.0; // 默认值,没有折扣
        if (yesno.equals("y")) {
    
    
            if (money >= 200) zk = 0.75;
            else zk = 0.8;
        } else {
    
    
            if (money >= 100) zk = 0.9;
        }
        System.out.println("实际支付:" + money * zk);
    }
}

In the above example, we show the nested usage of conditional statements to calculate the actual payment amount based on membership status and purchase amount.

9. String support for switch statements

Java 7 and above supports using strings as switchconditions for statements.

public class Demo10 {
    
    
    public static void main(String[] args) {
    
    
        String mingci = "1";
        switch (mingci) {
    
    
            case "1":
                System.out.println("夏令营");
                break;
            case "2":
                System.out.println("笔记本一台");
                break;
            case "3":
                System.out.println("移动硬盘");
                break;
            default:
                System.out.println("无");
                break;
        }
    }
}

In the above example, we use strings as switchconditions for statements to execute different codes based on different string values.

10. Transparent transmission phenomenon of switch statement

switchPassthrough in statements allows multiple casevalues ​​to share the same block of code.

public class Demo11 {
    
    
    public static void main(String[] args) {
    
    
        String mingci = "星期一";
        switch (mingci) {
    
    
            case "星期一":
            case "星期三":
                System.out.println("画画");
                break;
            case "星期二":
                System.out.println("休息");
                break;
            case "星期四":
                System.out.println("休息");
                break;
            case "星期五":
                System.out.println("移动硬盘");
                break;
            case "星期六":
            case "星期天":
                System.out.println("街舞");
                break;
            default:
                System.out.println("错误值");
                break;
        }
    }
}

In the above example, Monday and Wednesday share the same activity "Drawing", and Saturday and Sunday share the "Street Dance" activity.

11. Conditional statements and user interaction

Conditional statements are often used with user input to perform different actions based on user selections.

import java.util.Scanner;

public class Demo12 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入消费金额:");
        double money = scanner.nextDouble();
        System.out.println("是否换购:");
        System.out.println("1.满50元,加2元换购商品1");
        System.out.println("2.满100元,加3元换购商品2");
        System.out.println("3.满100元,加10元换购商品3");
        System.out.println("4.满200元,加10元换购商品4");
        System.out.println("5.满200元,加20元换购商品5");
        System.out.println("0.不换购");
        System.out.print("请选择:");
        int select = scanner.nextInt();
        String goods = "不换购"; // 默认值
        switch (select) {
    
    
            case 1:
                if (money > 50) {
    
    
                    money += 2;
                    goods = "商品1";
                }
                break;
            case 2:
                if (money > 100) {
    
    
                    money += 3;
                    goods = "商品2";
                }
                break;
            case 3:
                if (money > 100) {
    
    
                    money += 10;
                    goods = "商品3";
                }
                break;
            case 4:
                if (money > 200) {
    
    
                    money += 10;
                    goods = "商品4";
                }
                break;
            case 5:
                if (money > 200) {
    
    
                    money += 20;
                    goods = "商品5";
                }
                break;
        }
        System.out.println("消费金额:" + money);
        System.out.println("成功换购:" + goods);
    }
}

In the above example, based on the consumption amount and selection entered by the user, the program calculates the final payment amount and redemption items.

Summarize

Through reading this article, we have an in-depth study of conditional statements and control flow in the Java programming language. From the most basic if statements to more complex multiple conditional statements and string comparisons, we explore how to execute blocks of code based on different conditions. In addition, we also introduced the use of logical operators and the application of switch statements, and how to combine conditional statements with user interaction to make the program more intelligent and adaptable. These concepts and examples will help readers better understand and use conditional statements and control flow, and improve their programming skills and programming capabilities.

Guess you like

Origin blog.csdn.net/qq_42531954/article/details/132700055