From 7dc34339a2e392d35a4c948511d6676d98f4489c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=9B=BD=E4=BA=AE?= <647597435@qq.com> Date: Mon, 5 Dec 2022 18:45:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=88=91=E7=9A=84=E7=AC=AC=E4=BA=94?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\236\204if\350\257\255\345\217\245.md" | 297 ++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 "19 \345\224\220\345\233\275\344\272\256/20221203\346\216\247\345\210\266\350\257\255\345\217\245\345\222\214\351\241\272\345\272\217\347\273\223\346\236\204if\350\257\255\345\217\245.md" diff --git "a/19 \345\224\220\345\233\275\344\272\256/20221203\346\216\247\345\210\266\350\257\255\345\217\245\345\222\214\351\241\272\345\272\217\347\273\223\346\236\204if\350\257\255\345\217\245.md" "b/19 \345\224\220\345\233\275\344\272\256/20221203\346\216\247\345\210\266\350\257\255\345\217\245\345\222\214\351\241\272\345\272\217\347\273\223\346\236\204if\350\257\255\345\217\245.md" new file mode 100644 index 0000000..69bcc87 --- /dev/null +++ "b/19 \345\224\220\345\233\275\344\272\256/20221203\346\216\247\345\210\266\350\257\255\345\217\245\345\222\214\351\241\272\345\272\217\347\273\223\346\236\204if\350\257\255\345\217\245.md" @@ -0,0 +1,297 @@ +if语句格式 +示例: + +~~~java +public class IfDemo { + public static void main(String[] args) { + System.out.println("开始"); + + // 如果年龄大于18岁, 就可以上网吧 + int age = 17; + + if(age >= 18){ + // int a = 10; + System.out.println("可以上网吧"); + } + + System.out.println("结束"); + } +} + + +if语句格式2(理解) + +~~~java +格式: +if (关系表达式) { + 语句体1; +} else { + 语句体2; +} +~~~ + +示例:奇偶数 + +​ 任意给出一个整数,请用程序实现判断该整数是奇数还是偶数,并在控制台输出该整数是奇数还是偶数。 + +~~~java +public class Demo2If { + public static void main(String[] args) { + // 程序判断一个数, 是奇数还是偶数 + int num = 9; + + if(num % 2 == 0){ + System.out.println("偶数"); + }else{ + System.out.println("奇数"); + } + } +} +~~~ + +if语句格式3 + +~~~java +格式: +if (关系表达式1) { + 语句体1; +} else if (关系表达式2) { + 语句体2; +} +… +else { + 语句体n+1; +} +~~~ +示例: + +​ 定义一个在0~100之间的变量a, 90~100优秀,80~89良好,70~79中等,60~69及格,0~59请努力加油! + +~~~java +public class Demo3If { + public static void main(String[] args){ + int score = 65; + if(score >= 90 && score <= 100){ + System.out.println("优秀"); + }else if (score >= 80 && score <= 89){ + System.out.println("良好"); + }else if (score >= 70 && score <= 79){ + System.out.println("中等"); + }else if (score >= 60 && score <= 69){ + System.out.println("及格"); + }else if (score >= 0 && score <= 59){ + System.out.println("请努力加油"); + }else{ + System.out.println("成绩有误!"); + } + } +} +~~~ + +~~~java +import java.util.Scanner; +public class IfTest02 { + public static void main(String[] args){ + // 1. 使用Scanner录入考试成绩 + Scanner sc = new Scanner(System.in); + System.out.println("请输入您的成绩:"); + int score = sc.nextInt(); + // 2. 判断成绩是否在合法范围内 0~100 + if(score >=0 && score <= 100){ + // 合法成绩 + // 3. 在合法的语句块中判断成绩范围符合哪一个奖励 + if(score >= 95 && score <= 100){ + System.out.println("自行车一辆"); + }else if(score >= 90 && score <= 94){ + System.out.println("游乐场一次"); + }else if(score >= 80 && score <= 89){ + System.out.println("变形金刚一个"); + }else { + System.out.println("挨顿揍, 这座城市又多了一个伤心的人~"); + } + }else{ + // 非法的话, 给出错误提示 + System.out.println("您的成绩输入有误!"); + } + } +} +~~~ + +switch语句 + 分支语句switch语句 + +switch (表达式) { +case 1: +语句体1; +break; +case 2: +语句体2; +break; +... +default: +语句体n+1; +break; +} + +switch案例-减肥计划 +周一:跑步 +周二:游泳 +周三:慢走 +周四:动感单车 +周五:拳击 +周六:爬山 +周日:好好吃一顿 +示例代码: +public static void main(String[] args){ +// 1. 键盘录入星期数据,使用变量接收 +Scanner sc = new Scanner(System.in); +System.out.println("请输入"); +int week = sc.nextInt(); +// 2. 多情况判断,采用switch语句实现 +switch(week){ +// 3. 在不同的case中,输出对应的减肥计划 +case 1: +System.out.println("跑步"); +break; +case 2: +System.out.println("游泳"); +break; +case 3: +System.out.println("慢走"); +break; +case 4: +System.out.println("动感单车"); +break; +case 5: +System.out.println("拳击"); +break; +case 6: +System.out.println("爬山"); +break; +case 7: +System.out.println("好好吃一顿"); +break; +default: +System.out.println("您的输入有误"); +break; +} +} +} + +作业 +import java.util.Scanner; + +public class D { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入车型,夏利:1,富康:2,桑塔纳:3"); + int xl=1; + int fk=2; + int xtn=3; + int lc=sc.nextInt(); + System.out.println("请输入距离公里单位"); + int jl=sc.nextInt(); + double lx=(jl-3)*2.1+3; + double ff=(jl-3)*2.4+4; + double stn=(jl-3)*2.7+5; + switch (lc){ + case 1: + if(jl<=3){ + System.out.println("车型:夏利, 车费:3元,"); + break; + } else if(jl>3){ + System.out.println("车型:夏利, 车费:"+lx); + break; + } + } + switch (lc){ + case 2: + if(ff<=3){ + System.out.println("车型:富康, 车费:4元,"); + break; + } else if(ff>3){ + System.out.println("车型:富康, 车费:"+ff); + break; + } + } + switch (lc){ + case 3: + if(stn<=3){ + System.out.println("车型:富康, 车费:5元,"); + break; + } else if(stn>3){ + System.out.println("车型:富康, 车费:"+stn); + break; + } + } + } +} + +第二题 +import java.util.Scanner; + +public class D { + public static void main(String[] args) { + int d=0; + int mo=0; + int ye=0; + int su=0; + int le; + System.out.println("请输入年,月,日"); + Scanner sc=new Scanner(System.in); + ye= sc.nextInt(); + mo= sc.nextInt(); + d= sc.nextInt(); + switch (mo){ + case 1: + su=0; + break; + case 2: + su=31; + break; + case 3: + su=59; + break; + case 4: + su=90; + break; + case 5: + su=120; + break; + case 6: + su=151; + break; + case 7: + su=181; + break; + case 8: + su=212; + break; + case 9: + su=243; + break; + case 10: + su=273; + break; + case 11: + su=304; + break; + case 12: + su=334; + break; + default: + System.out.println("data error");break; + } + su=su+d; + if(ye%400==0||(ye%4==0 && ye%100!=0)) + le=1; + else + le=0; + if(le==1 && mo>2) + su++; + System.out.println("今天是第:"+su); + } +} + + +第三题 -- Gitee From 0c31f539f628e2783f4af0cea690bbdb57765a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=9B=BD=E4=BA=AE?= <647597435@qq.com> Date: Fri, 9 Dec 2022 10:34:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\345\222\214\346\225\260\347\273\204.md" | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 "19 \345\224\220\345\233\275\344\272\256/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" diff --git "a/19 \345\224\220\345\233\275\344\272\256/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" "b/19 \345\224\220\345\233\275\344\272\256/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" new file mode 100644 index 0000000..3672b99 --- /dev/null +++ "b/19 \345\224\220\345\233\275\344\272\256/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" @@ -0,0 +1,163 @@ +# Random产生随机数 + +##### **概述: Random类似Scanner,也是Java提供好的API,内部提供了产生随机数的功能 API后续课程详细讲解,现在可以简单理解为Java已经写好的代码 使用步骤:** + +##### ** 1. 导入包 import java.util.Random; + +##### 2. 创建对象 Random r = new Random(); 3 + +##### . 产生随机数 int num = r.nextInt(10); 解释: 10代表的是一个范围,如果括号写10,产生的随机数就是0-9,括号写20,参数的随 机数则是0-19 + +##### 示例代码:** + +```java +import java.util.Random; +public class Demo1Random { + /* Random : 产生随机数 + 1. 导包 : import java.util.Random; 导包的动作必须出现在类定义的上面 + 2. 创建对象 : Random r = new Random(); 上面这个格式里面,r 是变量名,可以变,其他的都不允许变 + 3. 获取随机数 : int number = r.nextInt(10); //获取数据的范围:[0,10) 包 括0,不包括10 上面这个格式里面,number是变量名,可以变,数字10可以变。其他的都不 允许变 + 需求: 产生随机数1-10之间的 */ public static void main(String[] args){ // + 2. 创建对象 Random r = new Random(); for(int i = 1; i <= 10; i++){ // + 3. 获取随机数 int num = r.nextInt(10) + 1; // 1-10 System.out.println(num); } } } +``` + +# 数组 + +数组介绍 + +数组就是存储数据长度固定的容器,存储多个数据的数据类型要一致。 + +数组的定义格式 + +第一种格式: + +```java +int[] arr; +double[] arr; +char[] arr; +``` + + 第二种格式: + +```java +int arr[]; +double arr[]; +char arr[]; +``` + +数组的动态初始化 + +什么是动态初始化:数组动态初始化就是只给定数组的长度,由系统给出默认初始化值。 + +动态初始化格式; + +```java +数据类型[] 数组名 = new 数据类型[数组长度]; + +int[] arr = new int[3]; +``` + + 动态初始化格式详解 + +- 等号左边: + + - int:数组的数据类型 + + - []:代表这是一个数组 + + - arr:代表数组的名称 + +- 等号右边: + + - new:为数组开辟内存空间 + - int:数组的数据类型 + - []:代表这是一个数组 + - 5:代表数组的长度 + +**代码 :** + +```java +package com.itheima.array; + +public class Demo2Array { + /* + 数组的动态初始化: + 在初始化的时候, 需要手动指定数组的长度, 系统会为数组容器分配初始值. + + 动态初始化格式: + 数据类型[] 数组名 = new 数据类型[数组的长度]; + + 注意: + 打印数组变量的时候, 会打印出数组的内存地址 + + [I@10f87f48 : + + @ : 分隔符 + [ : 当前的空间是一个数组类型 + I : 当前数组容器中所存储的数据类型 + 10f87f48 : 十六进制内存地址 + + 0 1 2 3 4 5 6 7 8 9 a b c d e f + */ + public static void main(String[] args) { + // 数据类型[] 数组名 = new 数据类型[数组的长度]; + // 通过new关键字创建了一个int类型的数组容器, 该容器可以存储5个int类型的整数, 该容器被arr数组变量所记录 + int[] arr = new int[5]; + // [I@10f87f48 + System.out.println(arr); + + byte[] bArr = new byte[3]; + // [B@b4c966a + System.out.println(bArr); + + } + +} +``` + +# 作业 + +需求: 程序自动生成一个1-100之间的数字,使用程序实现猜出这个数字是多少? 当猜错的时候根据不同情况给出相应的提示 A. 如果猜的数字比真实数字大,提示你猜的数据大了 B. 如果猜的数字比真实数字小,提示你猜的数据小了 C. 如果猜的数字与真实数字相等,提示恭喜你猜中了 + +``` +import java.util.Random; +import java.util.Scanner; + +public class D1 { + public static void main(String[] args) { + Random xx = new Random(); + Scanner sc = new Scanner(System.in); + int ls = xx.nextInt(100) + 1; + while (true) { + System.out.println("请输入你的数字"); + int wd = sc.nextInt(); + if (wd > ls) { + System.out.println("猜大了"); + } else if (wd < ls) { + System.out.println("猜小了"); + } else { + System.out.println("恭喜猜中"); + break; + } + } + System.out.println("感谢使用"); + } +} +``` +## 作业.2 + +```java +public class D3 { + public static void main(String[] args) { + int[] arr = {10, 20, 30, 40, 50}; + int da = arr[0]; + for (int i = 1; i< arr.length; i++){ + if (arr[i]>da){ + da=arr[i]; + } + } + System.out.println("最大为:"+da); + } +} +``` \ No newline at end of file -- Gitee From b8f78088aaa79f5bdfb3fdf1e643427aedc1992f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=9B=BD=E4=BA=AE?= <647597435@qq.com> Date: Mon, 12 Dec 2022 19:57:34 +0800 Subject: [PATCH 3/3] =?UTF-8?q?20221212=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20221212 \344\275\234\344\270\232.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "19 \345\224\220\345\233\275\344\272\256/20221212 \344\275\234\344\270\232.md" diff --git "a/19 \345\224\220\345\233\275\344\272\256/20221212 \344\275\234\344\270\232.md" "b/19 \345\224\220\345\233\275\344\272\256/20221212 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..592f8e1 --- /dev/null +++ "b/19 \345\224\220\345\233\275\344\272\256/20221212 \344\275\234\344\270\232.md" @@ -0,0 +1,36 @@ +作业 +1. 闽大举行选美比赛,男生参赛,10个女生当评委,定义一个数组,存放评分,用随机数模拟打分的过程,用程序算出选手的最后得分【去掉一个最高分,再去掉一个最低分,用剩下的分数求平均值】, + +import java.util.Random; +import java.util.Scanner; + +public class D2 { + public static void main(String[] args) { + //定义 + Random r=new Random(); + int[] sc=new int[10]; + int su=0; + //遍历数组 + for (int i =0; i< sc.length; i++){ + //随机生成0-100 + sc[i]= r.nextInt(101); + //打印生成的分数 + System.out.println(sc[i]+"\t"); + } + //定义最大最小值 + int zg= sc[0]; + int zx= sc[0]; + //找出最大最小值 + for (int i=0; i< sc.length; i++){ + su +=sc[i]; + if (sc[i]>zg){ + zg=sc[i]; + } else if (sc[i]