回顾
-
-
术语
jdk: 写代码 使用jdk A.java jre: java程序想要运行 提供环境 jvm: 真正的运行实在虚拟机执行了 class文件
-
-
-
程序的运行流程
1. 编写源代码 java 2. 编译java文件 javac A.java javac.exe A.class 3. 运行class文件 java A java.exe
-
-
-
环境变量配置
1. path----> bin目录的路径 exe 2. 新建JAVA_HOME ----> jdk的根目录 path-----> %JAVA_HOME%\bin
-
-
-
数据类型
-
-
引用数据类型 String class interface 数组 ....
-
基本数据类型----> 四类八种 1.整数类型 byte short int long 2.浮点型 float double
-
字符类型 char ''
-
布尔类型 boolean true false
-
- 5. 变量 ```java 1. 全局变量 [访问权限修饰符] [普通修饰符] 数据类型 变量名 [= 数据]; 2. 局部变量 1. [普通修饰符] 数据类型 变量名 = 数据; //初始化局部变量 2. [普通修饰符] 数据类型 变量名; //声明一个局部变量 变量名 = 数据;//赋值
-
-
运算符
-
赋值运算符 = int num = 100; += -= /= *= %= 算术运算符 + - * / % ++ -- + 比较运算符 > < >= <= == != 使用局部变量 ==: 1. 基本数据类型 比较的就是数据 2. 引用数据类型 比较的就是内存地址值 String s = "aaa"; String s1 = new String("aaa"); s==s1 false s.equlas(s1); true 逻辑运算符 & && | || ^ ! & VS &&: 位运算符 0 1 & | ^ 三元运算符 语法: 数据类型 result = (表达式boolean)?结果1:结果2;
-
-
数据类型转换
-
byte short char int long float double 1. 自动转换 int num =100; double d = num; 2. 强制转换 double d = 100.123; int num =(int) d;
流程控制语句
结构化的程序设计: 1. 顺序结构 2. 分支结构 3. 循环结构
1. 分支结构
1.1 条件判断
if else else if
1. 语法
if(条件表达式 boolean){ //true 执行if逻辑 }[else{ //false 执行else逻辑 }]
2. 案例
案例1
public static void main(String[] args) { //使用数据类型+变量 定义一个男生的基本信息 String name = "吴克峰"; int age = 20; double height = 180; boolean isHansomeBoy = false; double money = 1000000; //介绍女朋友----> 女生有要求: 1. 身高>175 必须是一个帅哥 2. 有钱 >50w boolean flag = (height > 175 && isHansomeBoy) || (money > 500000); if (flag) { //true System.out.println("乔碧萝可以" + name + "继续聊一聊"); } else { System.out.println("pass"); System.out.println("pass"); System.out.println("pass"); } System.out.println("最美殿下乔碧萝........."); //if...else里面有且只有1行代码逻辑你 可以省略{} 推荐不要省略 }
案例2: 模拟用户登录
String username = "admin"; String password = "1234"; //2. 知道真实的用户名以及密码 String trueName = "admin"; String truePass = "1234"; //3. 2者之间进行比较 if (username.equals(trueName) && password.equals(truePass)) { System.out.println("登录成功,欢迎你:" + username); } else { System.out.println("登录失败,用户名或者密码有误"); }
3. 卫语句
代码设计。
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请录入登录的用户名:"); String username = input.nextLine(); System.out.println("请录入登录的密码:"); String password = input.nextLine(); String trueName = "admin"; String truePass = "1234"; //使用卫语句+return 反着来 一般都不写else语句 //登录失败的条件 if (!username.equals(trueName) || !password.equals(truePass)) { System.out.println("登录失败,用户名或者密码有误"); //使用关键字 控制流程 return return;// 在main里面 遇见return 结束main方法 } //登录成功地逻辑 System.out.println("登录成功,欢迎你:" + username); }
4.多重if
public static void main(String[] args) { /* 对学员的结业考试成绩评测 成绩>=90 :优秀 成绩>=80 :良好 成绩>=60 :中等 成绩<60 :差*/ //使用if...else实现 //可以使用多个if实现功能 //弊端: 给一个分数 都会执行4次判断-----> 结果输出 有且只运行其中一个if的逻辑 //可以使用多重if替换 // // if(){ // }else if (){ // } ... else{} Scanner input = new Scanner(System.in); System.out.println("请录入学生的成绩:"); int score = input.nextInt(); if (score >= 90) { System.out.println("优秀"); } else if (score >= 80) { System.out.println("良好"); } else if (score >= 60) { System.out.println("中等"); } else { System.out.println("差"); } System.out.println("111111111111"); /* if (score >= 90) { System.out.println("优秀"); } if (score >= 80 && score < 90) { System.out.println("良好"); } if (score >= 60 && score < 80) { System.out.println("中等"); } if (score < 60) { System.out.println("差"); }*/ input.close(); }
5. if嵌套
public static void main(String[] args) { //动态录入3个数字 最大值 Scanner input = new Scanner(System.in); System.out.println("请录入3个数字:"); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int max; //使用if..else实现 /*if (a > b) { //a if (a > c) { max = a; } else { max = c; } } else { //b if (b > c) { max = b; } else { max = c; } }*/ max = Math.max(Math.max(a, b), c); //三元运算符 System.out.println("最大值:" + max); }
1.2 Scanner
1. 获得数据
public static void main(String[] args) { //1.创建Scanner变量 Scanner a = new Scanner(System.in);//打开了一个物理资源: 控制台 //2.使用a变量获得不同数据类型的数据 byte b = a.nextByte(); System.out.println("字节:" + b); short s = a.nextShort(); System.out.println("短整型:" + s); int i = a.nextInt(); System.out.println("整型:" + i); long lon = a.nextLong(); System.out.println("长整型:" + lon); float f = a.nextFloat(); System.out.println("float:" + f); double d = a.nextDouble(); System.out.println("double:" + d); System.out.println(a.nextBoolean()); String str = a.next(); System.out.println("str:" + str); //没有获得单字符的功能 //3.释放资源 a.close(); }
2. 问题
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("录入一个int:"); int i = input.nextInt(); System.out.println(i); System.out.println("请录入一个string:"); String next = input.next(); System.out.println(next); input.nextLine();//读取空字符串 自动换行 System.out.println("请录入一个string1:"); String line = input.nextLine(); System.out.println(line); input.close(); //第1个问题 //next()/nextInt()... //拿数据: 从左往右获得数据 从光标之后获得数据 //只能获得空格以前的数据。遇见空格 停止获得数据 不会换行 //第2个问题: //nextLine(): 无法录入的问题 获得了一个""的数据 //从光标之后读取数据 会自动换行 //读取一整行的数据 //2020-01-01 12:00:00 }
1.3 条件选择语句
等值判断。 switch...case...break...deafult
1. 语法
switch(变量/表达式){// 可以对哪些类型的数据进行判断? // byte short char int enum String Byte Short Character Interger case 数据1: //逻辑代码 [break;] case 数据2: //逻辑代码 break; ..... case 数据n: //逻辑代码 break; [default: //逻辑代码 break;] } //case之间无序的
2. 案例
public static void main(String[] args) { //动态录入月份的数据 动态的打印输出 这个月份属于哪个季节 Scanner input = new Scanner(System.in); System.out.println("请录入月份值:(1-12)"); int monthValue = input.nextInt(); switch (monthValue) { case 12: case 1: case 2: System.out.println("冬季"); break; case 3: case 4: case 5: System.out.println("春季"); break; case 6: case 7: case 8: System.out.println("夏季"); break; case 9: case 10: case 11: System.out.println("秋季"); break; default: System.out.println("录入的月份有误"); break; } }
2. 循环结构
多次重复的执行一段程序。 循环一定要结束。 不能出现死循环
2.1 while
1. 语法
//1. 循环条件 boolean true/false //2. 循环体 //3. 结束循环的条件 while(循环条件){ //循环体代码 }
2. 案例
public static void main(String[] args) { //循环输出1-10之间的数字 //while /*int num = 1; while (num <= 10) { //循环体 System.out.println(num); num++; } System.out.println("num:"+num);*/ //动态录入学生的个数 动态录入每个学生的成绩 总分 平均分 Scanner input = new Scanner(System.in); System.out.println("录入学生的个数:"); int studentCount = input.nextInt(); int num = 1; double totalScore = 0; while (num <= studentCount) { System.out.println("请录入第" + (num++) + "个学生的成绩:"); int score = input.nextInt(); totalScore += score; } System.out.println("总分:" + totalScore); System.out.println("平均分:" + (totalScore / studentCount)); input.close(); }
2.2 do...while
1. 语法
do{ //循环体 }while(循环条件); 区别: while 先判断是否满足循环条件 do...while: 先执行循环体 再判断是否满足循环条件 不管是否满足循环条件 至少执行一次循环体内容
2. 案例
public static void main(String[] args) { /* int num = 1; do { System.out.println(num++); } while (num <= 10); System.out.println(num);*/ //登录 //登录失败 给3次机会重新登录 String trueName = "admin"; String truePass = "1234"; Scanner input = new Scanner(System.in); int count = 3; //在循环里面使用break 结束当前的循环语句 do { count--; System.out.println("录入name:"); String name = input.next(); System.out.println("录入pass:"); String pass = input.next(); if (trueName.equals(name) && truePass.equals(pass)) { System.out.println("登录成功 欢迎你:" + name); break; } //登录失败 if (count == 0) { System.out.println("3次机会使用完毕 程序退出"); break; } System.out.println("登录失败,请重新登录,还剩下" + count + "次机会"); } while (count > 0); System.out.println("1111111111111"); input.close(); }
2.3 for
1. 语法
for([表达式1];[循环条件];[表达式2]){ //循环体 }
2. 案例
public static void main(String[] args) { //案例: 获得1-n之间所有的奇数总和 以及奇数个数 Scanner input = new Scanner(System.in); System.out.println("请录入一个整数:"); int num = input.nextInt(); int sum = 0, count = 0; for (int i = 1; i <= num; i++) { //判断i是否是奇数 if (i % 2 == 1) { sum += i; count++; } } System.out.println("奇数总和:" + sum); System.out.println("奇数个数:" + count); input.close(); }
3. 嵌套
打印九九乘法表
public static void main(String[] args) { //2*5=10 //4*10=40 //6*15=90 //..... //?*100=? /*for (int num1 = 2, num2 = 5; num2 <= 100; ) { System.out.println(num1 + "*" + num2 + "=" + (num1 * num2)); num1 += 2; num2 += 5; }*/ for (int num1 = 1; num1 <= 9; num1++) {//控制行数 for (int num2 = 1; num2 <= num1; num2++) {//控制打印内容 System.out.print(num2 + "*" + num1 + "=" + (num1 * num2) + "\t"); } System.out.println(); } //锻炼逻辑思维 }
3.流程控制语句关键字
3.1 break
可以与switch和循环语句综合使用、 switch: 结束switch逻辑、 while/do..while/for: 结束/跳出当前的循环语句
public static void main(String[] args) { //需求: 录入1-100之间数据 打印输出前10个偶数 统计10个偶数和 int sum = 0; int count = 1; for (int num = 1; num <= 100; num++) { if (num % 2 == 0) { System.out.println("第" + count + "个偶数:" + num); sum += num; count++; if (count == 11) { break; } } } System.out.println("10个偶数和:" + sum); }
3.2 continue
继续。只与循环语句综合使用。
跳过本次循环 继续执行下面的循环逻辑。
public static void main(String[] args) { //1-100 过滤掉里面带4的数字 for (int i = 1; i <= 100; i++) { //数字里面带4 if (i / 10 == 4 || i % 10 == 4) { continue; } System.out.println(i); } }
4. SVN
subversion 项目版本管理工具。
git 分布式的版本管理工具。