Scanner键盘录入和语句结构体

ndmtzwdx / 2024-10-23 / 原文

一、键盘录入

import java.util.Scanner;

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

    今天只需要掌握如何使用即可,不需要关系细节,后面会再说

    实现键录入的步骤:
        1、导包
        2、创建键盘录入对象
        3、调用方法实现键盘录入
            1)输入整数
            2)输入字符串

    Scanner,是jdk1.5之后出现的,思考:jdk1.5之前人们想要手动输入数据的话,怎么办?


 */


public class ScannerDemo1 {
    public static void main(String[] args) {
        //需求:求两个数之和
//        int a1 = 3;
//        int b1 = 4;
//        System.out.println(a1+b1);

        //数据类型 变量名 = 初始化值;
        Scanner sc = new Scanner(System.in);
//
//        System.out.println("请输入一个整数:");
//        int i = sc.nextInt(); // 程序运行到这一步的时候,发生阻塞,等待用户输入一个整数
//        System.out.println("用户输入的内容是:" + i);
//        System.out.println("请输入二个整数:");
//        int i2 = sc.nextInt();
//        System.out.println("用户输入的内容是:" + i2);
//        System.out.println("两个数之和为:" + (i + i2));

        System.out.println("请输入您的姓名: ");
        String name = sc.next();
        System.out.println("请输入您的年龄: ");
        int age = sc.nextInt();

        System.out.println("姓名:" + name + ", 年龄:" + age);


    }
}

二、顺序结构

public class ShunXuDemo1 {
    public static void main(String[] args) {
        //顺序结构:按照代码的编写顺序,自上而下执行
        System.out.println("hello world8");
        System.out.println("hello world2");
        System.out.println("hello world3");
        System.out.println("hello world4");
        System.out.println("hello world5");
        System.out.println("hello world6");
        System.out.println("hello world7");
        System.out.println("hello world1");
    }
}

三、if语句

1.第一种格式

import java.util.Scanner;
/*
    选择结构:
        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.第二种格式

import java.util.Scanner;

/*
    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.第三种格式

import java.util.Scanner;

/*
    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("成绩有误!");
        }

    }
}

四、for循环

适合在循环确定次数的时候使用

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

    注意事项:
        1、初始化条件语句,有且仅执行一遍
        2、初始化条件语句可以写在for循环的外面,和定义在for循环内部时比较,作用域不同
        3、大括号可以省略,但是省略后只能作用在第一条语句上,建议,永远不要省略
        4、判断条件语句能否不写?可以不写,但是会变成死循环
        5、控制条件语句也可以不写,但是可能会是死循环

    一个最简单的for死循环:
        for(;;){
            ....
        }

 */
public class ForXunDemo1 {
    public static void main(String[] args) {
        //需求:在控制台中输出10行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");
//        System.out.println("helloworld");

        //初始化语句:定义一个变量表示范围,每循环一次需要改变一次  int i = 1
        //判断条件语句:10行:范围 1-10 i<=10
        //控制条件语句:i++
        //循环体语句:System.out.println("helloworld");

        //使用for循环改进
//        int i = 1;
        for (int i = 1; i <= 10;i++) {
            System.out.println("helloworld - " + i);

//            System.out.println("helloworld - " + i);
        }


//        System.out.println(i);


    }
}

1.练习1

/*
    请在控制台输出数据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.out.println("------------------------");
        //求出1-10之间数据之和
        int sum = 0;
        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.练习2

/*
    需求:在控制台输出九九乘法表。
 */
public class ForTest2 {
    public static void main(String[] args) {
        // 需求1:在控制台中输出5行5列的*
        //输出一行
//        System.out.println(); //换行输出
//        System.out.print(); // 不换行输出
//        System.out.print("*\t"); // \t -> Tab键 -> 默认是4个空白字符
//        System.out.print("*\t");
//        System.out.print("*\t");
//        System.out.print("*\t");
//        System.out.print("*\t");
        //使用for循环输出一行*
//        for(int i=1;i<=5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//
//        for(int i=1;i<=5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//
//        for(int i=1;i<=5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//
//        for(int i=1;i<=5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//
//        for(int i=1;i<=5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
        //使用循环打印5行
        //双重for循环
        for (int i = 1; i <= 5; i++) { // 外层循环控制的是行数
            for (int j = 1; j <= 5; j++) { //内层循环控制的是列数
                System.out.print("*\t");
            }
            System.out.println();
        }

        System.out.println("------------------------------------");
        // 需求2:在控制台中输出三角形的*
        /*
               *                    第 1 行,共 1 列
               *    *               第 2 行,共 2 列
               *    *   *           第 3 行,共 3 列
               *    *   *   *       第 4 行,共 4 列
               *    *   *   *   *   第 5 行,共 5 列
               ...                  第 n 行,共 n 列
         */
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*\t");
            }
            System.out.println();
        }
        System.out.println("------------------------------------");
        //在控制台输出九九乘法表。
        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + (i * j)+"\t");
            }
            System.out.println();
        }
    }
}

五、switch选择语句

import java.util.Scanner;

/*
    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;
        }

    }
}

import java.util.Scanner;

/*
    输入月份输出季节:
        春季 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;
        }

    }
}

六、while循环语句

适合在不知道循环的次数的时候使用

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

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

     注意:
        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);
    }
}

while循环练习

/*
    我国最高山峰是珠穆朗玛峰: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+"次");

        //死循环
//        while (true){
//
//        }
    }
}