JAVA学习笔记第二天——逻辑控制语句
2020-07-08 09:01
323 查看
一、回顾
变量:内存中空间用于存储数据 数据类型: 8种基本数据类型:二进制补码 byte short/char int long float double boolean:true/false byte: -128~127 char: 0~65535 '\u0000' ~ '\uFFFF' A:65 a:97 '1':48 引用类型 除了基本数据类型,都是引用 数组、类、接口、枚举... 运算符: 算数运算符 + - * / % ++ -- 逻辑运算符 &&/& ||/| 短路 boolean f = a++ < 1 && b++ < 2; a=3,b=4 a = 4, b = 4 位运算符 & | ^ ! 比较运算符 > < = >= <= == != 三目运算符 boolean ? value1:value2 位移运算 >> << >>>[无符号右移] 命名规则: 变量:都是小写,多个单词拼接,首字母小写,第二个开始单词首字母大写 helloWorld -> 驼峰命名法 类名:首字母大写,驼峰命名法 HelloWorld 包名:都是小写,域名倒写 www.baidu.com com.baidu.baike.xx
二、逻辑控制语句
顺序结构、条件分支
if(判断的boolean类型的表达式) {} if - else :可以互相嵌套,但是嵌套次数不宜太多,<=3次最多 if - else if - ... - else 多种情况时使用 if 不能省,其他的else都可以省略 在if中判断相等的情况下: 可以用switch-case替代 switch中可以放置的变量类型有: int\short\byte\char JDK1.7以后 支持String 枚举
Demo 01 if 条件分支——判断一个年份是不是闰年
import java.util.Scanner; public class Demo01 { public static void main(String[] args) { // 1.判断一个年份是不是闰年 // 闰年: 能被4整除, 但是不能被100整除, 或者能被400整除 // 通过控制台输入年份,来判断 System.out.println("请输入年份: "); Scanner console = new Scanner(System.in); int year = console.nextInt(); if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "年,是闰年"); }else { System.out.println(year + "年,不是闰年"); } } }
Demo 02 if 条件分支——编写一个收银柜台收款程序
import java.util.Scanner; /** * 编写一个收银柜台收款程序, * 根据商品单价、购买数量以及收款金额计算并输出应收金额和找零; * 当总价大于或等于500时,享受8折优惠。 * 考虑程序的异常情况:收款金额小于应收金额。 */ public class Demo02 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入单价(¥):"); double price = console.nextDouble(); System.out.println("请输入数量:"); int count = console.nextInt(); System.out.println("请输入金额(¥):"); double money = console.nextDouble(); // 计算总金额 double sum = price * count; // 判断要不要打折 if (sum >= 500) { sum *= 0.8; } // 判断金额够不够 if (money >= sum) { System.out.println("应付金额:" + sum + ",找零:" + (money-sum)); } else { // 金额不够 System.out.println("金额有误!"); } } }
Demo 03 if 条件分支——给分数评级 0~100
public class Demo03 { public static void main(String[] args) { // 给分数评级 0~100 90以上A+ 85~90 A 80~85 B 70~80 C 60~70 D 60以下 E int score = 100; if (score >= 90) { System.out.println("分数A+"); } else { if (score >= 85) { System.out.println("分数A"); } else { if (score >= 80) { System.out.println("分数B"); } else { if (score >= 70) { System.out.println("分数C"); } else { if (score >= 60) { System.out.println("分数D"); } else { System.out.println("分数E"); } } } } } } }
Demo 04 if - else if - … - else ——给分数评级 0~100
public class Demo05 { public static void main(String[] args) { // 给分数评级 0~100 90以上A+ 85~90 A 80~85 B 70~80 C 60~70 D 60以下 E int score = 100; if (score >= 90) { System.out.println("分数A+"); } else if (score >= 85) { System.out.println("分数A"); } else if (score >= 80) { System.out.println("分数B"); } else if (score >= 70) { System.out.println("分数C"); } else if (score >= 60) { System.out.println("分数D"); } else { System.out.println("分数E"); } } }
Demo 05 if - else if - … - else ——控制台输入月份, 判断月份中有几天 1~12,并输出
import java.util.Scanner; /** * 控制台输入月份 * 判断月份中有几天 1~12,并输出 */ public class Demo06 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入月份(1~12):"); int month = console.nextInt(); if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { System.out.println(month + "月中有31天。"); } else if (month == 4 || month == 6 || month == 9 || month == 11) { System.out.println(month + "月中有30天。"); } else if (month == 2) { System.out.println(month + "月中有28天。"); } else { System.out.println("月份有误!"); } } }
Demo 06 switch-case ——控制台输入月份, 判断月份中有几天 1~12,并输出
import java.util.Scanner; /** * 控制台输入月份 * 判断月份中有几天 1~12,并输出 * 使用switch-case替代 */ public class Demo07 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入月份(1~12):"); int month = console.nextInt(); // switch-case具有穿透性,使用break可以阻断穿透性 switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println(month + "月中有31天。"); break; case 4: case 6: case 9: case 11: System.out.println(month + "月中有30天。"); break; case 2: System.out.println(month + "月中有28天。"); break; default: // 最后的break可以省略 System.out.println("月份有误!"); } } }
循环结构
重复执行的事情,有规律的事情 for - 有规律的 while - 当... 1.先判断有没有答对 2.再循环猜数字 do-while - 做...直到... 1.第一次不用判断,直接猜 2.猜完,才要判断循环要不要继续 结论:while和do-while在一定程度上可以通用 只有在第一次就不符合循环条件时,有区别 do-while至少执行1次,while可能会执行0次 break: 结束当前循环,可以借助变量名来结束外层循环 continue: 结束本次循环,继续下一次循环
Demo 01——for 循环初试
public class Demo08 { public static void main(String[] args) { // for // 1.i 赋值 int i=1 循环的初始值 - 位置可以在for外面 // 2.i 判断 i<10 循环的条件,可以不写,不写就是死循环 // 3.条件成立,执行{代码} // 4.i变化,步长, 变化规律 i++ for (int i = 0; i < 10; i++){ // for循环体 System.out.println(i); } // i 作用范围,只在for中有效 // -------- int i = 0; for (; i < 10; i++){ System.out.println(i); } // -------- for (int j = 0; j < 10;){ System.out.println(j); j++; } // -------- // 中间条件没有了,就相当于永久成立,说明是不能结束的循环-死循环 for (int j = 0; ; j++){ if (j >= 10) { // 结束循环 break; } System.out.println(j); } // 如果上面是死循环,死循环后面不能再加代码 // 死循环,不需要i变量 for (;;) { } } }
Demo 02——for 循环求 1~50 的和
public class Demo09 { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 50; i++) { sum += i; } System.out.println(sum); } }
Demo 03——while 循环输出
public class Demo10 { public static void main(String[] args) { // for (int i = 0; i < 10; i++){} int j = 0; while (j < 10) { j++; if (j==5){ continue; // 结束本次循环,继续下一次循环 } System.out.println(j); } } }
Demo 04——for 循环中break的使用
public class Demo11 { public static void main(String[] args) { loop1: for (int i = 0; i < 2; i++){ int j = 0; loop2: while (j < 3) { j++; System.out.println(j); if (j == 2) { // for循环直接结束 // break; // 结束当前循环 // break结束外层循环 break loop1; } } } } }
Demo 05——for 循环实现100以内加法
import java.util.Scanner; public class Demo12 { public static void main(String[] args) { /* 1.生成10个100以内的加法算术式 23 + 45 = ? 2.并且输入答案 3.最后显示分数 */ Scanner console = new Scanner(System.in); int score = 0; // 生成随机数: 生成一个随机的[0,1) 小数 0~99范围 for (int i = 1; i <= 10; i++) { int a = (int) (Math.random() * 100); int b = (int) (Math.random() * 100); System.out.println("(" +i +")."+a + " + " + b + " = ?"); System.out.print("请输入答案,输入-1退出:"); int answer = console.nextInt(); if (answer == -1) { break; } if (answer == (a + b)) { System.out.println("答对了!"); score += 10; } else { System.out.println("答错了!"); } } System.out.println("最终分数为:" + score); } }
Demo 06——while 循环猜字母游戏
import java.util.Scanner; public class Demo13 { public static void main(String[] args) { // 1.先生成一个1~1000的随机数 int random = (int) (Math.random() * 1000 + 1); System.out.println("最终结果是:" + random); // 2.猜一个数字 Scanner console = new Scanner(System.in); /*while (true) { int a = console.nextInt(); // 3.判断猜的数字大还是小 if (a > random) { System.out.println("太大了"); } else if (a < random) { System.out.println("太小了"); } else { System.out.println("恭喜你答对了!"); break; } }*/ int a = -1; // 先定义了一个a,一定不是正确答案 while (a != random) { System.out.println("请输入你猜的数字,输入0退出:"); a = console.nextInt(); // 判断是否退出 if (a == 0) { break; } // 3.判断猜的数字大还是小 if (a > random) { System.out.println("太大了"); } else if (a < random) { System.out.println("太小了"); } } if (a == random) { System.out.println("恭喜你答对了!"); } else { System.out.println("很遗憾!没有答对!"); } } }
Demo 06——do – while 实现猜字母游戏
import java.util.Scanner; public class Demo14 { public static void main(String[] args) { // 1.先生成一个1~1000的随机数 int random = (int) (Math.random() * 1000 + 1); System.out.println("最终结果是:" + random); // 2.猜一个数字 Scanner console = new Scanner(System.in); int a; // 先定义了一个a do { System.out.println("请输入你猜的数字,输入0退出:"); a = console.nextInt(); // 判断是否退出 if (a == 0) { break; } // 3.判断猜的数字大还是小 if (a > random) { System.out.println("太大了"); } else if (a < random) { System.out.println("太小了"); } } while (a != random); if (a == random) { System.out.println("恭喜你答对了!"); } else { System.out.println("很遗憾!没有答对!"); } } }
Demo 07——循环嵌套,命名
public class Demo08Loop { public static void main(String[] args) { // 1.循环是可以嵌套的 int a = 0; x:for(int i = 0; i < 5; i++) { y:for(int j = 0; j < 3; j++) { // 结束当前循环 if (j == 1) { // 结束这一次循环,继续下一次循环 continue; } System.out.println("i = " + i + ", j = " + j); } } // 9行: i表示行数 for(int i = 1 ;i <= 9; i++) { // 每一行有 i 列 for (int j = 1; j <= i; j++) { } } } }
相关文章推荐
- Java学习笔记---4.Java的分支循环语句
- java学习笔记--第二天
- Java学习笔记62. 异常处理语句 try...catch...finally
- [Java学习笔记] 猜猜看(2) ~ 中断循环语句的一种方法(能够中断所有进行中的循环)
- Java笔记———流程语句学习
- Java学习笔记28. 跳转语句break和continue
- JAVA基础知识学习笔记(二)--JAVA条件语句
- java学习笔记---数据类型、运算符和控制语句
- Java学习笔记——switch语句的参数类型
- JAVA学习笔记(三) - 选择语句
- 中软Java学习笔记第二天
- 4000 【Java】Java学习笔记-控制语句
- Java学习笔记6——Java控制语句(选择结构、循环结构、break语句和continue语句、带标签的break和continue、语句块)
- java第三天学习笔记【流程控制语句】
- java学习笔记——java条件语句
- JAVA基础第二天学习日记_标识符、常量与变量、运算符、条件控制语句
- Java学习笔记33:mybatis动态SQL语句
- 黑马程序员-java学习笔记-循环语句
- java学习笔记--------第二天
- Java基础学习笔记 -- 8(循环语句)