Java EE - 程序控制语句-Ramdom类

一、if语句

/**
     目标:学会使用if分支结构解决问题,理解其流程。
 */
public class IfDemo1 {
    public static void main(String[] args) {
        // 需求:心跳(60 - 100)之间是正常的,否则系统提示进一步检查
        // 格式1: if(条件表达式){  代码... }
        int heartBeat = 30;
        if(heartBeat < 60 || heartBeat > 100) {
            System.out.println("您的心跳数据是:" + heartBeat +",您可能需要进一步检查!");
        }
        System.out.println("检查结束");

        // 格式2: if(条件表达式){ 代码... } else {  代码... }
        // 需求:发红包。
        double money = 1500;
        // 发送一个1314.
        if(money >= 1314){
            System.out.println("您当前发送红包成功~~~");
        }else {
            System.out.println("您自己都没钱,就别发了~~");
        }

        // 格式3: if(条件表达式){ 代码...}else if(条件表达式){ 代码... } ... else{ 代码...}
        // 绩效系统: 0-60 C  60-80 B 80-90 A 90-100 A+
        int score = 90;
        if(score >= 0 && score < 60){
            System.out.println("您本月的绩效是:C");
        }else if(score >= 60 && score < 80){
            System.out.println("您本月的绩效是:B");
        }else if(score >= 80 && score < 90){
            System.out.println("您本月的绩效是:A");
        }else if(score >= 90 && score <= 100){
            System.out.println("您本月的绩效是:A+");
        }else {
            System.out.println("您录入的分数有毛病!");
        }
    }
}

二、switch分支

import java.util.Scanner;

public class If {
    public static void main(String[] args) {
        // 2.创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        // 3.提示需要输入的数据,并调用方法获取输入的数据
        System.out.println("请输入第一个整数:");
        int num1 = sc.nextInt();
        System.out.println("请输入第二个整数:");
        int num2 = sc.nextInt();
        System.out.println("请输入您要进行的运算(1:表示加法,2:表示减法,3:表示乘法,4:表示除法)");
        int type = sc.nextInt();
        // 4.使用switch语句判断计算类型,并输出相应的结果
        switch (type) {
            case 1:
                System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
                break;
            case 2:
                System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
                break;
            case 3:
                System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
                break;
            case 4:
                System.out.println(num1 + " / " + num2 + " = " + (num1 * 1.0 / num2));
                break;
            default:
                System.out.println("您输入的运算类别有误");
                break;
        }
    }
}

 1.switch小练习

public class SwitchDemo2 {
    public static void main(String[] args) {
        // 目标:学会使用switch分支结构,理解流程
        // 周一:埋头苦干,解决bug                                  周五:今晚吃鸡
        // 周二:请求大牛程序员帮忙                             周六:与王婆介绍的小芳相亲
        // 周三:今晚啤酒、龙虾、小烧烤                              周日:郁郁寡欢、准备上班。
        // 周四: 主动帮助新来的女程序解决bug
        String weekday = "周四";
        switch (weekday){
            case "周一":
                System.out.println("埋头苦干,解决bug ");
                break;
            case "周二":
                System.out.println("请求大牛程序员帮忙");
                break;
            case "周三":
                System.out.println("今晚啤酒、龙虾、小烧烤");
                break;
            case "周四":
                System.out.println("主动帮助新来的女程序解决bug");
                break;
            case "周五":
                System.out.println("今晚吃鸡");
                break;
            case "周六":
                System.out.println("与王婆介绍的小芳相亲");
                break;
            case "周日":
                System.out.println("郁郁寡欢、准备上班");
                break;
            default:
                System.out.println("数据有误!");
        }
        System.out.println("晚上去夜店蹦迪");
    }
}

2. switch注意点-练习

public class SwitchDemo3 {
    public static void main(String[] args) {
        // 目标:清楚switch的注意点。并在开发的时候注意。
        // 表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String、不支持double、float、long。
        //double a = 0.1 + 0.2;
        //System.out.println(a);
        long lg = 20;
//      switch (lg){

//      }

        // case给出的值不允许重复,且只能是字面量,不能是变量。
        switch (3){
            case 31:
                break;
            case 3:
                break;
        }

        // 不要忘记写break,否则会出现穿透现象。
        String weekday = "周二";
        switch (weekday){
            case "周一":
                System.out.println("埋头苦干,解决bug ");
                break;
            case "周二":
                System.out.println("请求大牛程序员帮忙");
                //break;
            case "周三":
                System.out.println("今晚啤酒、龙虾、小烧烤");
                //break;
            case "周四":
                System.out.println("主动帮助新来的女程序解决bug");
                break;
            case "周五":
                System.out.println("今晚吃鸡");
                break;
            case "周六":
                System.out.println("与王婆介绍的小芳相亲");
                break;
            case "周日":
                System.out.println("郁郁寡欢、准备上班");
                break;
            default:
                System.out.println("数据有误!");
        }
    }
}

三、for循环

循环概念:条件成立,就重复执行

执行流程:

  1. 初始化变量
  2. 判断条件:结果一定是boolean值
    • true - 执行代码块,并更新变量,重复第2个步骤
    • false- 跳出整个循环语句
public class ForDemo1 {
    public static void main(String[] args) {
        // 目标:学会使用for循环,并理解它的执行流程。
        // 需求:输出3次HelloWorld
        for (int i = 1; i < 3; i++) {
            System.out.println("HelloWorld");
        }

        System.out.println("---------------------");
        for (int i = 0; i < 5; i++) {
            System.out.println("HelloWorld");
        }

        System.out.println("---------------------");
        for (int i = 1; i < 5; i++) {
            System.out.println("HelloWorld");
        }

        System.out.println("---------------------");
        for (int i = 1; i <= 5; i++) {

        }

        System.out.println("---------------------");
        for (int i = 1; i <= 5; i+=2) {
            System.out.println("HelloWorld");
        }
    }
}

 1.小练习

计算1-5的和

public class ForTest2 {
    public static void main(String[] args) {
        // 需求:计算1-5的和
        // 2、定义一个整数变量用于累加数据求和
        int sum = 0;
        // 1、定义一个for循环找到 1 2 3 4 5
        for (int i = 1; i <= 5 ; i++) {
            // i = 1 2 3 4 5
            // 3、把循环的数据累加给sum变量
            /**
               等价于: sum = sum + i
               i == 1  sum = 0 + 1
               i == 2  sum = 1 + 2
               i == 3  sum = 3 + 3
               i == 4  sum = 6 + 4
               i == 5  sum = 10 + 5
             */
            sum += i;
        }
        System.out.println("1-5的和是:" + sum);
    }
}

 求1-10的奇数和

public class ForTest3 {
    public static void main(String[] args) {
        // 需求:求1-10的奇数和
        // 3、定义一个求和的变量 累加奇数和
        int sum = 0;
        // 1、定义一个循环找到 1 2 3...10
        for (int i = 1; i <= 10; i++) {
            // i  1 2 3 4 5 6 7 8 9 10
            // 2、筛选出奇数
            if(i % 2 == 1){
                // i = 1 3 5 7 9
                sum += i;
            }
        }
        // 4、输出求和变量即可
        System.out.println("1-10的奇数和是:" + sum);

        System.out.println("-------------------");
        // 2、定义一个求和的变量 累加奇数和
        int sum1 = 0;
        // 1、定义循环找到 1 3 5 7 9
        for (int i = 1; i <= 10; i+=2) {
            // i = 1 3 5 7 9
            sum1 += i;
        }
        // 3、输出求和变量即可
        System.out.println("1-10的奇数和是:" + sum1);
    }
}

 找出水仙花数并输出

public class ForTest4 {
    public static void main(String[] args) {
        // 需求:找出水仙花数并输出
        // 在循环外定义一个变量用于记录水仙花的个数
        int count = 0;
        // 1、定义一个for循环找出全部三位数
        for (int i = 100; i <= 999; i++) {
            // 2、判断这个三位数是否满足要求
            // i = 157
            // 个位
            int ge = i % 10;
            // 十位
            int shi = i / 10 % 10;
            // 百位
            int bai = i / 100;
            if( (ge*ge*ge + shi * shi * shi + bai * bai * bai) == i){
                System.out.print(i+"\t");
                count++;
            }
        }
        System.out.println(); // 换行!
        System.out.println("水仙花个数是:" + count);
    }
}

2.for循环嵌套 

public class ForForDemo9 {
    public static void main(String[] args) {
        // 目标:理解嵌套循环的执行流程
        // 场景:假如你有老婆,然后你犯错了,你老婆罚你说5天,每天3句我爱你。
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("我爱你");
            }
            System.out.println("--------------");
        }

        /**
            *****
            *****
            *****
            *****
         */
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println(); // 换行
        }
    }
}

 九九乘法表

 

for(int i = 1;i<=9;i++){
	for(int j = 1;j<=i;j++){
		System.out.print(j + "x" + i + "=" + (i*j) + "\t");
	}
	System.out.println();
}

四、while循环

表达式的结果一定是boolean
true - 循环
false- 跳出循环

public class WhileDemo5 {
    public static void main(String[] args) {
        // 目标:学会使用while循环,并理解它的流程
        int i = 0;
        while (i < 3){
            System.out.println("Hello World");
            i++;
        }
        System.out.println("----------------------");
        int j = 0;
        while (j < 3){
            System.out.println("Hello World");
        }
    }
}

 1.while循环小练习

public class WhileTest6 {
    public static void main(String[] args) {
        // 需求:珠穆朗峰高度是8848860 纸张厚度 0.1 折叠纸张直到不低于珠穆朗峰位置,求折叠几次
        // 1、定义变量记录山峰的高度 纸张的厚度
        double peakHeight = 8848860;
        double paperThickness = 0.1;

        // 3、定义一个变量用于记录纸张折叠的次数
        int count = 0;

        // 2、定义一个while循环控制纸张进行折叠
        while (paperThickness < peakHeight){
            // 让纸张的厚度多一倍
            paperThickness *= 2;
            count++;
        }
        System.out.println("折叠的次数:" + count);
        System.out.println("纸张的最终厚度:" + paperThickness);
    }
}

五、dowhile循环

先执行一遍代码块
再判断表达式,表达式的值一定是boolean类型
true - 循环
false- 跳出循环

public class DoWhileDemo7 {
    public static void main(String[] args) {
        // 目标:学会使用dowhile循环,并理解其执行流程
        int i = 0;
        do {
            System.out.println("Hello World");
            i++;
        }while (i < 3);

        System.out.println("--------------------");
        for (int j = 0; j < 3; j++) {
            System.out.println("Hello World");
        }
        for (int j = 0; j < 3; j++) {
            System.out.println("Hello World");
        }

        int n = 0;
        while (n < 3){
            System.out.println("Hello World");
            n++;
        }
        System.out.println(n);
    }
}

六、定义死循环

import java.util.Scanner;

public class DeadForDemo8 {
    public static void main(String[] args) {
        // 目标:学会定义死循环。
//        for ( ; ; ) {
//            System.out.println("Hello World~~~~");
//        }

        // 经典写法
//        while (true) {
//            System.out.println("我是快乐的死循环~~~~");
//        }

//        do{
//            System.out.println("我是快乐的死循环~~~~");
//        }while (true);
        System.out.println("-----------------------------");

        // 1、定义正确的密码
        int okPassword = 520;
        // 2、定义一个死循环让用户不断的输入密码认证
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入正确的密码:");
            int password = sc.nextInt();
            // 3、使用if判断密码是否正确
            if(password == okPassword){
                System.out.println("登录成功了~~~");
                break; // 可以理解结束当前所在循环的执行的
            }else {
                System.out.println("密码错误!");
            }
        }
    }
}

 

七、break和continue作用

break作用于循环中,表示跳出当前循环

continue作用于循环中,表示跳过循环体剩余的部分 

public class BreakAndContinueDemo10 {
    public static void main(String[] args) {
        // 目标:理解break 和 continue的作用。
        // 场景:假如你又有老婆了,然后你犯错了,你老婆罚你做5天家务,每天都是洗碗。
        // 但是洗碗到第三天后心软了 原谅你了不用洗了
        for (int i = 0; i < 5; i++) {
            System.out.println("快乐的洗碗~~~~");
            if(i == 2) {
                break; // 跳出并结束当前循环的执行~~
            }
        }

        // continue 跳出当前循环的当次执行,进入循环的下一次
        // 场景:假如你又有老婆了,然后你犯错了,你老婆罚你做5天家务,
        // 每天都是洗碗。但是洗碗到第三天后心软了 原谅你了不用洗了 但是依然不解恨 继续洗第4天 5天
        for (int i = 1; i <= 5 ; i++) {
            if(i == 3){
                continue; // 立即跳出当次执行,进入循环的下一次!
            }
            System.out.println("洗碗:" + i);
        }


    }
}

 输入一个年份,再输入一个月份,把那个月的日历打印出来

 

//1.输入年和月
Scanner scan = new Scanner(System.in);
System.out.println("请输入年:");
int year = scan.nextInt();// 2020
System.out.println("请输入月:");
int month = scan.nextInt();// 6
//2.计算1900~输入年的天数
int allDayOfYear = 0;
for (int i = 1900; i < year; i++) {
    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//闰年
        allDayOfYear += 366;
    } else {//平年
        allDayOfYear += 365;
    }
}
//3.计算1~输入月的天数
int allDayOfMonth = 0;
for (int i = 1; i < month; i++) {
    switch (i) {
        case 1:case 3:case 5:case 7:case 8:case 10:case 12:
            allDayOfMonth += 31;
            break;
        case 4:case 6:case 9:case 11:
            allDayOfMonth += 30;
            break;
        case 2:
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                allDayOfMonth += 29;
            } else {
                allDayOfMonth += 28;
            }
            break;
    }
}
//4.计算1900~输入年的天数 + 计算1~输入月的天数
int allDay = allDayOfYear + allDayOfMonth;
//5.计算星期
int week = allDay % 7 + 1;
//6.计算当月的天数
int day = 0;
switch (month) {
    case 1:case 3:case 5:case 7:case 8:case 10:case 12:
        day = 31;
        break;
    case 4:case 6:case 9:case 11:
        day = 30;
        break;
    case 2:
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            day = 29;
        } else {
            day = 28;
        }
        break;
}
//7.打印日历
System.out.println("一\t二\t三\t四\t五\t六\t日");
int num = 0;//控制换行
//打印空格
for (int i = 1; i < week; i++) {
    System.out.print("\t");
    num++;
}
//打印日期
for (int i = 1; i <= day; i++) {
    System.out.print(i + "\t");
    num++;
    if (num % 7 == 0) {
        System.out.println();//换行
    }
}

八、break和continue操作外部循环

public class BreakAndContinueDemo11 {
    public static void main(String[] args) {
        // 目标:理解break和continue操作外部循环。
        // 场景:假如你有老婆,你老婆罚你说3天,每天5句我爱你,但是说到第二天的第3句就心软了,以后都不用说了!
        OUT:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 5; j++) {
                System.out.println("我爱你~~");
                if(i == 2 && j == 3){
                    break OUT; // 指定跳出外部循环,并结束外部循环了!
                }
            }
            System.out.println("-----------------");
        }

        System.out.println("====================================================");
        // continue可以指定结束外部循环的当次执行,进入外部循环的下一次执行
        // 场景:假如你有老婆,你老婆罚你说3天,每天5句我爱你,但是说到第二天的第3句就心软了,当天
        // 不用说了,但是依然不解恨,第3天还是要说的。
        OUT:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 5; j++) {
                System.out.println("我爱你~~");
                if(i == 2 && j == 3){
                    continue OUT;
                }
            }
            System.out.println("-----------------");
        }
    }
}

九、 随机数Random类

import java.util.Random;

public class RandomDemo1 {
    public static void main(String[] args) {
        // 目标:学会使用Java提供的随机数类Random
        // 1、导包
        // 2、创建随机数对象
        Random r = new Random();

        // 3、调用nextInt功能(方法)可以返回一个整型的随机数给你
        for (int i = 0; i < 20; i++) {
            int data = r.nextInt(10); // 0 - 9 不包含10的(包前不包后)
            System.out.println(data);
        }

        System.out.println("-----------------------");
        // 1 - 10 ==> -1 ==> (0 - 9) + 1
        int data = r.nextInt(10) + 1;
        System.out.println(data);

        // 3 - 17 ==> -3 ==> (0 - 14) + 3
        int data1 = r.nextInt(15) + 3;
        System.out.println(data1);
    }
}

 1.随机数ramdom小练习

import java.util.Random;
import java.util.Scanner;

public class RamdomTest2 {
    public static void main(String[] args) {
        // 1、随机一个幸运号码 1- 100之间  (0 - 99) + 1
        Random r = new Random();
        int luckNumber = r.nextInt(100) + 1;

        // 2、使用一个死循环让用户不断的去猜测,并给出提示
        Scanner sc = new Scanner(System.in);
        while (true) {
            // 让用户输入数据猜测
            System.out.println("请您输入猜测的数据(1-100):");
            int guessNumber = sc.nextInt();

            // 3、判断这个猜测的号码与幸运号码的大小情况
            if(guessNumber > luckNumber){
                System.out.println("您猜测的数据过大~");
            }else if(guessNumber < luckNumber){
                System.out.println("您猜测的数据过小");
            }else {
                System.out.println("恭喜您,猜中了,可以去买单了~~~");
                break; // 直接跳出并结束当前死循环!!
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值