当前位置:   article > 正文

java温习——2024年5月2日_2024学java学8还是21

2024学java学8还是21

1.java下载

        作用:编程语言,下载了才可以进行编程

        其实项目学习基本java8够用了,用最新的稳定版java21也可以。

        下载地址

        Java Downloads | Oracle

 

网盘自取: 

链接: https://pan.baidu.com/s/1qx8Fpi8rKnA0lYAvnW60kA?pwd=w9kr 提取码: w9kr 复制这段内容后打开百度网盘手机App,操作更方便哦


       配置环境变量   

       变量值因路径而不同。

       原因:可以在任意目录下使用java javac 命令

JAVA_HOME

        

%JAVA_HOME%\bin


 验证是否安装成功

cmd窗口打开

 


 2.idea下载

作用:方便进行java编程的工具

安装idea前提是java环境需要配置好,否则打不开idea

官网下载:Other Versions - IntelliJ IDEA

 记得勾选


 全局配置:  (一定是首页面进行的配置才是全局配置)

1.编码配置UTF-8

2.


idea使用: 

链接:https://pan.baidu.com/s/1UleEXSaZQgLap8G5yukhnQ?pwd=envy 
提取码:envy

idea打不开的情况

解决办法:(如果你是学生的话就不要安装什么dll修复工具,很多都是收费的)

可能是电脑的MICROSOFT VISUAL C++ 2019 RUNTIME 损坏了 需要修复

由于我这里丢失了 所以没有  需要下载微软修复软件进行修复

链接:https://pan.baidu.com/s/16KZzWvV7PO9bxfOYuLQnkw?pwd=gayt 
提取码:gayt

然后就可以找到有问题的组件进行修复就可以了,根据自己的问题选择

2.1 idea的配置 

3. javaSE基础学习



3.1 git使用


可以将自己写的代码放到网上,将写的代码放上去,然后还可以进行修改,保存。别人也可以看到。这样你写的代码就都可以在上面看到了

1.下载git       Git - 下载软件包 (git-scm.com)

2.双击安装,我习惯安装到空间足够大的盘符 

3.配置git

3.1鼠标右键进入open git bash here

输入命令

  1. # 配置用户名
  2. git config --global user.name "huang" //(输入你自己的名字)
  3. # 配置邮箱
  4. git config --global user.email "qq号码@qq.com" //(注册账号时用的邮箱,输入你自己的)

3.2配置ssh免密登录,就是每次登录不需要输入密码进行认证了

  1. # ssh免密登录
  2. ssh-keygen -t rsa -C "(qq号码)@qq.com" // 输入你自己的qq号码
  3. # 然后连续三次回车。 用户目录下会有 id_rsa 和 id_rsa.pub
  4. # 查看密钥
  5. cat ~/.ssh/id_rsa.pub
  6. # 然后登录进入 gitee,在设置里面找到 SSH KEY 将id_rsa.pub 文件的密钥内容粘贴进去
  7. # 使用 ssh -T git@gitee.com 测试是否成功即可 注意第二个回车需要输入yes后在回车
  8. ssh -T git@gitee.com

4.然后我们去gitee去初始化一个仓库,用于存放我们开发的代码

5.通过idea克隆到本地,idea配置git

6.克隆gitee仓库


3.2 idea的HelloWorld

这部分较好理解,所以精简

打开idea,然后创建工程,配置idea的jdk环境

首先创建工程和模块这些需要注意的是:创建顺序

  1. project工程(app)
  2. module模块(具体功能)
  3. package包(分类)
  4. class类(代码编写)

1.在idea中以及有一个空的工程learn-javase,至于这个工程具体是做什么的可以后面在这个工程里创建对应的模块

至于命名规则:  非强制性要求,可以随便命名,只不过这样命名容易理解

工程project表示做的这个工程是干什么的,比如学习javaSE的就可以这样命名:learn-javase

2.创建模块 在idea中file新建module   命名为helloworld


3.建包  在src中  命名为com.域名.功能名

比如,com.huang.helloworld

4.在包package里面建类  HelloWorld ,后面不需要加.java后缀了,因为idea自动加了。至于首字母为什么大写,规定。大驼峰命名法,并且类名要和文件名一致。  

完整代码

  1. public class HelloWorld {
  2. public static void main(String[] args) {
  3. // 这是打印语句,会将双引号里面的内容在控制台打印输出
  4. System.out.println("HelloWorld!");
  5. }
  6. }

右键在此代码页面然后运行即可

5. 提交到git

5.1 设置忽略不需要的提交的文件.gitignore

 提交之前需要忽略的内容如下

  1. # 设置一些不需要提交的文件
  2. # **/ 表示父目录的任意目录下的名字为mvnw文件不会上传
  3. **/mvnw
  4. **/mvnw.cmd
  5. **/.gitignore
  6. **/.mvn
  7. **/target/
  8. .idea

        

这样gitee上就有了你写的所有代码了


3.3注释


  1. /**
  2. 学习目的:学习使用注释
  3. 1.什么是注释
  4. 写在程序中对程序进行解释说明的文字
  5. 2.java程序中书写注释的方式有几种,各有什么不同
  6. 单行注释://
  7. 多行注释:*/ /* */ /*
  8. 文档注释:*/ /** */ /*
  9. 3.注释有什么特点
  10. 不影响程序的执行,编译后的class文件中已经没有注释了
  11. 4.注释的快捷键是
  12. ctrl+/ 单行注释(对当前行进行注释)
  13. ctrl+shift+/ 多行注释(会将选中的内容进行注释)
  14. */
  15. public class annotation_01 {
  16. //这是一个main程序,是程序的主入口
  17. public static void main(String[] args){
  18. //输出语句,会将引号里面的内容在控制台打印输出
  19. System.out.println("学习注释的使用");
  20. //这是单行注释
  21. /*
  22. 这是多行注释
  23. */
  24. /**
  25. 这是文档注释
  26. */
  27. }
  28. }

3.4字面量


  1. /**
  2. 学习目标:
  3. 1.字面量是什么
  4. 数据在程序中的书写格式
  5. 2.字符、字符串在程序中的书写格式有什么要求
  6. 字符必须用单引号围起来,有且只有一个字符
  7. 字符串必须用双引号围起来
  8. 3.几个常见的特殊值的书写格式是
  9. true false null /n /t
  10. */
  11. public class Literals_01 {
  12. public static void main(String[] args){
  13. //整数
  14. System.out.println(1);
  15. System.out.println(-1);
  16. //小数
  17. System.out.println(0.11);
  18. //字符
  19. System.out.println(' ');
  20. System.out.println('a');
  21. System.out.println('1');
  22. //补全当前字符串长度至8的整数倍,根据当前字符串长度补1~8个空格
  23. System.out.println('\t'+"石");
  24. //换行
  25. System.out.println('\n');
  26. System.out.println('荒');
  27. //字符串
  28. System.out.println("");
  29. System.out.println("你");
  30. System.out.println("你好呀");
  31. //布尔值
  32. System.out.println(true);
  33. System.out.println(false);
  34. //null
  35. String s = null;
  36. System.out.println(s);
  37. }
  38. }

 *关键字和标识符

  1. /**
  2. 学习目标:
  3. 什么是关键字
  4. 关键字就是java自己要用到的词,并且有特殊含义的一些词,我们就不能用来作为:类名,变量名,否则就会报错
  5. 什么是标识符
  6. 标识符就是名字
  7. 标识符的规则:由字母、数字、下划线、美元符等组成,且不能以数字开头,不能用关键字作为名字
  8. */
  9. public class KeywordAndIdentifierDemo {
  10. public static void main(String args[]){
  11. //关键字 就是java中自己要用到的词,并且有特殊含义的一些词
  12. //我们就不能作为:类名、变量名,否则会报错
  13. //int class = 10;
  14. int Class = 10;
  15. //标识符 就是名字,由字母、数字、下划线、美元符等组成,且不能以数字开头,
  16. //也不能用关键字作为名字
  17. // int 2a = 10;
  18. int _1 = 1;
  19. int $a = 3;
  20. // int static = 2;
  21. }
  22. }

3.5变量

1.变量的定义,用处、特点

  1. /**
  2. 学习目标:
  3. 1.变量是什么,完整的定义格式是什么样的
  4. 变量是用来存储一个数据的,本质是内存中的一块区域
  5. 数据类型 变量名称 = 数据
  6. 2.变量有什么用
  7. 可以用来记录要处理的数据,编写代码更灵活,管理代码更方便
  8. 3.变量有什么特点,基于这个特点有什么应用场景
  9. 变量里装的数据是可以被替换的。
  10. 比如:微信钱包里的余额,统计班级人数,统计公交车上下人数
  11. */
  12. public class VariableDemo1 {
  13. public static void main(String[] args) {
  14. //定义一个变量记录一个数据
  15. int a = 22;
  16. double b = 32.3;
  17. System.out.println(a);
  18. System.out.println(b);
  19. }
  20. }

 

 2.变量使用时的注意事项

  1. /**
  2. 学习目标:
  3. 使用变量时有哪些注意点
  4. 变量要先声明,才能使用
  5. 什么类型的变量,只能存储什么类型的数据
  6. 变量存在访问范围,同一个范围内,多个变量的名字不能一样
  7. 变量定义时可以不给赋初始值,但在使用时,变量里必须要有值
  8. */
  9. public class VariableDemo2 {
  10. public static void main(String args[]){
  11. //变量要先声明才能使用
  12. //System.out.println(a);
  13. int a = 20;
  14. System.out.println(a);
  15. //变量是什么类型,存储的数据就是什么类型
  16. /* int b;
  17. b = 20.3;*/
  18. int b;
  19. b = 10;
  20. //变量存在访问范围,同一个范围内,多个变量的名字不能一样
  21. //int a;
  22. {
  23. int c = 10;
  24. System.out.println(c);
  25. }
  26. int c = 10;
  27. System.out.println(c);
  28. //变量定义时可以不用赋初始值,但在使用时,变量里必须要有值
  29. int d;
  30. //System.out.println(d);
  31. d = 12;
  32. System.out.println(d);
  33. }
  34. }

3.6数据类型

1.ASCALL码

  1. /**
  2. 字符数据在计算机中是怎么存储的
  3. 字符存的是ASCAII码表中对应的数字的二进制形式
  4. 2进制、8进制、16进制的学习
  5. */
  6. public class VariableDemo3_ASCAII {
  7. public static void main(String args[]){
  8. //'a'-> 97
  9. System.out.println('a' + 0);
  10. //'A'-> 65
  11. System.out.println('A' + 0);
  12. //'0'-> 48
  13. System.out.println('0' + 0);
  14. //2进制 以0b或0B开头
  15. int a1 = 0b01100001;
  16. System.out.println(a1);
  17. //8进制 以0开头
  18. int a2 = 0141;
  19. System.out.println(a2);
  20. //16进制 以0X或0x开头
  21. int a3 = 0xFA;
  22. System.out.println(a3);
  23. }
  24. }

 

2.基本数据类型

  1. /**
  2. 学习目标:
  3. 掌握基本数据类型
  4. 四大类八种
  5. 整型 byte short int long
  6. 浮点型 float double
  7. 字符型 char
  8. 布尔型 boolean
  9. */
  10. public class VariableDemo3_DataType {
  11. public static void main(String[] args) {
  12. //byte
  13. // byte b = 129;//超出范围
  14. byte b = 127;
  15. //short
  16. short s = 12333;
  17. //int
  18. int i = 2234;
  19. //long 任意数字默认是整数,在后面加个L/l就是long类型
  20. long L = 23999999999L;
  21. //浮点型 小数后要加f
  22. float f = 23.33f;
  23. double d = 34.22;
  24. //布尔型boolean
  25. boolean b1 = true;
  26. boolean b2 = false;
  27. //字符型
  28. char c1 = '你';
  29. char c2 = '他';
  30. //引用数据类型
  31. String str = "石昊";
  32. System.out.println(str);
  33. }
  34. }

3. 类型转换

  1. /**
  2. 学习目标:
  3. 为什么要进行类型转换
  4. 存在不同类型的变量赋值给其他类型的变量
  5. 什么是自动类型转换
  6. 类型范围小的变量可以直接赋值给类型范围大的变量
  7. */
  8. public class VariableDemo5_TypeConversion {
  9. public static void main(String[] args) {
  10. byte c = 23;
  11. int a = c;
  12. System.out.println(c);
  13. System.out.println(a);
  14. }
  15. }

1. 表达式中的类型转换
  1. /**
  2. 学习目标:
  3. 什么是表达式的自动类型转换
  4. 小范围的类型会自动转换成大范围的类型进行运算
  5. 表达式的最终结果类型是由谁决定的
  6. 最终类型是由表达式中的最高类型决定的
  7. 表达式中有哪些类型转换是需要注意的
  8. byte short char 是直接转换成int类型参与运算的
  9. */
  10. public class VariableDemo6_TypeConversion {
  11. public static void main(String[] args) {
  12. //小范围的类型会自动转换为大范围的类型进行运算
  13. byte a = 10;
  14. int b = 20;
  15. int rs1 = a + b;
  16. System.out.println(rs1);
  17. System.out.println("-------------------------------");
  18. //最终结果类型是由表达式中的最高类型决定的
  19. long d = 30;
  20. long rs2 = a + b + d;
  21. System.out.println(rs2);
  22. System.out.println("-------------------------------");
  23. //byte short char 是直接转换成int类型参与运算的
  24. byte a1 = 10;
  25. short a2 = 20;
  26. char a3 = 'a';
  27. int rs3 = a1 + a2 + a3;
  28. System.out.println(rs3);
  29. }
  30. }

2.强制类型转换
  1. /**
  2. 学习目标:
  3. 什么是强制类型转换
  4. 默认情况下,范围大的类型变量直接赋值给范围小的类型变量
  5. 可以强行将范围大的类型变量赋值给类型范围小的变量
  6. 强制类型转换中有什么注意的地方
  7. 可能出现数据丢失
  8. 小数强制类型转换成整数是直接截断小数保留整数
  9. */
  10. public class VariableDemo7_TypeConversion {
  11. public static void main(String[] args) {
  12. //范围大的类型变量赋值给范围小的
  13. int a = 20;
  14. byte b = (byte) a;
  15. System.out.println(b);//20
  16. System.out.println("-------------------------------");
  17. //可能出现数据丢失
  18. int c1 = 200;
  19. byte c2 = (byte) c1;
  20. System.out.println(c2);//-56
  21. System.out.println("-------------------------------");
  22. //小数强制转换成整数是直接截断小数保留整数
  23. double d = 30.22;
  24. int i = (int) d;
  25. System.out.println(i);//30
  26. }
  27. }

 3.7运算符

1.算术运算符

  1. /**
  2. 学习目标:
  3. 算术运算符有哪些
  4. + - * / %
  5. /需要注意什么,为什么
  6. 如果两个整数做除法,其结果一定是整数,因为最高类型是整数
  7. +除了作基本数学运算,还有哪些功能
  8. 与字符串作+运算时会被当成连接符,其结果还是字符串
  9. 技巧:能算则算,不能算就在一起
  10. */
  11. public class OperatorDemo1 {
  12. public static void main(String[] args) {
  13. int a = 5;
  14. int b = 2;
  15. System.out.println(a + b);//7
  16. System.out.println(a - b);//3
  17. System.out.println(a * b);//10
  18. System.out.println(a / b);//2
  19. //两个整数做除法时,其结果一定是整数,因为最高类型是整数
  20. System.out.println(1.0 * a / b);//2.5 如果非要是小数 ,则可以乘以1.0
  21. System.out.println(5.0 / 2);//2.5 可以用5.0
  22. System.out.println(a % b);//1
  23. System.out.println("----------------------------------");
  24. System.out.println("荒" + 'a');//荒a
  25. int a2 = 3;
  26. System.out.println("荒" + a2);//荒3
  27. System.out.println(a2 + 'a' + "荒");//100荒
  28. System.out.println("荒" + 'a' + a2);//荒a3
  29. }
  30. }

2.自增自减

  1. /**
  2. 学习目标:
  3. 自增、自减运算符是什么,有什么作用,需要注意什么
  4. ++ -- 对当前变量+1 -1;
  5. 只能操作变量,不能操作字面量
  6. 自增、自减运算符放在变量前后有什么区别吗
  7. 如果单独使用放前放后是没有区别的
  8. 非单独使用时,放在前面,先进行变量自增/自减,再使用变量
  9. 放在后面,先使用变量,在进行变量自增/自减
  10. */
  11. public class OperatorDemo2 {
  12. public static void main(String[] args) {
  13. //++
  14. int a = 2;
  15. a++;
  16. ++a;
  17. System.out.println(a);//4
  18. //--
  19. int b = 2;
  20. b--;
  21. --b;
  22. System.out.println(b);//0
  23. //只能操作变量,不能操作字面量,否则会报错
  24. // System.out.println(2++);
  25. //非单独使用时
  26. //放在变量前,先自增/自减,在使用变量
  27. int a1 = 2;
  28. int b1 = ++a1;
  29. System.out.println(b1);//3
  30. //放在变量后,先使用变量,在自增/自减
  31. int i = 2;
  32. int j = i++;
  33. System.out.println(j);//2
  34. }
  35. }

3.赋值运算符

  1. /**
  2. 学习目标:
  3. 赋值运算符有哪些
  4. 基本赋值运算符:= (从右边往左边看)
  5. 扩展赋值运算符:+= -= *= /= %=
  6. 2.扩展赋值运算符的作用是什么,有什么特点
  7. +=可以实现数据的累加,把别人的数据加给自己
  8. 特点是扩展赋值运算符自带强制类型转换
  9. */
  10. public class OperatorDemo3 {
  11. public static void main(String[] args) {
  12. //=
  13. int a = 2;
  14. System.out.println(a);
  15. //+=
  16. byte b = 5;
  17. byte c = 5;
  18. //b = (byte) (b + c);
  19. //b += c;//等价于 b = (byte) (b + c);
  20. //b -= c;//等价于 b = (byte) (b - c);
  21. //b *= c;//等价于 b = (byte) (b * c);
  22. //b /= c;//等价于 b = (byte) (b / c);
  23. b %= c; //等价于 b = (byte) (b % c);
  24. System.out.println(b);
  25. }
  26. }

 4.逻辑运算符

  1. /**
  2. 学习目标:
  3. 1.逻辑运算符有哪些,有什么特点
  4. &:有一个为false,结果是false
  5. &&:有一个为false,结果是false,但前一个为false,后一个条件不执行
  6. |:有一个为true,结果是true
  7. ||:有一个为true,结果是true,但前一个为true,后一个条件不执行
  8. !:!false = true; !true = false;
  9. ^:相同是false,不同是true
  10. 实际开发中,常用的逻辑运算符还是:&& || !
  11. */
  12. public class OperatorDemo4 {
  13. public static void main(String[] args) {
  14. //&:有一个为false,结果是false
  15. int a = 3;
  16. int b = 4;
  17. //System.out.println(a > b & ++a > 2);
  18. //System.out.println(a);
  19. // &&:有一个为false,结果是false,但前一个为false,后一个条件不执行
  20. System.out.println(a > b && ++a > 2);
  21. System.out.println(a);
  22. // |:有一个为true,结果是true
  23. //System.out.println(a < b | ++a > 2);
  24. //System.out.println(a);
  25. // ||:有一个为true,结果是true,但前一个为true,后一个条件不执行
  26. System.out.println(a < b || ++a > 2);
  27. System.out.println(a);
  28. // !:!false = true; !true = false;
  29. System.out.println(!false);
  30. System.out.println(!true);
  31. // ^:相同是false,不同是true
  32. System.out.println(false ^ false);
  33. System.out.println(true ^ true);
  34. System.out.println(true ^ false);
  35. System.out.println(false ^ true);
  36. }
  37. }

3.8第一个api的使用——Scanner 键盘录入

  1. /**
  2. 学习目标:
  3. 什么是api,什么是api文档
  4. api是application programming interface,应用程序编程接口,就是java已经写好的程序,可以直接用
  5. api文档是java提供的程序使用说明书
  6. 2.java程序中如何实现接收用户键盘录入的数据
  7. 使用java提供的Scanner来完成,步骤如下:
  8. 导包:import java.util.Scanner
  9. 抄代码得到扫描器对象:Scanner sc = new Scanner(System.in);
  10. 抄代码等待接收用户输入的数据:int age = sc.nextInt();
  11. String name = sc.next();
  12. */
  13. public class Api_01_Scanner {
  14. public static void main(String[] args) {
  15. //java程序中如何实现接收用户键盘录入的数据
  16. // 使用java提供的Scanner来完成,步骤如下:
  17. // 导包:import java.util.Scanner
  18. // 抄代码得到扫描器对象:Scanner sc = new Scanner(System.in);
  19. Scanner sc = new Scanner(System.in);
  20. // 抄代码等待接收用户输入的数据:int age = sc.nextInt();
  21. System.out.println("请输入你的年龄:");
  22. int age = sc.nextInt();
  23. System.out.println("你的年龄是:" + age);
  24. // String name = sc.next();
  25. System.out.println("请输入你的姓名:");
  26. String name = sc.next();
  27. System.out.println(name + ",欢迎你");
  28. }
  29. }

3.9流程控制

1.顺序结构

2.分支结构

  1. /**
  2. 学习目标:
  3. if分支是什么,有哪些应用场景
  4. if分支是可以根据条件,选择执行某段程序 应用场景:发红包 绩效评级
  5. 2.if分支的写法有几种,各有什么特点
  6. if(条件表达式){
  7. 语句体;
  8. }
  9. if(条件表达式){
  10. 语句体1;
  11. } else{
  12. 语句体2;
  13. }
  14. if(条件表达式1){
  15. 语句体1;
  16. }else if(条件表达式2){
  17. 语句体2;
  18. }else if(条件表达式3){
  19. 语句体3;
  20. }
  21. ...
  22. else {
  23. 语句体n+1;
  24. }
  25. */
  26. public class IfDemo1 {
  27. public static void main(String[] args) {
  28. //1.测量体温
  29. double bodyTemperature = 38.3;
  30. if(bodyTemperature >= 37){
  31. System.out.println("发烧了");
  32. }
  33. //2.发红包
  34. double money = 99.2;
  35. if(money > 80){
  36. System.out.println("发送成功");
  37. }else{
  38. System.out.println("余额不足^^");
  39. }
  40. //3.绩效评级
  41. int score = 83;
  42. if(score >= 0 && score < 60){
  43. System.out.println("评级为:D");
  44. }else if(score >= 60 && score < 80){
  45. System.out.println("评级为:C");
  46. }else if(score >= 80 && score < 90){
  47. System.out.println("评级为:B");
  48. }else if(score >= 90 && score <= 100){
  49. System.out.println("评级为:A");
  50. }else {
  51. System.out.println("输入的分数有误^^");
  52. }
  53. }
  54. }

  1. /**
  2. 学习目标:
  3. 掌握switch分支的格式、执行流程
  4. 是通过比较值来决定执行那条分支
  5. 执行流程:
  6. 1.先执行表达式的值,在拿着这个值去与case后的值进行比较
  7. 2.与那个case后的值匹配为true就执行那个case块的代码,遇到break就跳出switch分支
  8. 3.如果全部case后的值与之匹配都是false,则执行default块的代码
  9. */
  10. public class SwitchDemo1 {
  11. public static void main(String[] args) {
  12. String week = "周四";
  13. switch (week){
  14. case "周一" :
  15. System.out.println("全职法师");
  16. break;
  17. case "周二" :
  18. System.out.println("狐妖小红娘");
  19. break;
  20. case "周三" :
  21. System.out.println("镖人");
  22. break;
  23. case "周四" :
  24. System.out.println("不良人");
  25. break;
  26. case "周五" :
  27. System.out.println("完美世界");
  28. break;
  29. case "周六" :
  30. System.out.println("斗罗大陆");
  31. break;
  32. case "周日" :
  33. System.out.println("斗破苍穹");
  34. break;
  35. default:
  36. System.out.println("输入错误");
  37. break;
  38. }
  39. }
  40. }
  1. /**
  2. 学习目标:
  3. 使用switch的注意点
  4. */
  5. public class SwitchDemo32 {
  6. public static void main(String[] args) {
  7. //表达式类型只能是byte short int char jdk5开始支持枚举,jdk7开始支持String,
  8. //不支持float double long
  9. //case给出的值不允许重复,且只能是字面量,不能是变量
  10. //正常使用switch的时候,不要忘记写break,否则会出现穿透现象
  11. //case穿透 丢失break导致
  12. String week = "周四";
  13. switch (week){
  14. case "周一" :
  15. System.out.println("全职法师");
  16. break;
  17. case "周二" :
  18. System.out.println("狐妖小红娘");
  19. break;
  20. case "周三" :
  21. System.out.println("镖人");
  22. break;
  23. case "周四" :
  24. System.out.println("不良人");
  25. //break;
  26. case "周五" :
  27. System.out.println("完美世界");
  28. break;
  29. case "周六" :
  30. System.out.println("斗罗大陆");
  31. break;
  32. case "周日" :
  33. System.out.println("斗破苍穹");
  34. break;
  35. default:
  36. System.out.println("输入错误");
  37. break;
  38. }
  39. }
  40. }

  1. /**
  2. 学习目标:
  3. switch穿透性能解决什么问题
  4. */
  5. public class SwitchDemo3 {
  6. public static void main(String[] args) {
  7. //存在多个case分支的代码是一样时,可以把代码写到以一个case块
  8. //其他case块通过穿透性能穿透到该case块即可,这样可以简化代码
  9. String week = "周一";
  10. switch (week){
  11. case "周一" :
  12. case "周二" :
  13. case "周三" :
  14. case "周四" :
  15. System.out.println("全职法师");
  16. break;
  17. case "周五" :
  18. System.out.println("完美世界");
  19. break;
  20. case "周六" :
  21. System.out.println("斗罗大陆");
  22. break;
  23. case "周日" :
  24. System.out.println("斗破苍穹");
  25. break;
  26. default:
  27. System.out.println("输入错误");
  28. break;
  29. }
  30. }
  31. }

3.循环.

  1. /**
  2. 学习目标:
  3. 掌握for循环格式以及执行流程
  4. */
  5. public class ForDemo1 {
  6. public static void main(String[] args) {
  7. for (int i = 0; i < 3; i++){
  8. System.out.println("荒");
  9. }
  10. System.out.println("-----------------");
  11. for (int i = 1; i <= 10; i+=2){
  12. System.out.println("荒");
  13. }
  14. }
  15. }
  1. /**
  2. 学习目标:
  3. for循环的应用
  4. */
  5. public class ForDemo2 {
  6. public static void main(String[] args) {
  7. //输出1-100之间的数
  8. for (int i = 1; i <= 100; i++) {
  9. System.out.println(i);
  10. }
  11. System.out.println("--------------------");
  12. //计算1到100之间的和
  13. int sum = 0;
  14. for (int i = 1; i <= 100; i++) {
  15. sum = sum + i;
  16. }
  17. System.out.println("和为:" + sum);
  18. System.out.println("--------------------");
  19. //计算1到100之间的奇数和
  20. int sum1 = 0;
  21. for (int i = 1; i < 100; i += 2) {
  22. sum1 = sum1 + i;
  23. }
  24. System.out.println("奇数和为:" + sum1);
  25. System.out.println("--------------------");
  26. int sum2 = 0;
  27. for (int i = 1; i <= 100; i++) {
  28. if (i % 2 == 1) {
  29. sum2 = sum2 + i;
  30. }
  31. }
  32. System.out.println("奇数和为:" + sum2);
  33. }
  34. }

  1. /**
  2. 学习目标:
  3. while循环的格式,执行流程
  4. */
  5. public class WhileDemo3 {
  6. public static void main(String[] args) {
  7. int i = 1;
  8. while (i < 5){
  9. System.out.println("荒天帝");
  10. i++;
  11. }
  12. }
  13. }

  1. /**
  2. 目标:
  3. 学习while的使用场景
  4. */
  5. public class WhileDemo4 {
  6. public static void main(String[] args) {
  7. //珠穆朗玛峰高度为8848860mm,现在有一个纸张厚度为0.1mm,对纸张进行折叠
  8. //折叠多少次会超过它的高度
  9. double peakHeight = 8848860;
  10. double paperThickness = 0.1;
  11. int count = 0;
  12. while (paperThickness < peakHeight) {
  13. paperThickness = paperThickness * 2;
  14. count++;
  15. }
  16. System.out.println("纸张折叠次数为" + count);
  17. System.out.println("纸张此时厚度为" + paperThickness);
  18. }
  19. }
  1. /**
  2. 目标:
  3. 掌握dowhile循环的格式以及执行流程
  4. */
  5. public class DoWhileDemo5 {
  6. public static void main(String[] args) {
  7. int i = 0;
  8. do{
  9. System.out.println("荒天帝");
  10. i++;
  11. }while(i < 3);
  12. //for循环中,控制循环的变量只在循环中使用,while循环中控制
  13. //循环的变量在循环后还可以使用
  14. for (int j = 0; j < 3; j++) {
  15. System.out.println("荒天帝2");
  16. }
  17. //System.out.println(j);
  18. int k = 0;
  19. while (k < 3) {
  20. System.out.println("荒天帝3");
  21. k++;
  22. }
  23. System.out.println(k);
  24. }
  25. }

  1. /**
  2. 目标:
  3. 了解循环嵌套的格式以及执行流程
  4. */
  5. public class LoopNestingDemo6 {
  6. public static void main(String[] args) {
  7. for (int i = 0; i < 3; i++) {
  8. for (int j = 0; j < 4; j++) {
  9. System.out.println("荒天帝");
  10. }
  11. System.out.println("--------------------");
  12. }
  13. for (int i = 0; i < 4; i++) {
  14. for (int j = 0; j < 4; j++) {
  15. System.out.print("*");
  16. }
  17. System.out.println();
  18. }
  19. }
  20. }
  1. /**
  2. 目标:
  3. 了解死循环
  4. */
  5. public class EndlessLoopDemo7 {
  6. public static void main(String[] args) {
  7. /*for (; ;) {
  8. System.out.println("荒天帝");
  9. }*/
  10. // while (true) {
  11. // System.out.println("荒天帝");
  12. // }
  13. do {
  14. System.out.println("荒天帝");
  15. } while (true);
  16. }
  17. }

4.练习

  1. /**
  2. 目标:
  3. 生成随机数有几步
  4. 如何生成指定区间的随机数
  5. */
  6. public class Demo1_Random {
  7. public static void main(String[] args) {
  8. Random r = new Random();
  9. for (int i = 1; i <= 20; i++) {
  10. int number = r.nextInt(10);
  11. System.out.println(number);
  12. }
  13. System.out.println("------------------------");
  14. //生成9—20之间的随机数
  15. //9-20 -> -9 -> (0-11) + 9
  16. for (int i = 1; i <= 20; i++) {
  17. int number2 = r.nextInt(12) + 9;
  18. System.out.println(number2);
  19. }
  20. }
  21. }
  1. /**
  2. 目标:
  3. 学以致用,利用流程控制完成猜数字游戏
  4. */
  5. public class Demo2_Random {
  6. public static void main(String[] args) {
  7. //随机生成1-100之间的一个数字
  8. Random r = new Random();
  9. int luckNumber = r.nextInt(100) + 1;
  10. Scanner sc = new Scanner(System.in);
  11. //利用死循环猜数字
  12. while (true) {
  13. System.out.println("请输入你猜的数字:");
  14. int guessNumber = sc.nextInt();
  15. //利用分支结构判断猜的数字与随机生成的数字是否一样
  16. if (guessNumber == luckNumber) {
  17. System.out.println("恭喜你,猜对了~");
  18. break;
  19. } else if (guessNumber > luckNumber) {
  20. System.out.println("你猜的数字过大");
  21. } else {
  22. System.out.println("你猜的数字过小");
  23. }
  24. }
  25. }
  26. }

3.10数组

1.数组定义

  1. /**
  2. 目标:
  3. 掌握数组的定义
  4. */
  5. public class Demo1_Array {
  6. public static void main(String[] args) {
  7. int[] ages = new int[]{1, 2, 3, 4};
  8. int ages2[] = new int[]{1, 2, 3, 4};
  9. int[] ages3 = {1, 2, 3, 4};
  10. double[] scores = {88.2, 89.2, 99.3, 80};
  11. }
  12. }
  1. /**
  2. 目标:
  3. 掌握静态初始化数组,访问数组 修改数组
  4. */
  5. public class Demo2_Array {
  6. public static void main(String[] args) {
  7. int[] ages = {29, 20, 34, 30};
  8. System.out.println(ages[0]);
  9. System.out.println(ages[1]);
  10. System.out.println(ages[2]);
  11. System.out.println(ages[3]);
  12. ages[0] = 20;
  13. System.out.println(ages[0]);
  14. System.out.println(ages[1]);
  15. System.out.println(ages[2]);
  16. System.out.println(ages[3]);
  17. System.out.println(ages.length);
  18. }
  19. }
  1. /**
  2. * 目标:
  3. * 知道什么是遍历,会遍历
  4. */
  5. public class Demo3_Array {
  6. public static void main(String[] args) {
  7. int[] ages = {10, 20, 30, 40};
  8. for (int i = 0; i < ages.length; i++) {
  9. System.out.println(ages[i]);
  10. }
  11. }
  12. }
  1. /**
  2. * 目标:
  3. * 数组遍历-求和
  4. */
  5. public class Demo4_Array {
  6. public static void main(String[] args) {
  7. int[] arr = {10, 20, 30, 40};
  8. int sum = 0;
  9. for (int i = 0; i < arr.length; i++) {
  10. sum = sum + arr[i];
  11. }
  12. System.out.println("总和为:" + sum);
  13. }
  14. }
  1. /**
  2. * 目标:
  3. * 掌握动态初始化数组
  4. */
  5. public class Demo5_Array {
  6. public static void main(String[] args) {
  7. int[] ages = new int[3];
  8. for (int i = 0; i < ages.length; i++) {
  9. System.out.println(ages[i]);
  10. }
  11. ages[0] = 1;
  12. ages[1] = 1;
  13. ages[2] = 1;
  14. for (int i = 0; i < ages.length; i++) {
  15. System.out.println(ages[i]);
  16. }
  17. //默认值
  18. //byte short int char long默认值为0
  19. byte[] arr = new byte[10];
  20. System.out.println(arr[0]);
  21. System.out.println(arr[9]);
  22. System.out.println("------------------");
  23. short[] arr1 = new short[10];
  24. System.out.println(arr1[0]);
  25. System.out.println(arr1[9]);
  26. System.out.println("-------------------");
  27. char[] arr2 = new char[10];
  28. System.out.println((int) arr2[0]);
  29. System.out.println((int) arr2[9]);
  30. System.out.println("------------------");
  31. long[] arr3 = new long[10];
  32. System.out.println(arr3[0]);
  33. System.out.println(arr3[9]);
  34. System.out.println("------------------");
  35. //float double类型的默认值为0.0
  36. float[] arr4 = new float[10];
  37. System.out.println(arr4[0]);
  38. System.out.println(arr4[9]);
  39. System.out.println("------------------");
  40. double[] arr5 = new double[10];
  41. System.out.println(arr5[0]);
  42. System.out.println(arr5[9]);
  43. System.out.println("------------------");
  44. //String类型为null
  45. String[] arr6 = new String[10];
  46. System.out.println(arr6[0]);
  47. System.out.println(arr6[9]);
  48. System.out.println("------------------");
  49. }
  50. }
  1. /**
  2. * 目标:
  3. * 完成评委打分案例
  4. */
  5. public class Demo6_Array {
  6. public static void main(String[] args) {
  7. //定义一个动态数组用于后面存储6个评委的打分
  8. double[] scores = new double[6];
  9. Scanner sc = new Scanner(System.in);
  10. //遍历数组,将评委的分数存储到数组
  11. for (int i = 0; i < scores.length; i++) {
  12. System.out.println("请输入第" + ( i + 1 ) + "个评委的打分:");
  13. double score = sc.nextDouble();
  14. scores[i] = score;
  15. }
  16. //定义变量用于求和
  17. double sum = 0;
  18. //遍历数组求和
  19. for (int i = 0; i < scores.length; i++) {
  20. sum = sum + scores[i];
  21. }
  22. //求平均分
  23. sum = sum / scores.length;
  24. System.out.println("最终分数为:" + sum);
  25. }
  26. }

2.数组内存

  1. /**
  2. * 目标:
  3. * 知道变量和数组在计算机中的执行过程,java程序在计算机中的执行过程
  4. */
  5. public class Demo1_Array {
  6. public static void main(String[] args) {
  7. //变量里的数据是在栈内存里存放的
  8. int a = 10;
  9. System.out.println(a);
  10. // arr在栈内存中 arr变量存放的是数组地址值
  11. // 数组元素在堆内存中
  12. int[] arr = {20, 39, 40, 48};
  13. System.out.println(arr);
  14. System.out.println(arr[0]);
  15. arr[0] = 1;
  16. }
  17. }
  1. /**
  2. * 目标:
  3. * 知道多个数组变量指向同一个数组对象的原因
  4. */
  5. public class Demo2_Array {
  6. public static void main(String[] args) {
  7. int[] arr = {20, 10, 30, 40};
  8. int[] arr2 = arr;
  9. for (int i = 0; i < arr.length; i++) {
  10. System.out.println(arr[i]);
  11. }
  12. System.out.println("------------------");
  13. for (int i = 0; i < arr2.length; i++) {
  14. System.out.println(arr2[i]);
  15. }
  16. arr2[1] = 1;
  17. System.out.println(arr[1]);
  18. }
  19. }

3.练习

  1. /**
  2. * 目标:
  3. * 求数组中的最大值
  4. */
  5. public class Demo1 {
  6. public static void main(String[] args) {
  7. //把数据拿到程序中,用数组装起来
  8. int[] arr = {1, 3, 6, 9, 4, 1, 8, 2, 5, 7};
  9. //定义变量用来记录最大值,初始值默认存储的是数组中的第一个元素作为参照
  10. int max = arr[0];
  11. //从第二个元素开始遍历
  12. for (int i = 1; i < arr.length; i++) {
  13. //如果当前元素大于变量max存储的数据,则替换变量max存储的值为该元素
  14. if (arr[i] > max) {
  15. max = arr[i];
  16. }
  17. }
  18. System.out.println("最大值为:" + max);
  19. }
  20. }
  1. /**
  2. * 目标:
  3. * 数组反转
  4. */
  5. public class Demo2 {
  6. public static void main(String[] args) {
  7. //定义数组存储数据
  8. int[] arr = {1, 2, 3, 4, 5};
  9. //通过遍历数组来交换
  10. //定义i指向前一个位置,j指向后一个位置,然后两两交换值,i<j时才交换
  11. for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
  12. //定义临时变量,记录后一个位置的值
  13. int temp = arr[j];
  14. //把前一个位置的值赋值给后一个位置
  15. arr[j] = arr[i];
  16. //把临时变量记录的后一个位置的值赋值给前一个位置
  17. arr[i] = temp;
  18. }
  19. //遍历数组
  20. for (int i = 0; i < arr.length; i++) {
  21. System.out.print(arr[i] + " ");
  22. }
  23. }
  24. }
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. /**
  4. * 目标:
  5. * 实现数组打乱
  6. */
  7. public class Demo3 {
  8. public static void main(String[] args) {
  9. //定义动态数组存放数据
  10. int[] arr = new int[5];
  11. //遍历数组记录数据
  12. Scanner sc = new Scanner(System.in);
  13. for (int i = 0; i < arr.length; i++) {
  14. System.out.println("请输入第" + (i + 1) + "个数据");
  15. int num = sc.nextInt();
  16. arr[i] = num;
  17. }
  18. //遍历数组打乱数据
  19. Random r = new Random();
  20. for (int i = 0; i < arr.length; i++) {
  21. //定义随机索引 用来和当前元素交换值
  22. int index = r.nextInt(arr.length);
  23. //定义临时变量记录随机索引的值
  24. int temp = arr[index];
  25. //将当前元素的值赋值给随机索引处的元素
  26. arr[index] = arr[i];
  27. //将temp中记录的随机索引的元素的值赋值给当前元素
  28. arr[i] = temp;
  29. }
  30. //遍历数组即可
  31. for (int i = 0; i < arr.length; i++) {
  32. System.out.print(arr[i] + " ");
  33. }
  34. }
  35. }

3.11方法

1.方法定义

修饰符: 方法可以有访问修饰符,例如 public、private、protected 或默认(包内可见)。
返回类型: 方法可以返回一个值,指定返回值的数据类型,如果方法不返回任何值,可以使用 void。
方法名: 方法名是方法的标识符,用于在程序中调用方法。
参数列表: 方法可以接受零个或多个参数,参数用于向方法传递数据。
方法体: 方法体包含实际执行的代码块,实现方法的功能。

  1. /**
  2. * 目标:
  3. * 学习方法的使用
  4. 方法(Method):一组执行特定任务的代码块。它可以在类中定义一次,然后在本方法、其他方法中被多次调用。
  5. 作用:提高代码的可读性和可维护性。
  6. */
  7. public class Demo1_Method {
  8. public static void main(String[] args) {
  9. int rs = sum(10, 20);
  10. System.out.println("和是:" + rs);
  11. }
  12. public static int sum(int a, int b){
  13. int c = a + b;
  14. return c;
  15. }
  16. }
  1. /**
  2. * 目标:
  3. * 根据需求设计合适的方法
  4. */
  5. public class Demo2_Method {
  6. public static void main(String[] args) {
  7. print(3);
  8. System.out.println("------------");
  9. print(4);
  10. }
  11. public static void print(int n){
  12. for (int i = 1; i <= n ; i++) {
  13. System.out.println("荒天帝");
  14. }
  15. }
  16. }
  1. /**
  2. * 目标:
  3. * 知道方法使用时的常见问题
  4. */
  5. public class Demo3_Method {
  6. public static int sum(int a, int b){
  7. int c = a + b;
  8. //如果方法的返回值类型写了具体类型,方法内部则必须使用return返回对应类型的数据
  9. return c;
  10. //return下面的语句,不能编写代码,属于无效的代码,执行不到
  11. //System.out.println("荒天帝");
  12. }
  13. public static void main(String[] args) {
  14. //但一个方法不能定义在另一个方法里面
  15. /* public static void print(){
  16. System.out.println("荒天帝");
  17. }*/
  18. //方法不调用就不会执行,调用方法时传给方法的数据,必须严格匹配方法的参数情况
  19. int sum = sum(10, 20);
  20. //调用有返回值的方法,有三种方式,1.可以定义变量接收结果
  21. int sum1 = sum(28,38);
  22. //2.直接输出调用
  23. System.out.println(sum(10,38));
  24. //3.直接调用
  25. sum(12,38);
  26. //调用无返回值的方法,只有一种方式:直接调用
  27. print();
  28. }
  29. //方法在类中的位置放前放后无所谓
  30. public static void print(){
  31. System.out.println("荒天帝");
  32. //方法的返回值类型写void(无返回声明时),方法内不能使用return返回数据,
  33. //return 3;
  34. }
  35. }

  1. /**
  2. * 目标:
  3. * 方法案例
  4. */
  5. public class Demo4_Method {
  6. public static void main(String[] args) {
  7. System.out.println(sum(100));
  8. judge(21);
  9. judge(10);
  10. }
  11. public static int sum(int n){
  12. int sum = 0;
  13. for (int i = 1; i <= n ; i++) {
  14. sum += i;
  15. }
  16. return sum;
  17. }
  18. public static void judge(int num){
  19. if (num % 2 == 0) {
  20. System.out.println(num + "是偶数");
  21. } else {
  22. System.out.println(num + "是奇数");
  23. }
  24. }
  25. }

2.方法形参

  1. /**
  2. * 目标:
  3. * 知道java的参数传递机制
  4. */
  5. public class Demo1_Method {
  6. public static void main(String[] args) {
  7. int a = 30;
  8. change(a);
  9. System.out.println("mian:" + a);
  10. }
  11. public static void change(int a) {
  12. System.out.println("方法1:" + a);
  13. a = 29;
  14. System.out.println("方法2:" + a);
  15. }
  16. }
  1. /**
  2. * 目标
  3. * 引用数据类型的参数传递
  4. */
  5. public class Demo2_Method {
  6. public static void main(String[] args) {
  7. int[] arr = {1, 2, 3};
  8. change(arr);
  9. System.out.println("main方法:" + arr[1]);
  10. }
  11. public static void change(int[] arr) {
  12. System.out.println("方法1:" + arr[1]);
  13. arr[1] = 100;
  14. System.out.println("方法2:" + arr[1]);
  15. }
  16. }

  1. /**
  2. * 目标:
  3. * 打印数组内容
  4. */
  5. public class Demo3_Method {
  6. public static void main(String[] args) {
  7. int[] arr = {1, 2, 3, 4, 5};
  8. printArray(arr);
  9. int[] arr2 = null;
  10. printArray(arr2);
  11. int[] arr3 = {};
  12. printArray(arr3);
  13. }
  14. public static void printArray(int[] arr) {
  15. //判断数组是否合法 ,如果为null直接打印null
  16. if (arr == null) {
  17. System.out.println("null");
  18. return;
  19. }
  20. System.out.print("[");
  21. for (int i = 0; i < arr.length; i++) {
  22. // if (i == arr.length - 1) {
  23. // System.out.print(arr[i]);
  24. // } else {
  25. // System.out.print(arr[i] + ", ");
  26. // }
  27. System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
  28. }
  29. System.out.println("]");
  30. }
  31. }
  1. /**
  2. * 目标:
  3. * 判断两个数组里面的内容是否一样
  4. */
  5. public class Demo4_Method {
  6. public static void main(String[] args) {
  7. int[] arr1 = {1, 2, 3};
  8. int[] arr2 = {1, 2, 3};
  9. System.out.println(equals(arr1, arr2));
  10. }
  11. public static boolean equals(int[] arr1, int[] arr2) {
  12. //如果两个都为null
  13. if (arr1 == null && arr2 == null) {
  14. return true;
  15. }
  16. //其中有一个为null
  17. if (arr1 == null || arr2 == null) {
  18. return false;
  19. }
  20. //长度不一样不相等
  21. if (arr1.length != arr2.length) {
  22. return false;
  23. }
  24. //元素内容不一样不相等
  25. for (int i = 0; i < arr1.length; i++) {
  26. if (arr1[i] != arr2[i]) {
  27. return false;
  28. }
  29. }
  30. //其他情况相等
  31. return true;
  32. }
  33. }

3.方法重载

  1. /**
  2. * 目标:
  3. * 知道方法重载是什么
  4. */
  5. public class Demo1_Overload {
  6. public static void main(String[] args) {
  7. test();
  8. test(1.1);
  9. }
  10. //方法重载只跟方法和形参列表相关
  11. //方法名相同,形参列表不同:形参个数、类型、顺序不同
  12. public static void test(){
  13. System.out.println("1");
  14. }
  15. public static void test(double a){
  16. System.out.println(a);
  17. }
  18. int test(int a){
  19. return a;
  20. }
  21. void test(int a, double b){
  22. System.out.println(a + b);
  23. }
  24. void test(double b, int a){
  25. System.out.println(a + b);
  26. }
  27. }
  1. /**
  2. * 目标:
  3. * 方法重载练习
  4. */
  5. public class Demo2_Overload {
  6. public static void main(String[] args) {
  7. fire();
  8. fire("米国");
  9. fire("米国",999);
  10. }
  11. public static void fire(){
  12. //System.out.println("向岛国发送了一枚武器");
  13. fire("岛国");
  14. }
  15. public static void fire(String country){
  16. //System.out.println("向" + country + "发送了一枚武器");
  17. fire(country,1);
  18. }
  19. public static void fire(String country, int num){
  20. System.out.println("向" + country + "发送了" + num + "枚武器");
  21. }
  22. }

4.return

  1. /**
  2. * 目标:
  3. * 知道无返回值的方法中,怎么跳出并结束当前所在的方法
  4. */
  5. public class Demo1_Return {
  6. public static void main(String[] args) {
  7. division(2,0);
  8. }
  9. public static void division(int a, int b){
  10. if(b == 0){
  11. System.out.println("除数不能为0");
  12. return;//跳出并结束当前所在方法的执行
  13. }
  14. int c = a / b;
  15. System.out.println(c);
  16. }
  17. }

3.12案例练习

1.购买飞机票

  1. /**
  2. * 目标:
  3. * 完成购买飞机票案例
  4. */
  5. public class Demo1 {
  6. public static void main(String[] args) {
  7. double price = calculate(1000, 8, "头等舱");
  8. System.out.println(price);
  9. }
  10. public static double calculate(double price, int month, String seat){
  11. if(month >= 5 && month <= 10){
  12. //旺季
  13. switch (seat){
  14. case "头等舱":
  15. price *= 0.9;
  16. break;
  17. case "经济舱":
  18. price *= 0.85;
  19. break;
  20. }
  21. }else {
  22. //淡季
  23. switch (seat){
  24. case "头等舱":
  25. price *= 0.7;
  26. break;
  27. case "经济舱":
  28. price *= 0.65;
  29. break;
  30. }
  31. }
  32. return price;
  33. }
  34. }

2.生成随机验证码

  1. /**
  2. * 目标:
  3. * 完成生成随机验证码的案例
  4. */
  5. public class Demo2 {
  6. public static void main(String[] args) {
  7. System.out.println(createCode(4));
  8. }
  9. public static String createCode(int n){
  10. //for循环 生成几位随机数
  11. Random r = new Random();
  12. String code = "";
  13. for (int i = 1; i <= n; i++) {
  14. //随机0:数字 1:大写字母 2:小写字母
  15. int num = r.nextInt(3);
  16. switch (num){
  17. case 0:
  18. code += r.nextInt(10);
  19. break;
  20. case 1:
  21. //大写字母 65 65+25 0-25
  22. char c = (char) (r.nextInt(26) + 65);
  23. code += c;
  24. break;
  25. case 2:
  26. //小写字母 97 97+25 0-25
  27. char c1 = (char) (r.nextInt(26) + 97);
  28. code += c1;
  29. break;
  30. }
  31. }
  32. return code;
  33. }
  34. }

3.评委打分

  1. /**
  2. * 目标
  3. * 完成评委打分案例
  4. */
  5. public class Demo3 {
  6. public static void main(String[] args) {
  7. System.out.println(getScore(6));
  8. }
  9. public static double getScore(int number){
  10. //定义动态数组存放评委分数
  11. int[] scores = new int[number];
  12. //for循环遍历 录入评委分数
  13. Scanner sc = new Scanner(System.in);
  14. for (int i = 0; i < scores.length; i++) {
  15. System.out.println("请输入第" + (i + 1) + "个评委的分数:");
  16. int score = sc.nextInt();
  17. scores[i] = score;
  18. }
  19. //记录总和
  20. int sum = 0;
  21. //记录最大值
  22. int max = scores[0];
  23. //记录最小值
  24. int min = scores[0];
  25. //遍历求值
  26. for (int i = 0; i < scores.length; i++) {
  27. int score = scores[i];
  28. //总和
  29. sum += score;
  30. //最大值
  31. if(score > max){
  32. max = score;
  33. }
  34. //最小值
  35. if(score < min){
  36. min = score;
  37. }
  38. }
  39. return 1.0 * (sum - max - min) / (number - 2);
  40. }
  41. }

4.数字加密

  1. /**
  2. * 目标
  3. * 完成数字加密案例
  4. */
  5. public class Demo4 {
  6. public static void main(String[] args) {
  7. System.out.println(encrypt(8346));
  8. }
  9. public static String encrypt(int number){
  10. //拆分数字
  11. int[] numbers = split(number);
  12. //遍历数组进行加密
  13. for (int i = 0; i < numbers.length; i++) {
  14. numbers[i] = (numbers[i] + 5) % 10;
  15. }
  16. //数组反转
  17. reverse(numbers);
  18. //拼接数字
  19. String encrypt = "";
  20. for (int i = 0; i < numbers.length; i++) {
  21. encrypt += numbers[i];
  22. }
  23. return encrypt;
  24. }
  25. public static void reverse(int[] numbers) {
  26. for (int i = 0, j = numbers.length - 1; i < j; i++, j--) {
  27. //定义临时变量存放后一个位置的值
  28. int temp = numbers[j];
  29. //将前一个位置的值赋值给后一个位置
  30. numbers[j] = numbers[i];
  31. //将temp中记录的后一个位置的值赋值给前一个位置
  32. numbers[i] = temp;
  33. }
  34. }
  35. public static int[] split(int number) {
  36. //定义动态初始化数组存放拆分的数字
  37. int[] numbers = new int[4];
  38. numbers[0] = number / 1000;
  39. numbers[1] = (number / 100) % 10;
  40. numbers[2] = (number / 10) % 10;
  41. numbers[3] = number % 10;
  42. return numbers;
  43. }
  44. }

5.数组拷贝

  1. /**
  2. * 目标:
  3. * 完成数组拷贝
  4. */
  5. public class Demo5 {
  6. public static void main(String[] args) {
  7. int[] arr = {11, 22, 33};
  8. int[] copy = copy(arr);
  9. print(copy);
  10. }
  11. public static void print(int[] copy) {
  12. System.out.print("[");
  13. for (int i = 0; i < copy.length; i++) {
  14. System.out.print(i == copy.length - 1 ? copy[i] : copy[i] + ", ");
  15. }
  16. System.out.println("]");
  17. }
  18. public static int[] copy(int[] arr) {
  19. //定义新数组
  20. int[] arr1 = new int[arr.length];
  21. //将原数组的值赋值给新数组
  22. for (int i = 0; i < arr.length; i++) {
  23. arr1[i] = arr[i];
  24. }
  25. return arr1;
  26. }
  27. }

6.抢红包

  1. /**
  2. * 目标
  3. * 抢红包
  4. */
  5. public class Demo6 {
  6. public static void main(String[] args) {
  7. int[] moneys = {20, 30, 10, 999, 666};
  8. money1(moneys);
  9. }
  10. public static void money(int[] moneys) {
  11. //遍历数组
  12. Scanner sc = new Scanner(System.in);
  13. Random r = new Random();
  14. for (int i = 0; i < moneys.length; i++) {
  15. System.out.println("请输入随机内容进行抽奖~");
  16. sc.next();
  17. //进行抽奖
  18. //获得一个随机索引,如果不为0则中奖,然后将这个索引对应的
  19. //元素值改为0
  20. while (true) {
  21. int index = r.nextInt(moneys.length);
  22. if (moneys[index] != 0) {
  23. System.out.println("恭喜你抽中了" + moneys[index] + "红包");
  24. moneys[index] = 0;
  25. break;
  26. }
  27. }
  28. }
  29. }
  30. public static void money1(int[] moneys) {
  31. //遍历数组 将数组中的红包进行打乱
  32. //将当前元素与随机索引位置的元素进行交换
  33. Scanner sc = new Scanner(System.in);
  34. Random r = new Random();
  35. for (int i = 0; i < moneys.length; i++) {
  36. int index = r.nextInt(moneys.length);
  37. //定义临时变量记录随机索引处的值
  38. int temp = moneys[index];
  39. //将当前元素的值赋值给随机索引对应的元素
  40. moneys[index] = moneys[i];
  41. //将temp里面存储的随机索引对应的元素值赋值给当前元素
  42. moneys[i] = temp;
  43. }
  44. //进行抽奖
  45. for (int i = 0; i < moneys.length; i++) {
  46. System.out.println("请输入随机内容进行抽奖~");
  47. sc.next();
  48. System.out.println("恭喜你抽中了" + moneys[i] + "红包");
  49. }
  50. }
  51. }

7.找素数

  1. /**
  2. * 目标:
  3. * 找素数
  4. */
  5. public class Demo7 {
  6. public static void main(String[] args) {
  7. System.out.println("共有" + search2(100, 200));
  8. int count = 0;
  9. for (int i = 100; i <= 200; i++) {
  10. if(check(i)){
  11. count++;
  12. System.out.println(i);
  13. }
  14. }
  15. System.out.println("共有" + count);
  16. }
  17. public static int search1(int start, int end) {
  18. int count = 0;
  19. for (int i = start; i <= end; i++) {
  20. //信号位
  21. boolean flag = true;
  22. //从2遍历到该数据的一半进行判断
  23. for (int j = 2; j <= i / 2; j++) {
  24. if (i % j == 0) {
  25. flag = false;
  26. break;
  27. }
  28. }
  29. //根据flag判断是否为素数
  30. if (flag) {
  31. count++;
  32. System.out.println(i);
  33. }
  34. }
  35. return count;
  36. }
  37. public static int search2(int start, int end) {
  38. int count = 0;
  39. out:
  40. for (int i = start; i <= end; i++) {
  41. //从2遍历到该数据的一半进行判断
  42. for (int j = 2; j <= i / 2; j++) {
  43. if (i % j == 0) {
  44. continue out;
  45. }
  46. }
  47. //为素数
  48. count++;
  49. System.out.println(i);
  50. }
  51. return count;
  52. }
  53. public static boolean check(int data) {
  54. //从2遍历到该数据的一半进行判断
  55. for (int j = 2; j <= data / 2; j++) {
  56. if (data % j == 0) {
  57. return false;
  58. }
  59. }
  60. //为素数
  61. return true;
  62. }
  63. }

8.打印乘法表

  1. /**
  2. * 打印乘法表 三角形
  3. */
  4. public class Demo8 {
  5. public static void main(String[] args) {
  6. for (int i = 1; i <= 9; i++) {
  7. for (int j = 1; j <= i ; j++) {
  8. System.out.print(j + "X" + i + "=" + (j * i) + " ");
  9. }
  10. System.out.println();
  11. }
  12. // 行数 个数 空格数
  13. // * 1 1 2
  14. // *** 2 3 1
  15. //***** 3 5 0
  16. int n = 10;
  17. for (int i = 1; i <= n; i++) {
  18. //打印空格数
  19. for (int j = 1; j <= n - i; j++) {
  20. System.out.print(" ");
  21. }
  22. //打印*
  23. for (int j = 1; j <= 2*i - 1; j++) {
  24. System.out.print(j % 2 == 0 ? " " : "*");
  25. }
  26. System.out.println();
  27. }
  28. }
  29. }

9.双色球

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. /**
  4. * 目标:
  5. * 实现双色球
  6. */
  7. public class Demo9 {
  8. public static void main(String[] args) {
  9. int[] userNumbers = userSelectNumbers();
  10. System.out.println("您投注的号码为:");
  11. print(userNumbers);
  12. int[] luckyNumbers = createNumbers();
  13. System.out.println("中奖号码为:");
  14. print(luckyNumbers);
  15. judge(userNumbers,luckyNumbers);
  16. }
  17. //打印数组
  18. public static void print(int[] numbers) {
  19. System.out.print("[");
  20. for (int i = 0; i < numbers.length; i++) {
  21. System.out.print(i == numbers.length - 1 ? numbers[i] : numbers[i] + ", ");
  22. }
  23. System.out.println("]");
  24. }
  25. //用户选号
  26. public static int[] userSelectNumbers() {
  27. //定义动态初始化数组 6个红色球号码,1个蓝色号码
  28. int[] numbers = new int[7];
  29. Scanner sc = new Scanner(System.in);
  30. for (int i = 0; i < numbers.length - 1; i++) {
  31. //用户输入6个蓝色号码
  32. while (true) {
  33. System.out.println("请您输入第" + (i + 1) + "个蓝色号码");
  34. int number = sc.nextInt();
  35. //判断输入的号码是否合法 1-33 不重复
  36. if (number < 1 || number > 33) {
  37. System.out.println("对不起,您输入的号码不在范围内,请重新输入");
  38. } else {
  39. //重复
  40. if (exist(numbers, number)) {
  41. System.out.println("对不起,您输入的号码重复,请重新输入");
  42. } else {
  43. //合法
  44. numbers[i] = number;
  45. //跳出循环
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. //生成蓝色号码 1-16
  52. while (true) {
  53. System.out.println("请输入最后一个蓝色号码");
  54. int number = sc.nextInt();
  55. if (number < 1 || number > 16) {
  56. System.out.println("对不起,您输入的蓝色号码不在范围内,请重新输入");
  57. } else {
  58. //合法
  59. numbers[6] = number;
  60. //结束循环
  61. break;
  62. }
  63. }
  64. return numbers;
  65. }
  66. //判断号码是否重复
  67. public static boolean exist(int[] numbers, int number) {
  68. //遍历数组 如果遍历到0,说明号码已经比对完,没有重复,直接返回false
  69. // [1,2,3,4,0,0] 5
  70. for (int i = 0; i < numbers.length; i++) {
  71. if (numbers[i] == 0) {
  72. return false;
  73. }
  74. //相等则表示重复了
  75. if (numbers[i] == number) {
  76. return true;
  77. }
  78. }
  79. //如果循环结束都没有 找出重复号码表示没有重复
  80. return false;
  81. }
  82. //随机一组中奖号码
  83. public static int[] createNumbers() {
  84. //定义动态数组存放中将号码
  85. int[] numbers = new int[7];
  86. Random r = new Random();
  87. //6个红色号码
  88. for (int i = 0; i < numbers.length - 1; i++) {
  89. while (true) {
  90. //生成随机号码 1-33 -> (0-32) + 1
  91. int number = r.nextInt(33) + 1;
  92. //号码不重复
  93. if (!exist(numbers, number)) {
  94. numbers[i] = number;
  95. break;//将不重复的号码赋值给当前位置,结束死循环
  96. }
  97. }
  98. }
  99. //1个蓝色号码 1-16 ->(0-15)+1
  100. int number = r.nextInt(16) + 1;
  101. numbers[6] = number;
  102. return numbers;
  103. }
  104. //判断中奖情况
  105. public static void judge(int[] userNumbers, int[] luckyNumbers) {
  106. //记录红球、蓝球中奖数量
  107. int redCount = 0;
  108. int blueCount = 0;
  109. //比对红球中奖数量
  110. for (int i = 0; i < userNumbers.length - 1; i++) {
  111. for (int j = 0; j < luckyNumbers.length - 1; j++) {
  112. if (userNumbers[i] == luckyNumbers[i]) {
  113. redCount++;
  114. break;
  115. }
  116. }
  117. }
  118. //比对蓝球中奖数量
  119. blueCount = userNumbers[6] == luckyNumbers[6] ? 1 : 0;
  120. System.out.println("红球命中数量为:" + redCount);
  121. System.out.println("蓝球命中数量为:" + blueCount);
  122. //判断中奖情况
  123. if (redCount == 6 && blueCount == 1) {
  124. System.out.println("恭喜您,中奖1000万");
  125. } else if (redCount == 6 && blueCount == 0) {
  126. System.out.println("恭喜您,中奖500万");
  127. } else if (redCount == 5 && blueCount == 1) {
  128. System.out.println("恭喜您,中奖3000元");
  129. } else if (redCount == 5 && blueCount == 0 || redCount == 4 && blueCount == 1) {
  130. System.out.println("恭喜您,中奖200元");
  131. } else if (redCount == 4 && blueCount == 0 || redCount == 3 && blueCount == 1) {
  132. System.out.println("恭喜您,中奖10元");
  133. } else if (redCount < 3 && blueCount == 1) {
  134. System.out.println("恭喜您,中奖5元");
  135. } else {
  136. System.out.println("谢谢参与~~~");
  137. }
  138. }
  139. }

3.13oop学习

1.面向对象基础

  1. public class Student {
  2. String name;
  3. double chinese;
  4. double english;
  5. double score;
  6. public void pass(double score){
  7. if(this.score > score){
  8. System.out.println("恭喜您,考试通过");
  9. }else{
  10. System.out.println("不及格");
  11. }
  12. }
  13. public void totalScore() {
  14. System.out.println(name + "总成绩为:" + (chinese + english));
  15. }
  16. public void averageScore() {
  17. System.out.println(name + "平均成绩为:" + (chinese + english) / 2);
  18. }
  19. }
  20. //一个代码文件中,可以写多个class类,但只能有一个用public修饰,
  21. //且public修饰的类名必须成为代码文件名
  22. class Person {
  23. }
  24. /*public class Car {
  25. }*/
  1. /**
  2. * 目标:
  3. * 面向对象快速入门
  4. */
  5. public class Demo1_Object {
  6. public static void main(String[] args) {
  7. Student student1 = new Student();
  8. student1.name = "播妞";
  9. student1.chinese = 88;
  10. student1.english = 89;
  11. student1.totalScore();
  12. student1.averageScore();
  13. Student student2 = new Student();
  14. student2.name = "播仔";
  15. student2.chinese = 80;
  16. student2.english = 90;
  17. student2.totalScore();
  18. student2.averageScore();
  19. }
  20. }
  1. /**
  2. * 目标:
  3. * 掌握类和对象的注意点
  4. */
  5. public class Demo2_Object {
  6. public static void main(String[] args) {
  7. //成员变量存在默认值,不用赋初值
  8. Student s1 = new Student();
  9. System.out.println(s1.name);
  10. System.out.println(s1.english);
  11. //对象与对象之间的数据不会相互影响,但多个变量指向同一个对象的时候
  12. //就会相互影响了
  13. Student s2 = new Student();
  14. s1.name = "张三";
  15. s2.name = "李四";
  16. System.out.println(s1.name);
  17. System.out.println(s2.name);
  18. //如果某个对象没有一个变量引用它,则该对象无法被操作了,该对象
  19. //会成为垃圾对象
  20. s1 = null;
  21. //System.out.println(s1.name);
  22. }
  23. }
  1. /**
  2. * 目标
  3. * 了解this关键字 :用来拿到当前对象 那个对象调用方法,this就指向那个对象
  4. */
  5. public class Demo3 {
  6. public static void main(String[] args) {
  7. Student student = new Student();
  8. student.score = 90;
  9. student.pass(80);
  10. }
  11. }

2.构造器

1.构造器是什么

public 类名(){}

2.在哪里调用,常用他来干嘛

对象创建时,我们可以指定对象去调用那个构造器执行

构造器常用于完成对象初始化

3.构造器在使用时,有哪些注意点

类在设计时,如果不屑构造器,java会为类自动生成一个无参构造器

一旦定义了有参构造器,java就不会帮我们的类生成无参构造器了,此时就建议自己写一个无参构造器出来了

  1. public class Student {
  2. private String name;
  3. private int age;
  4. public void setAge(int age){
  5. if(age >= 0 && age <= 100){
  6. this.age = age;
  7. }else {
  8. System.out.println("您输入的年龄不合法");
  9. }
  10. }
  11. public int getAge(){
  12. return age;
  13. }
  14. public Student(){
  15. System.out.println("无参构造器执行了");
  16. }
  17. public Student(String name, int age){
  18. System.out.println("有参构造器执行了");
  19. this.name = name;
  20. this.age = age;
  21. }
  22. }
  1. public class Teacher {
  2. private String name;
  3. private int age;
  4. public Teacher() {
  5. }
  6. public Teacher(String name, int age) {
  7. this.name = name;
  8. this.age = age;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public int getAge() {
  17. return age;
  18. }
  19. public void setAge(int age) {
  20. this.age = age;
  21. }
  22. }

  1. public class TeacherOperator {
  2. Teacher teacher;
  3. public TeacherOperator(Teacher teacher) {
  4. this.teacher = teacher;
  5. }
  6. public void printAge(){
  7. System.out.println(teacher.getAge());
  8. }
  9. }

  1. /**
  2. * 目标
  3. * 掌握构造器以及封装
  4. */
  5. public class Demo1 {
  6. public static void main(String[] args) {
  7. //如果没有写构造器,java会为类自动生成一个无参构造器的
  8. Student student = new Student();
  9. /*student.name = "荒天帝";
  10. student.age = 18;*/
  11. Student s1 = new Student("张三", 20);
  12. /*System.out.println(s1.name);
  13. System.out.println(s1.age);*/
  14. Student s2 = new Student();
  15. s2.setAge(-23);
  16. System.out.println(s2.getAge());
  17. }
  18. }

3.封装

 1.什么是封装

就是用类设计对象处理某一个事物的数据时,应该把要处理的数据,以及处理这些数据的方法,设计到一个对象中去

2.封装的设计规范是什么样的

合理隐藏,合理暴露

3.代码层面如何控制对象的成员公开或隐藏

public 公开

private 隐藏

  1. /**
  2. * 目标
  3. * javabean学习
  4. */
  5. public class Demo2 {
  6. public static void main(String[] args) {
  7. Teacher t1 = new Teacher();
  8. t1.setAge(77);
  9. TeacherOperator to = new TeacherOperator(t1);
  10. to.printAge();
  11. }
  12. }
  1. public class Movie {
  2. private int id;
  3. private String name;
  4. private double prince;
  5. private double score;
  6. private String actor;
  7. private String director;
  8. private String info;
  9. public Movie() {
  10. }
  11. public Movie(int id, String name, double prince, double score, String actor, String director, String info) {
  12. this.id = id;
  13. this.name = name;
  14. this.prince = prince;
  15. this.score = score;
  16. this.actor = actor;
  17. this.director = director;
  18. this.info = info;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public double getPrince() {
  27. return prince;
  28. }
  29. public void setPrince(double prince) {
  30. this.prince = prince;
  31. }
  32. public double getScore() {
  33. return score;
  34. }
  35. public void setScore(double score) {
  36. this.score = score;
  37. }
  38. public String getActor() {
  39. return actor;
  40. }
  41. public void setActor(String actor) {
  42. this.actor = actor;
  43. }
  44. public String getDirector() {
  45. return director;
  46. }
  47. public void setDirector(String director) {
  48. this.director = director;
  49. }
  50. public String getInfo() {
  51. return info;
  52. }
  53. public void setInfo(String info) {
  54. this.info = info;
  55. }
  56. public int getId() {
  57. return id;
  58. }
  59. public void setId(int id) {
  60. this.id = id;
  61. }
  62. }

  1. public class MovieOperator {
  2. private Movie[] movies;
  3. public MovieOperator(Movie[] movies){
  4. this.movies = movies;
  5. }
  6. //打印全部电影信息
  7. public void printAllMovies(){
  8. for (int i = 0; i < movies.length; i++) {
  9. Movie m = movies[i];
  10. System.out.println("电影编号是:" + m.getId());
  11. System.out.println("电影名是:" + m.getName());
  12. System.out.println("电影价格是:" + m.getPrince());
  13. System.out.println("电影主演是:" + m.getActor());
  14. System.out.println("电影导演是:" + m.getDirector());
  15. System.out.println("电影评分是:" + m.getScore());
  16. System.out.println("电影其他信息是:" + m.getInfo());
  17. System.out.println("-------------------");
  18. }
  19. }
  20. //根据id查询电影信息
  21. public void selectMovieById(int id){
  22. for (int i = 0; i < movies.length; i++) {
  23. Movie m = movies[i];
  24. if(m.getId() == id){
  25. System.out.println("你查询的电影信息如下:");
  26. System.out.println("电影编号是:" + m.getId());
  27. System.out.println("电影名是:" + m.getName());
  28. System.out.println("电影价格是:" + m.getPrince());
  29. System.out.println("电影主演是:" + m.getActor());
  30. System.out.println("电影导演是:" + m.getDirector());
  31. System.out.println("电影评分是:" + m.getScore());
  32. System.out.println("电影其他信息是:" + m.getInfo());
  33. }
  34. }
  35. }
  36. }

  1. /**
  2. * 目标
  3. * 面向对象练习 展示电影信息
  4. */
  5. public class Demo3 {
  6. public static void main(String[] args) {
  7. Movie[] movies = new Movie[4];
  8. movies[0] = new Movie(1, "全职法师", 23, 10, "莫凡", "未知", "100人想看");
  9. movies[1] = new Movie(2, "完美世界", 23, 10, "莫凡", "未知", "100人想看");
  10. movies[2] = new Movie(3, "镖人", 23, 10, "莫凡", "未知", "100人想看");
  11. movies[3] = new Movie(4, "狐妖小红娘", 23, 10, "莫凡", "未知", "100人想看");
  12. //电影操作对象
  13. MovieOperator mo = new MovieOperator(movies);
  14. Scanner sc = new Scanner(System.in);
  15. while (true) {
  16. System.out.println("欢迎来到电影系统-----------------");
  17. System.out.println("请选择操作");
  18. System.out.println("1:查询所有电影信息");
  19. System.out.println("2:根据id查询电影信息");
  20. int select = sc.nextInt();
  21. switch (select) {
  22. case 1:
  23. mo.printAllMovies();
  24. break;
  25. case 2:
  26. System.out.println("请输入id:");
  27. int id = sc.nextInt();
  28. mo.selectMovieById(id);
  29. break;
  30. default:
  31. System.out.println("没有此选项");
  32. break;
  33. }
  34. }
  35. }
  36. }

4.成员变量

  1. /**
  2. * 目标
  3. * 区别局部变量和成员变量
  4. */
  5. public class A03_variable {
  6. public static void main(String[] args) {
  7. //类中的位置不同,成员变量在类中,方法外,局部变量常见于方法内
  8. int a = 10;
  9. //初始值不同:成员变量有默认值不需要初始化,局部变量没有默认值,使用前要赋值
  10. //内存位置不同:成员变量在堆内存中,局部变量在栈内存中
  11. //作用域不同:成员变量:整个对象,局部变量:在所属的大括号内
  12. //生命周期不同:成员变量:与对象共生死,局部变量:方法调用而生,方法结束而亡
  13. }
  14. }

3.14 api

1. String

  1. /* 目标
  2. * 常用api-String
  3. */
  4. public class Demo1_String {
  5. public static void main(String[] args) {
  6. String s = "荒天帝";
  7. System.out.println(s);
  8. String s1 = new String("荒天帝");
  9. System.out.println(s1);
  10. char[] chs = {'a', 'b', 'c'};
  11. byte[] bytes = {65,66,67};
  12. String s2 = new String(chs);
  13. System.out.println(s2);
  14. String s3 = new String(bytes);
  15. System.out.println(s3);
  16. }
  17. }
  1. /**
  2. * 目标
  3. * String提供的操作字符串数据的常用方法
  4. */
  5. public class Demo2_String {
  6. public static void main(String[] args) {
  7. //public int length();获取字符串的长度返回,就是字符个数
  8. String s = "今天,天气真好";
  9. System.out.println(s.length());
  10. //public char charAt(int index)获取某个索引位置处的字符返回
  11. String s1 = "今天,一定也要坚持到最后";
  12. System.out.println(s1.charAt(7));
  13. //public char[] toCharArray();将当前字符串转换成字符数组返回
  14. String s2 = "坚持,坚持,再坚持";
  15. char[] chars = s2.toCharArray();
  16. for (int i = 0; i < chars.length; i++) {
  17. System.out.println(chars[i]);
  18. }
  19. //public boolean equals(Object anObject)判断当前字符串与另外一个字符串
  20. //的内容是否一样,一样返回true
  21. String s3 = new String("荒天帝");;
  22. String s4 = new String("炎帝");
  23. String s5 = new String("炎帝");
  24. System.out.println(s4 == s5);
  25. System.out.println(s4.equals(s5));
  26. System.out.println(s3.equals(s4));
  27. //public boolean equalsIgnoreCase(String anotherString)
  28. //判断当前字符串与另外一个字符串的内容是否一样(忽略大小写)
  29. String s6 = "12abC";
  30. String s7 = "12Abc";
  31. System.out.println(s6.equals(s7));
  32. System.out.println(s6.equalsIgnoreCase(s7));
  33. //public String substring(int beginIndex,int endIndex)
  34. //根据开始和结束索引进行截取,得到新的字符串(包前不包后)
  35. String str1 = "我不能选择那最好的,是那最好的选择了我";
  36. String substring = str1.substring(10, 19);
  37. System.out.println(substring);
  38. //public String substring(int beginIndex)
  39. //从传入的索引处截取,截取到末尾,得到新的字符串返回
  40. String str2 = "人不是生来就给打败的,你可以毁灭我,但就是打不败我";
  41. String substring1 = str2.substring(18);
  42. System.out.println(substring1);
  43. //public String replace(CharSequence target,CharSequence replacement)
  44. //使用新值将字符串中的旧值替换,得到新的字符串
  45. String str3 = "你就是个垃圾,连垃圾都不如";
  46. String newStr3 = str3.replace("垃圾", "**");
  47. System.out.println(newStr3);
  48. //public boolean contains(CharSequence s)
  49. //判断字符串中是否包含了某个字符串
  50. String str4 = "要在这个世界上取得胜利,就必须贯彻始终:至逝世都不能撒手。";
  51. System.out.println(str4.contains("胜利"));
  52. //public boolean startsWith(String prefix)
  53. //判断字符串是否以某个字符串内容开头,开头返回true
  54. String str5 = "凡事都要有统一和决断,因此成功不站在自信的一方,而站在有计划的一方。";
  55. boolean flag = str5.startsWith("凡事");
  56. System.out.println(flag);
  57. //public String[] split(String regex)
  58. //把字符串按照某个字符串内容分割,并返回字符串数组回来
  59. String str6 = "如果说时间是最宝贵的东西,那么浪费时间就是最大的挥霍。";
  60. String[] split = str6.split(",");
  61. for (int i = 0; i < split.length; i++) {
  62. System.out.println(split[i]);
  63. }
  64. }
  65. }
1.1练习
  1. import java.util.Scanner;
  2. /**
  3. 目标:
  4. 完成用户登录
  5. 需求:系统的正确登录名好密码是:qingtian/123456,请在控制台开发一个登录界面
  6. 接收用户输入的登录名和密码,判断用户是否登录成功,登录成功后展示:“欢迎进入系统!”
  7. 即可停止程序,(注意:要求最多给用户三次登录机会)
  8. */
  9. public class Demo1 {
  10. public static void main(String[] args) {
  11. //获得一个扫描器用于获得用户键盘录入的数据
  12. Scanner sc = new Scanner(System.in);
  13. //4.使用循环控制登录界面最多显示3次
  14. for (int i = 0; i < 4; i++) {
  15. //1.开发登录界面提示用户通过键盘输入用户名和密码
  16. System.out.println("请输入用户名:");
  17. //等待接收用户输入的用户名
  18. String username = sc.next();
  19. System.out.println("请输入密码:");
  20. //等待接收用户输入的密码
  21. String password = sc.next();
  22. //3.根据登录方法返回的认证结果判断用户是否登录成功
  23. boolean rs = check(username, password);
  24. if (rs) {
  25. //登录成功
  26. System.out.println("欢迎进入系统!");
  27. break;//登录成功后结束for循环
  28. }else {
  29. //登录失败
  30. System.out.println("您输入的用户名或密码错误~~");
  31. }
  32. }
  33. }
  34. /**
  35. * 2.设计一个登录方法,对用户的登录名和密码进行正确性认证
  36. * @param username
  37. * @param password
  38. * @return
  39. */
  40. public static boolean check(String username, String password){
  41. //记录正确的用户名和密码
  42. String rightUsername = "qingtian";
  43. String rigthPassword = "123456";
  44. //判断用户输入的用户名和密码是否正确
  45. /* if(rightUsername.equals(username) && rigthPassword.equals(password)){
  46. return true;
  47. }
  48. return false;*/
  49. //简化代码
  50. return rightUsername.equals(username) && rigthPassword.equals(password);
  51. }
  52. }
  1. import java.util.Random;
  2. /**
  3. 目标
  4. 使用String开发验证码
  5. 需求:实现随机产生验证码,验证码的每位可能是数字、大写字母、小写字母
  6. */
  7. public class Demo2 {
  8. public static void main(String[] args) {
  9. //4.主程序中,调用该方法即可得到随机验证码了
  10. System.out.println(createCode(4));
  11. System.out.println(createCode(6));
  12. }
  13. /**
  14. * 1.设计一个方法,该方法接收一个整型变量,最终要返回对应位数的随机验证码
  15. * @param n
  16. * @return
  17. */
  18. public static String createCode(int n) {
  19. //2.方法内定义两个变量:一个用来记录生成的验证码,
  20. // 一个用来记住要用到的全部字符
  21. String code = "";
  22. String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  23. //3.定义for循环控制生成多少位随机字符,每次得到一个字符范围内的随机索引
  24. //根据索引提取该字符,把字符交给code变量连接起来,循环结束后
  25. //在循环外返回code即可
  26. //获得一个随机数对象,用于获取随机索引
  27. Random r = new Random();
  28. for (int i = 0; i < n; i++) {
  29. //调用Random里面的方法生成随机索引
  30. //参数包前不包后
  31. //调用String里面length()方法,获取字符串的长度返回(就是字符个数)
  32. int index = r.nextInt(data.length());
  33. //根据索引在data中提取该字符
  34. //调用String里面的charAt(int index)方法,获取某个索引位置处的字符返回
  35. //把字符交给code变量连接起来
  36. code += data.charAt(index);//等价于 code = code + data.charAt(index)
  37. }
  38. //将生成的随机验证码返回
  39. return code;
  40. }
  41. }

2.ArrayList 

  1. import java.util.ArrayList;
  2. /**
  3. 目标
  4. ArrayList提供的常用方法
  5. */
  6. public class Demo1 {
  7. public static void main(String[] args) {
  8. ArrayList<String> list = new ArrayList<>();
  9. //public boolean add(E e)
  10. //将指定的元素添加到此集合的末尾
  11. list.add("荒天帝石昊");
  12. list.add("纳兰家纳兰嫣然");
  13. list.add("萧家萧炎");
  14. //public void add(int index, E element)
  15. // 在此集合的指定位置插入指定的元素
  16. list.add(1,"萧家萧炎");
  17. System.out.println(list);
  18. //public E get(int index)
  19. //返回指定索引处的元素
  20. System.out.println(list.get(1));
  21. //public int size()
  22. //返回集合中元素的个数
  23. System.out.println(list.size());
  24. //public E remove(int index)
  25. // 删除指定索引处的元素,返回被删除的元素
  26. list.remove(0);
  27. System.out.println(list);
  28. //public boolean remove(Object o)
  29. // 删除指定的元素,返回删除是否成功
  30. System.out.println(list.remove("萧家萧炎"));
  31. System.out.println(list);
  32. //public E set(int index, E element)
  33. //修改指定索引处的元素,返回被修改的元素
  34. System.out.println(list.set(0, "云岚山云韵"));
  35. System.out.println(list);
  36. }
  37. }
  1. import java.util.ArrayList;
  2. /**
  3. 目标:
  4. 掌握从容器中找出某些数据并成功删除的技巧
  5. 需求:现在购物车中存储了如下商品:java入门,宁夏枸杞,黑枸杞
  6. 人字拖,特级枸杞,枸杞子。现在用户不想买枸杞了,选择了批量删除
  7. 请完成该需求
  8. */
  9. public class Demo2 {
  10. public static void main(String[] args) {
  11. //1.后台使用ArrayList集合表示购物车,存储这些商品名
  12. ArrayList<String> list = new ArrayList<>();
  13. list.add("java入门");
  14. list.add("宁夏枸杞");
  15. list.add("黑枸杞");
  16. list.add("人字拖");
  17. list.add("特级枸杞");
  18. list.add("枸杞子");
  19. //2.遍历集合中的每一个数据,只要这个数据包含了“枸杞”
  20. //就删除它
  21. /*//public int size():返回集合中元素的个数
  22. for (int i = 0; i < list.size(); i++) {
  23. //public E get(int index)
  24. //返回指定索引处的元素
  25. String e = list.get(i);
  26. //包含“枸杞” 就删除
  27. //public boolean contains(CharSequence s)
  28. //判断字符串中是否包含了某个字符串
  29. if (e.contains("枸杞")) {
  30. //public boolean remove(Object o)
  31. //删除指定的元素,返回删除是否成功
  32. list.remove(e);
  33. //索引值i-1
  34. i--;
  35. }
  36. }*/
  37. //倒着遍历
  38. for (int i = list.size() - 1; i >= 0; i--) {
  39. //public E get(int index)
  40. //返回指定索引处的元素
  41. String e = list.get(i);
  42. //包含“枸杞” 就删除
  43. //public boolean contains(CharSequence s)
  44. //判断字符串中是否包含了某个字符串
  45. if (e.contains("枸杞")) {
  46. //public boolean remove(Object o)
  47. //删除指定的元素,返回删除是否成功
  48. list.remove(e);
  49. }
  50. }
  51. //3.输出集合看是否已经成功删除了全部枸杞数据了
  52. System.out.println(list);
  53. }
  54. }
  1. /**
  2. * 目标:
  3. * 模仿外卖系统中的商家系统
  4. * 需求:完成菜品的上架以及菜品信息浏览功能
  5. */
  6. public class Demo3 {
  7. public static void main(String[] args) {
  8. FoodOperator fo = new FoodOperator();
  9. fo.start();
  10. }
  11. }
  1. /**
  2. * 菜品实体类
  3. * 用来封装数据
  4. */
  5. public class Food {
  6. private String name;
  7. private double price;
  8. private String desc;
  9. public Food() {
  10. }
  11. public Food(String name, double price, String desc) {
  12. this.name = name;
  13. this.price = price;
  14. this.desc = desc;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public double getPrice() {
  23. return price;
  24. }
  25. public void setPrice(double price) {
  26. this.price = price;
  27. }
  28. public String getDesc() {
  29. return desc;
  30. }
  31. public void setDesc(String desc) {
  32. this.desc = desc;
  33. }
  34. }
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. /**
  4. * 菜品操作实体类
  5. * 数据的业务处理
  6. */
  7. public class FoodOperator {
  8. //用于存放商品
  9. private ArrayList<Food> foodList = new ArrayList<>();
  10. //上架商品
  11. public void add(){
  12. //创建菜品对象 用来保存菜品数据
  13. Food f = new Food();
  14. Scanner sc = new Scanner(System.in);
  15. System.out.println("请输入菜品名字:");
  16. String name = sc.next();
  17. f.setName(name);
  18. System.out.println("请输入价格:");
  19. double price = sc.nextDouble();
  20. f.setPrice(price);
  21. System.out.println("请输入描述:");
  22. String desc = sc.next();
  23. f.setDesc(desc);
  24. //将菜品对象添加到集合中
  25. foodList.add(f);
  26. System.out.println("商品上架成功");
  27. }
  28. //展示菜品信息
  29. public void get(){
  30. if (foodList.size() == 0) {
  31. System.out.println("没有菜品,请先上架!!");
  32. return;
  33. }
  34. System.out.println("商品信息如下:");
  35. for (int i = 0; i < foodList.size(); i++) {
  36. Food f = foodList.get(i);
  37. System.out.println(f.getName());
  38. System.out.println(f.getPrice());
  39. System.out.println(f.getDesc());
  40. System.out.println("-------------");
  41. }
  42. }
  43. public void start(){
  44. Scanner sc = new Scanner(System.in);
  45. while (true) {
  46. System.out.println("欢迎来到系统~~");
  47. System.out.println("1.上架商品");
  48. System.out.println("2.展示商品");
  49. System.out.println("3.退出系统");
  50. System.out.println("请输入您的选择");
  51. String option = sc.next();
  52. switch (option) {
  53. case "1":
  54. add();
  55. break;
  56. case "2":
  57. get();
  58. break;
  59. case "3":
  60. return;
  61. default:
  62. System.out.println("没有此选项,请重新选择!");
  63. break;
  64. }
  65. }
  66. }
  67. }

3.案列

  1. public class Test {
  2. public static void main(String[] args) {
  3. ATM atm = new ATM();
  4. atm.start();
  5. }
  6. }

  1. public class Account {
  2. private String cardId;//卡号
  3. private String username;//用户名
  4. private char gender;
  5. private String password;
  6. private double money;//余额
  7. private double limit;//每次取现额度
  8. public String getCardId() {
  9. return cardId;
  10. }
  11. public void setCardId(String cardId) {
  12. this.cardId = cardId;
  13. }
  14. public String getUsername() {
  15. return username + (gender == '男' ? "先生" : "女士");
  16. }
  17. public void setUsername(String username) {
  18. this.username = username;
  19. }
  20. public char getGender() {
  21. return gender;
  22. }
  23. public void setGender(char gender) {
  24. this.gender = gender;
  25. }
  26. public String getPassword() {
  27. return password;
  28. }
  29. public void setPassword(String password) {
  30. this.password = password;
  31. }
  32. public double getMoney() {
  33. return money;
  34. }
  35. public void setMoney(double money) {
  36. this.money = money;
  37. }
  38. public double getLimit() {
  39. return limit;
  40. }
  41. public void setLimit(double limit) {
  42. this.limit = limit;
  43. }
  44. }
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. public class ATM {
  5. private ArrayList<Account> accounts = new ArrayList<>();
  6. private Scanner sc = new Scanner(System.in);
  7. private Account loginAcc;
  8. {
  9. Account acc = new Account();
  10. acc.setUsername("荒天帝");
  11. acc.setPassword("123456");
  12. acc.setGender('男');
  13. acc.setCardId("12345678");
  14. acc.setLimit(1000);
  15. accounts.add(acc);
  16. }
  17. public void start() {
  18. while (true) {
  19. System.out.println("=========欢迎来到ATM系统==========");
  20. System.out.println("1.账户登录");
  21. System.out.println("2.账户开户");
  22. System.out.println("请选择~~");
  23. int choose = sc.nextInt();
  24. switch (choose) {
  25. case 1:
  26. //登录
  27. login();
  28. break;
  29. case 2:
  30. //开户
  31. createAccount();
  32. break;
  33. default:
  34. System.out.println("没有此选项!");
  35. }
  36. }
  37. }
  38. /** 展示用户登录成功后的操作界面 */
  39. private void showUserCommand(){
  40. while (true) {
  41. System.out.println(loginAcc.getUsername() + "您可以进行以下操作:===========");
  42. System.out.println("1、查询账户");
  43. System.out.println("2、存款");
  44. System.out.println("3、取款");
  45. System.out.println("4、转账");
  46. System.out.println("5、修改密码");
  47. System.out.println("6、退出");
  48. System.out.println("7、注销当前账户");
  49. System.out.println("请输入您的选择");
  50. int command = sc.nextInt();
  51. switch(command){
  52. case 1:
  53. //查询账户
  54. showLoginAccount();
  55. break;
  56. case 2:
  57. //存款
  58. depositMoney();
  59. break;
  60. case 3:
  61. //取款
  62. withdrawMoney();
  63. break;
  64. case 4:
  65. //转账
  66. transferMoney();
  67. break;
  68. case 5:
  69. //修改密码
  70. updatePassword();
  71. return;
  72. case 6:
  73. //退出
  74. System.out.println(loginAcc.getUsername() + "您退出系统成功");
  75. return;
  76. case 7:
  77. //注销当前账户
  78. if (deletAccount()) {
  79. return;//返回欢迎界面
  80. }
  81. break;
  82. default:
  83. System.out.println("没有此选项,请确认~");
  84. }
  85. }
  86. }
  87. /** 修改密码操作 */
  88. private void updatePassword() {
  89. System.out.println("==修改密码操作==");
  90. while (true) {
  91. System.out.println("请输入当前密码:");
  92. String password = sc.next();
  93. //判断登录密码是否正确
  94. if(loginAcc.getPassword().equals(password)){
  95. while (true) {
  96. //正确则可以开始修改密码了
  97. //请输入新的密码
  98. System.out.println("请输入新的密码:");
  99. String newPassword = sc.next();
  100. System.out.println("请再次输入密码");
  101. String confirmPassword = sc.next();
  102. //判断两次输入的密码是否一致
  103. if(confirmPassword.equals(newPassword)){
  104. //一样就可以更新账户密码了
  105. loginAcc.setPassword(confirmPassword);
  106. System.out.println("恭喜您,修改密码成功~~");
  107. return;
  108. }else{
  109. //不一样
  110. System.out.println("您输入的两次密码不一致~~");
  111. }
  112. }
  113. }else{
  114. System.out.println("您输入的登录密码不正确~~");
  115. }
  116. }
  117. }
  118. private boolean deletAccount() {
  119. System.out.println("==销户操作==");
  120. //确认用户是否要销户
  121. System.out.println("您真的要销户吗?y/n");
  122. String command = sc.next();
  123. switch (command){
  124. case "y":
  125. //确认销户
  126. //判断当前账户是否还有余额
  127. if(loginAcc.getMoney() > 0){
  128. //还有余额则不能销户
  129. System.out.println("您当前的账户还有余额,不能销户~~");
  130. return false;
  131. }else{
  132. //表明可以销户了
  133. accounts.remove(loginAcc);
  134. System.out.println("您的账户销户成功~~");
  135. return true;
  136. }
  137. default:
  138. System.out.println("好的,您的账户保留");
  139. return false;
  140. }
  141. }
  142. /** 转账 */
  143. private void transferMoney() {
  144. System.out.println("==转账操作==");
  145. //判断当前系统是否有其他账户
  146. if(accounts.size() < 2){
  147. System.out.println("当前系统只有您一个账户,不能转账~~");
  148. return;
  149. }
  150. //判断自己账户是否有钱
  151. if(loginAcc.getMoney() == 0){
  152. System.out.println("当前余额为0,不能转账~");
  153. return;
  154. }
  155. while (true) {
  156. //输入转账卡号
  157. System.out.println("请输入对方卡号:");
  158. String cardId = sc.next();
  159. // 判断当前卡号在系统是否存在
  160. Account acc = getAccountByCardId(cardId);
  161. if(acc == null){
  162. System.out.println("您输入的卡号不存在");
  163. }else{
  164. while (true) {
  165. //说明卡号存在,请输入转账金额
  166. System.out.println("请输入转账金额");
  167. double money = sc.nextDouble();
  168. //判断转账金额是否超过账户金额
  169. if(loginAcc.getMoney() < money){
  170. System.out.println("转账失败,您的余额为:" + loginAcc.getMoney());
  171. }else{
  172. //说明可以进行转账,认证对方姓氏
  173. String name = "*" + acc.getUsername().substring(1);
  174. System.out.println("请输入【" + name + "】的姓氏");
  175. String preName = sc.next();
  176. //判断是否认证成功
  177. if(!acc.getUsername().startsWith(preName)){
  178. //认证失败
  179. System.out.println("认证失败");
  180. }else {
  181. //说明认证成功,开始转账
  182. loginAcc.setMoney(loginAcc.getMoney() - money);
  183. acc.setMoney(acc.getMoney() + money);
  184. System.out.println("转账成功~~");
  185. return;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /** 取款 */
  193. private void withdrawMoney() {
  194. System.out.println("==取款操作==");
  195. //先判断账户的余额是否大于等于100
  196. if(loginAcc.getMoney() < 100){
  197. //如果小于100,则表示余额不足
  198. System.out.println("余额不足,不能取款,您当前的余额是:" + loginAcc.getMoney());
  199. return;//跳出并结束当前取款操作
  200. }
  201. while (true) {
  202. //说明余额充足可以取款
  203. System.out.println("请输入您取款的数目:");
  204. double money = sc.nextDouble();
  205. //判断取款金额是否足够
  206. if(loginAcc.getMoney() < money){
  207. //不足够
  208. System.out.println("余额不足,您当前的余额为:" + loginAcc.getMoney());
  209. }else{
  210. //余额足够 判断是否取款金额是否超过取款限额
  211. if(loginAcc.getLimit() < money){
  212. System.out.println("您当前取款金额超过每次限额,您每次最多可取:" + loginAcc.getLimit());
  213. }else{
  214. //取款成功,更新当前的账户余额
  215. loginAcc.setMoney(loginAcc.getMoney() - money);
  216. System.out.println("恭喜您取款" + money + "成功,您当前的余额为:" + loginAcc.getMoney() );
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. /** 存款 */
  223. private void depositMoney() {
  224. System.out.println("==存款操作==");
  225. System.out.println("请输入您要存款的数目:");
  226. double money = sc.nextDouble();
  227. //存款成功,更新账户余额
  228. loginAcc.setMoney(loginAcc.getMoney() + money);
  229. System.out.println("恭喜您,存款:" + money + "成功,您当前的余额是:" + loginAcc.getMoney());
  230. }
  231. /** 查询账户 */
  232. private void showLoginAccount(){
  233. System.out.println(loginAcc.getUsername() + "您的账户信息如下:==============");
  234. System.out.println("用户名:" + loginAcc.getUsername());
  235. System.out.println("卡号:" + loginAcc.getCardId());
  236. System.out.println("性别:" + loginAcc.getGender());
  237. System.out.println("余额:" + loginAcc.getMoney());
  238. System.out.println("取现额度:" + loginAcc.getLimit());
  239. }
  240. /** 完成用户登录操作 */
  241. private void login(){
  242. System.out.println("======用户登录=======");
  243. //判断当前系统中是否存在账户
  244. if(accounts.size() == 0){
  245. //当前系统没有账户,直接结束登录操作
  246. System.out.println("当前系统还没有账户哟~,请先开户后,再来登录哟~");
  247. return;//跳出登录方法
  248. }
  249. while (true) {
  250. //如果当前系统存在账户,提示用户输入卡号
  251. System.out.println("请输入您的卡号:");
  252. String cardId = sc.next();
  253. //根据卡号去账户集合中查询账户对象
  254. Account acc = getAccountByCardId(cardId);
  255. //如果没有找到账户对象,说明登录卡号不存在,提示继续输入卡号
  256. if(acc == null){
  257. System.out.println("您输入的卡号不存在,请确认~");
  258. }else{
  259. while (true) {
  260. //如果找到了账户对象,说明卡号存在,继续输入密码
  261. System.out.println("请您输入密码:");
  262. String pwd = sc.next();
  263. //如果密码不正确,提示继续输入密码
  264. if(!acc.getPassword().equals(pwd)){
  265. System.out.println("您输入的密码不正确,请确认~");
  266. }else{
  267. loginAcc = acc;
  268. //如果密码正确,说明登录成功,并给出相应的提示
  269. System.out.println("欢迎" + acc.getUsername() + ",登录成功,您的卡号是:" + acc.getCardId());
  270. //......
  271. showUserCommand();
  272. return;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. private void createAccount() {
  279. //创建账户对象封装数据
  280. System.out.println("===========开户操作==============");
  281. Account account = new Account();
  282. System.out.println("请输入账户名:");
  283. String name = sc.next();
  284. account.setUsername(name);
  285. while (true) {
  286. System.out.println("请输入性别:");
  287. char gender = sc.next().charAt(0);
  288. if (gender == '男' || gender == '女') {
  289. account.setGender(gender);
  290. break;
  291. } else {
  292. System.out.println("性别只能是男或女");
  293. }
  294. }
  295. while (true) {
  296. System.out.println("请输入密码:");
  297. String password = sc.next();
  298. System.out.println("请输入确认密码:");
  299. String confirmPassword = sc.next();
  300. if (password.equals(confirmPassword)) {
  301. account.setPassword(confirmPassword);
  302. break;
  303. } else {
  304. System.out.println("两次输入的密码不一样");
  305. }
  306. }
  307. System.out.println("请输入每次取现限制额度");
  308. double limit = sc.nextDouble();
  309. account.setLimit(limit);
  310. //系统生成卡号
  311. String cardId = createCardId();
  312. account.setCardId(cardId);
  313. accounts.add(account);
  314. System.out.println("恭喜你," + account.getUsername() + "开户成功,您的卡号是:" + account.getCardId());
  315. }
  316. /**
  317. * 定义方法,用来生成不重复的卡号
  318. */
  319. private String createCardId() {
  320. Random r = new Random();
  321. while (true) {
  322. //定义变量记录卡号
  323. String cardId = "";
  324. //循环生成卡号
  325. for (int i = 0; i < 8; i++) {
  326. int number = r.nextInt(10);//随机生成0-9
  327. cardId += number;
  328. }
  329. //判断卡号是否重复
  330. if (getAccountByCardId(cardId) == null) {
  331. //没有重复
  332. return cardId;
  333. }
  334. }
  335. }
  336. /**
  337. * 定义方法根据id查询账户
  338. *
  339. * @param cardId
  340. * @return
  341. */
  342. private Account getAccountByCardId(String cardId) {
  343. //遍历账户集合
  344. for (int i = 0; i < accounts.size(); i++) {
  345. Account acc = accounts.get(i);
  346. //根据id判断是否查询到账户
  347. if (acc.getCardId().equals(cardId)) {
  348. //id相等,查询成功
  349. return acc;
  350. }
  351. }
  352. //查无此账户
  353. return null;
  354. }
  355. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/803028
推荐阅读
相关标签
  

闽ICP备14008679号