Java 初学 day03

java 03

1、if判断语句

1、定义语句1

选择结构:
    if选择语句
    switch选择语句

if选择语句:
    语句定义格式1:
        if(关系表达式){
           语句体;
        }

注意

注意事项:
    1、if小括号中的语句,可以很复杂,但是最终的结果一定是boolean类型
    2、只有当if语句体中的语句只有一行的时候,大括号才可以省略,建议永远不要省略大括号
    3、小括号后面可以添加分号,相当于if语句拥有一个空的语句体

代码示例

public class IfDemo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入江川的性别:");
        String gender = sc.next();


        //A
        //B
        //A.equals(B)
        if("男".equals(gender));{
            System.out.println("去男厕所");
            System.out.println("洗手");
        }

    }
}

2、定义语句2

if语句定义格式2:
        if(关系表达式){
            语句体1;
        }else{
            语句体2;
        }

注意

注意:
    if-else语句中,只会执行其中某一个语句体,不会同时都执行!

代码示例

public class IfDemo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入江川的性别:");
        String gender = sc.next();


        //A
        //B
        //A.equals(B)
        if("nan".equals(gender)){
            System.out.println("去男厕所");
            System.out.println("洗手");
        }else {
            System.out.println("去女厕所");
            System.out.println("洗手");
        }

    }
}

3、定义语句3

if语句的第三种格式:
    if(关系表达式1){
        语句体1;
    }else if(关系表达式2){
        语句体2;
    }...{
        语句体n;
    }else{
        语句体n+1;
    }

代码示例

/*
需求:
     1、通过把学生考试成绩分等级来引出if语句的第三种格式
         90-100 优秀
         80-90  好
         70-80  良
         60-70  及格
         60一下   不及格
*/

public class IfDemo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入江川的考试成绩:");
        int score = sc.nextInt();

        if(score>=90 && score<=100){
            System.out.println("优秀");
        }else if(score>=80 && score<90){
            System.out.println("好");
        }else if(score>=70 && score<80){
            System.out.println("良");
        }else if(score>=60 && score<70){
            System.out.println("及格");
        }else if(score>=0 && score<60){
            System.out.println("不及格");
        }else {
            System.out.println("成绩有误!");
        }

    }
}

4、练习

//获取三个数据中的最大值
public class IfTest1 {
    public static void main(String[] args) {
        
        int a = 4;
        int b = 9;
        int c = 6;

        if(a<b){
            if(b<c){
                System.out.println("c是最大值");
            }else {
                System.out.println("b是最大值");
            }
        }else {
            if(a>c){
                System.out.println("a是最大值");
            }else {
                System.out.println("c是最大值");
            }
        }
    }
}

2、switch语句

switch选择语句:
     语句定义格式:
         switch(表达式){
             case 常量值1:
                 表达式1;
                 break;
             case 常量值2:
                 表达式2;
                 break;
             ...
             default:
                 表达式n;
                 break;

         }

执行流程:严格按照下面的流程执行。
     1、先计算表达式中的值
     2、然后拿着计算出来的值自上而下匹配所有的case,当某一个case后面的常量值与计算出来的值一样的时候,
         执行其中的语句体,遇到break,结束整个switch语句.
     3、当所有的case都不匹配的时候,执行default中的语句体,当遇到break的时候,结束整个switch语句.

 注意:
     1、表达式的取值:byte,short,int,char,枚举,String
     2、case后面只能跟常量,不能是变量
     3、break能不能不写?能不写,但是会发生switch穿透!
     4、default语句能不能不写?可以不写,但是为了程序的严谨性,最好加上
     5、default语句是不是一定要放在所有case之后呢?不一定,可以放在switch语句中的任意位置

代码示例

public class SwitchDemo {
    public static void main(String[] args) {
        //假设我们所带的金额,正好是购买一瓶饮料的价格,多了或少了都买不了,只有正好才可以消费
        //可乐 3,脉动 5,红牛 7,ad钙奶 4
        System.out.println("请输入您带的金额:");
        Scanner sc = new Scanner(System.in);
        int price = sc.nextInt();

        switch (price) {
            case 3:
                System.out.println("欢迎购买一瓶 可乐!");
                break;
            case 4:
                System.out.println("欢迎购买一瓶 ad钙奶!");
                break;
            case 5:
                System.out.println("欢迎购买一瓶 脉动!");
                break;
            case 7:
                System.out.println("欢迎购买一瓶 红牛!");
                break;
            default:
                System.out.println("没有您所带金额的饮料!!");
                break;
        }

    }
}

练习

/*
    输入月份输出季节:
        春季 3-5
        夏季 6-8
        秋季 9-11
        冬季 12-2
 */
 public class SwitchDemo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入需要查看季节的月份:");
        int season = sc.nextInt();

        switch (season){
            case 12:
            case 1:
            case 2:
                System.out.println("冬季");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println("春季");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏季");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋季");
                break;
            default:
                System.out.println("没有该月份。。。");
                break;
        }

    }
}

3、scanner

键盘录入:程序在运行过程中,用户可以根据自己的需求输入参运算的值

实现录入的步骤:

​ 1、导包

import java.util.Scanner;

​ 2、创建键盘录入对象

Scanner scanner = new Scanner(System.in);

​ 3、调用方法键盘录入

​ 1)输入整数

int i = scanner.nextInt();

​ 2)输入字符串

String i = scanner.next();

代码示例

public class ScannerDemo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的姓名: ");
        String name = sc.next();
        System.out.println("请输入您的年龄: ");
        int age = sc.nextInt();
        System.out.println("姓名:" + name + ", 年龄:" + age);
    }
}

4、循环

1、for循环

for循环:

​ 语句定义格式:
​ for(初始化语句;判断条件语句;控制条件语句){

​ 循环体语句;

​ }

注意事项:

​ 1、初始化条件语句,有且仅执行一遍

​ 2、初始化条件语句可以写在for循环的外面,和定义在for循环内部时比较,作用域不同

​ 3、大括号可以省略,但是省略后只能作用在第一条语句上,建议,永远不要省略

​ 4、判断条件语句能否不写?可以不写,但是会变成死循环

​ 5、控制条件语句也可以不写,但是可能会是死循环

一个最简单的for死循环:

​ for(;😉{

​ ......

​ }

//需求:在控制台中输出10行helloworld
public class ForXunDemo1 {
    public static void main(String[] args) {
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
        System.out.println("helloworld");
    }
}
//用for循环改进
/*
	初始化语句:定义一个变量表示范围,每循环一次需要改变一次  int i = 1
	判断条件语句:10行:范围 1-10 i<=10
	控制条件语句:i++
	循环体语句:System.out.println("helloworld");
*/
public class ForXunDemo1 {
    public static void main(String[] args) {
     	for (int i = 1; i <= 10;i++) {
            System.out.println("helloworld - " + i);
        }
     	
    }
}

练习:

package com.shujia.day03;

/*
    请在控制台输出数据1-10
    请在控制台输出数据10-1
    求出1-10之间数据之和
    求出1-100之间偶数和
    求出1-100之间奇数和
    求5的阶乘【自己实现】
    在控制台输出所有的”水仙花数”,统计”水仙花数”共有多少个

 */
public class ForTest {
                public static void main(String[] args) {
                    //请在控制台输出数据1-10
                    for (int i = 1; i <= 10; i++) {
                        System.out.println(i);
        }
        System.out.println("------------------------");
        //请在控制台输出数据10-1
        for (int i = 10; i > 0; i--) {
            System.out.println(i);
        }
        System.opublic class ForTest {
            public static void main(String[] args) {
                //请在控制台输出数据1-10
                for (int i = 1; i <= 10; i++) {
                    System.out.println(i);
                }
                System.out.println("------------------------");

        for (int i = 1; i <= 10; i++) {
            sum += i;
        }
        System.out.println("1-10之间的和为:" + sum);
        System.out.println("------------------------");
        //求出1-100之间偶数和
        //求出1-100之间奇数和
        int ouSum = 0;
        int jiSum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                ouSum += i;
            } else {
                jiSum += i;
            }
        }
        System.out.println("1-100之间的偶数和为:" + ouSum);
        System.out.println("1-100之间的奇数和为:" + jiSum);
        System.out.println("------------------------");

        int count = 0;
        for (int i = 100; i < 1000; i++) {
            int baiWei = i / 100;
            int shiWei = i % 100 / 10;
            int geWei = i % 10;
            if ((baiWei * baiWei * baiWei + shiWei * shiWei * shiWei + geWei * geWei * geWei) == i) {
                System.out.println(i);
                count++;
            }
        }

        System.out.println("水仙花数共计 " + count + " 个");
    }
}

2、while循环

1、定义

while循环:
    语句定义格式1:
        初始化条件语句;
        while(判断条件语句){
           循环体语句;
           控制条件语句;
        }

     语句定义格式2:
        初始化条件语句;
        do{
            循环体语句;
           控制条件语句;
        }while(判断条件语句)

2、注意

 1、while循环可以和for循环等价转换
 2、for循环和while循环的使用场景:
        for循环适合明确的范围内循环  【吃葡萄🍇】
        当循环次数不明确获取就是要求次数的时候,优先考虑while循环【喝水】

代码示例

public class WhileDemo1 {
    public static void main(String[] args) {
        //输出10行hello world
        for (int i = 1; i < 0; i++) {
            System.out.println("hello world");
        }
        System.out.println("---------------------------");
        int i = 1;
        while (i < 0){
            System.out.println("hello world");
            i++;
        }
        System.out.println("---------------------------");
        int i2 = 1;
        do{
            System.out.println("hello world");
            i2++;
        }while (i2 < 0);
    }
}

练习:

package com.shujia.day03;

/*
    我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。
    请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?

 */
public class WhileTest1 {
    public static void main(String[] args) {
        int high = 884800;
        int thickness = 1;
        int count = 0;
        while (thickness<high){
            thickness *=2;
            count++;
        }

        System.out.println("共折叠 "+count+"次");
}

5、跳转控制语句 方法

跳转控制语句:
    break关键字
    continue
    return

1、break

	break的作用:
		跳出单层循环
		跳出多层循环
			带标签的跳出
			格式:标签名: 循环语句
			标签名要符合Java的命名规则

    break: 打破,打碎,终止

    使用break的注意事项:
        1、break的使用需要在特定的场景下使用才有意义
        2、break只能在switch选择语句或者循环中使用
public class BreakDemo {
    public static void main(String[] args) {
//        break;

        //输出1-10,当遇到5的时候,使用break
        for(int i=1;i<=10;i++){
            if(i==5){
                break; // 终止整个for循环
            }
            System.out.println(i);
        }

        System.out.println("-------------------------------------");
        //需求:当j==5的时候,使用break
        jc:for (int i = 1; i <= 10; i++) {
            lg:for (int j = 1; j <= i; j++) {
                if(j==5){
//                    break; // 终止最近的整个for循环
                    break jc;
                }
                System.out.print(j + "*" + i + "=" + (i * j)+"\t");
            }
            System.out.println();
        }
    }
}

2、continue

continue的使用场景:
	在循环语句中
	离开使用场景的存在是没有意义的
	
continue的作用:
	单层循环对比break,然后总结两个的区别
		break  退出当前循环
		continue  退出本次循环

代码示例

public class ContinueDemo {
    public static void main(String[] args) {
        for(int i=1;i<=10;i++){
            if(i==5){
                continue; // 结束当次循环,继续下一次循环
            }
            System.out.println(i);
        }
    }
}

3、return

return关键字不是为了跳转出循环体,更常用的功能是结束一个方法,也就是退出一个方法。跳转到上层调用的方法。
return必须在方法中写,一个方法只会有一个return生效,表示结束整个方法
public class ReturnDemo {
//    return; // return必须在方法中写

    public static void main(String[] args) {
//        return; // 结束整个方法
//        System.out.println("hello world");
//        return;

        for(int i=1;i<=10;i++){
            if(i==5){
                return;
            }
            System.out.println(i);
        }
        System.out.println("hello world");
    }
}

posted @ 2024-09-19 23:44  小钱没有钱  阅读(7)  评论(0编辑  收藏  举报