专栏java基础专栏
上一期我们简单的学习了方法,本期开始,尽可能使用方法实现功能。此时,我们将学习java中的语句。
if (判断条件) {
语句体
}
执行流程:先执行小括号里面的判断条件,结果只有两个
一个是true 、一个是false
,然后根据判断的条件,再决定是否需要执行语句体。小括号里面的判断条件为 true,执行花括号里面的语句体
下括号里面的判断条件为false,则不执行花括号里面的语句体
代码示例:(判断是否成年)
package com.liujintao.meif;
/*
if 语句的使用:
*/
public class IfDemo {
public static void main(String[] args) {
String result = ifTest();
System.out.println(result); // 您已成年
}
public static String ifTest() {
// 如下代码
int age = 18;
String str = "";
if (age >= 18) {
str = age >= 18 ? "您已成年" : "未满十八,未成年!";
}
return str;
}
}
如果 否则
if (判断条件) {
语句体1
} else {
语句体2
}
执行流程:先执行小括号里面的判断条件,结果只有两个
一个是true 、一个是false
,然后根据判断的条件,再决定是否需要执行语句体。小括号里面的判断条件为 true,执行花括号里面的 语句体1,else后面的 语句体2 不执行
下括号里面的判断条件为false,则不执行花括号里面的 语句体1,else后面的 语句体2 执行
package com.liujintao.meif;
/*
if 语句的第二种格式:
if () {
语句1
} else {
语句2
}
*/
public class IfDemo {
public static void main(String[] args) {
ifTest2(); // 未成年,不可以上网吧!
}
public static void ifTest2() {
int age = 17;
if (age >= 18) {
System.out.println("成年,可以上网吧!");
} else {
System.out.println("未成年,不可以上网吧!");
}
}
}
如果 否则如果 否则
if (判断条件1) {
语句1
} else if (判断条件2) {
语句2
}
......省略
else {
语句n + 1
}
执行流程:
1、首先计算判断条件1 的值
2、如果值为 true 就执行语句1,如果值为 false 就计算判断语句2 的值
3、如果值为 true 就执行语句2 , 如果值为 false 就计算判断语句3的值
…
如果所有的值都为false 就执行else里面的语句。
package com.liujintao.meif;
/*
if 语句的第种格式:
*/
import java.util.Scanner;
public class IfDemo {
public static void main(String[] args) {
ifTest3(); // 输入 1 和 2 或者 其他数字
}
public static void ifTest3() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的数字身份(1: 会员 2: 非会员):");
int num = sc.nextInt();
if (num == 1) {
System.out.println("会员身份");
} else if (num == 2) {
System.out.println("非会员身份");
} else {
System.out.println("输入有误");
}
}
}
需求1、:判断密码是否正确
需求2、:根据学生的成绩,执行相应的区间语句
package com.liujintao.test;
import java.util.Scanner;
public class IfTest {
/*
需求1、:判断密码是否正确
*/
public static void main(String[] args) {
getPassword();
/*
输入内容:123456
返回结果:password True!
*/
}
// 需求1;
public static void getPassword() {
System.out.println("enter your PIN:");
Scanner sc = new Scanner(System.in);
int pswd = sc.nextInt();
// 配对
if (pswd == 123456) {
System.out.println("password True!");
} else {
System.out.println("password False!");
}
}
}
package com.liujintao.test;
import java.util.Scanner;
public class IfTest {
/*
需求2、:根据学生的成绩,执行相应的区间语句
*/
public static void main(String[] args) {
handleScore();
}
// 需求2;
public static void handleScore() {
Scanner sc = new Scanner(System.in);
System.out.println("input score:");
int score = sc.nextInt();
if (score >= 90 && score <= 100) {
System.out.println('A');
} else if (score >= 80 && score < 90) {
System.out.println('B');
} else if (score >= 70 && score < 80) {
System.out.println('C');
} else if (score >= 60 && score < 70) {
System.out.println('D');
} else if (score >= 0 && score < 60) {
System.out.println('E');
} else { // 除了上面的外,其余的数字都返回 ** ,优化处理!(也可使用 if 先判断)
System.out.println("**");
}
}
}
嵌套优化写法:
// 需求2;
public static void handleScore() {
Scanner sc = new Scanner(System.in);
System.out.println("input score:");
int score = sc.nextInt();
if (score > 0 || score <= 100) {
if (score >= 90 && score <= 100) {
System.out.println('A');
} else if (score >= 80 && score < 90) {
System.out.println('B');
} else if (score >= 70 && score < 80) {
System.out.println('C');
} else if (score >= 60 && score < 700) {
System.out.println('D');
} else if (score >= 0 && score < 60) {
System.out.println('E');
}
} else {
System.out.println("输入有误!");
}
}
if 语句的 注意事项:
switch () {
case 值1:
语句体1;
break;
case 值2:
语句体2;
break;
case 值3:
语句体3;
break
case 值4:
语句体4;
break;
........
default:
语句体n + 1;
break;
}
执行流程:
- 首先,计算switch语句中的表达式的值。
- 接下来,程序将根据表达式的值与每个case标签的值进行比较,以找到与表达式值匹配的case标签。
- 一旦找到匹配的case标签,程序将从该标签处开始执行相应的代码块,直到遇到break语句或者switch语句结束。
- 如果没有找到与表达式值匹配的case标签,程序将会执行default标签后的代码块(如果有的话),或者直接跳过switch语句。
package com.liujintao.meif;
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
/*
switch语句的基本使用
*/
switchTest1();
}
public static void switchTest1() {
Scanner sc = new Scanner(System.in);
int week = sc.nextInt();
switch(week) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
default:
System.out.println("请合理输入数字!");
break;
}
}
}
case 后面的值不允许重复
case 后面的值,只能是字面量,不能是变量
switch () 中 可以接收的类型为
case 穿透现象:当我们再一个case中没有加上break语句,那么无论匹不匹配,都回到下一个case中执行代码,直到遇到break才停止。(在有些时候,case穿透还可以优化代码)
switch(week) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日");
break;
case 6:
case 7:
System.out.println("休息日");
default:
System.out.println("请合理输入数字!");
break;
}
下面两种使用的少:
switch(5) {
case 1,2,3,4,5:
System.out.println("工作日");
break;
case 6,7:
System.out.println("休息日");
break;
default:
System.out.println("输入有误");
break;
}
switch(5) {
case 1,2,3,4,5 -> System.out.println("工作日");
case 6,7 -> System.out.println("休息日");
default -> System.out.println("输入有误");
}
if语句: 适用于范围性的判断
switch语句:适用于固定值的匹配
循环就和跑圈一样,只要没跑到符合条件的圈数,就重复一直跑。
语法格式
for (初始化语句; 判断条件; 条件控制语句) {
循环体语句;
}
执行流程:
1、执行初始化语句,在整个循环中,只执行一次
2、执行判断条件,看返回结果是 true 还是 false,true执行第三步,false循环结束
3、执行循环体语句
4、执行条件控制语句
5、回到 2 继续...
代码示例
for (int i;i <= 3; i++) {
System.out.println("跑圈");
}
package com.liujintao.test;
public class ForTest {
public static void main(String[] args) {
Print();
}
/*
需求:使用循环打印 结果(自定义): 这里打印十次 内容
*/
public static void Print() {
for (int i = 1;i <= 10; i++) {
System.out.println("this is java");
}
}
}
自定义循环次数
循环多少次,取决于用户输入的数字
同时还做了节流判断
package com.liujintao.test;
import java.util.Scanner;
public class ForTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
Print( count);
}
public static void Print(int count) {
if (count < 1) {
System.out.println("输入的内容有问题!(节流判断)");
} else {
for (int i = 1; i <= count; i++) {
System.out.println("this is java");
}
}
}
}
打印循环次数
public static void printNumber() {
for (int i = 1; i <= 3; i++) {
System.out.println(i); // i 就是变量,是每次循环 i 的数字, 就是循环的次数。
}
}
由此可知,变量 i,在循环中还能使用。
for (int i = 3; i >= 1; i--) {
System.out.println(i); // 倒序
}
通过上面知道,for循环里面的i,不仅能++ ,还能–
需求:求1-100之间的偶数和,并把求和结果在控制台输出
步骤分析:
1、使用for循环,获取 1-100数字。
2、循环使用if语句判断,筛选出偶数
3、将筛选出来的数据累加起来
代码实现
int sum = 0;
for (int i = 1;i <= 100; i++) {
if (i % 2 == 0) {
sum += i;
}
}
return sum;
第二种方式:
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
return sum;
需求:在控制台输出所有的 “水仙花数”
什么是水仙花数?:个位、十位、百位的数字立方和等于原数
实现步骤:
100-999之间:
1.获取到所有的三位数
2.将每个三位数:拆分开
for (int i = 100; i <= 999; i++) {
// 拆分数字
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (ge * ge *ge + shi * shi * shi + bai * bai * bai == i) {
System.out.println(i + "是水仙花数");
}
}
需求就是多了一个统计合数,代码如下:
int count = 0;
for (int i = 100; i <= 999; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.println(i + "是水仙花数!");
count += 1;
}
}
return count;
1、 for循环{} 中定义的变量,在每一轮循环结束后,都会从内存释放。
2、for循环()中定义的变量,在整个循环结束后,会从内存中释放。
3、for循环 () 和 {} 之间不要写分号。
循环嵌套:就是在循环中,继续出现循环语句。
需要注意:当循环执行完一次,下一轮循环才会开启。
1、使用循环嵌套打印四行五列的星星
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.print() 打印后不换行
System.out.println() 打印后换行
这里就用使用后了这两个输出语句。
2、在控制台用 * 打印五行的直角三角形
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
唯一需要注意的是,外层循环执行一次,内层循环执行一周期。切记!
3、倒直角三角形打印
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
初始化语句;
while(判断条件) {
循环语句;
条件控制语句;
}
1、执行初始化语句
2、执行判断条件:结果为true,执行循环体。false结束循环
3、条件成立,执行循环体
4、执行条件控制语句
5、回到第二步,重复(直到条件不好吃呢管理)
int i = 1;
while (i <= 5) {
System.out.println("Hello world");
}
package com.liujintao.test;
/*
使用 while循环 打印水仙花数
*/
public class whileTest {
public static void main(String[] args) {
sxhNum();
}
public static void sxhNum() {
int i = 100;
int count = 0;
while (i <= 999) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (ge * ge * ge + shi * shi* shi + bai * bai * bai == i) {
System.out.println(i + "是水仙花数");
count += 1;
}
i++;
}
System.out.println("水仙花个数为:" + count);
}
}
初始化语句
do {
循环语句;
条件控制语句;
} while (判断条件);
1、执行初始化语句
2、执行循环语句
3、执行条件判断语句
4、执行条件判断语句: true执行循环体(回到第二步重复),false 结束循环
package com.liujintao.loop;
/*
do....while 语句: 打印水仙花数。
*/
public class doWhileDemo {
public static void main(String[] args) {
doWhileNum();
}
public static void doWhileNum() {
int count = 0;
int i = 100;
do {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.println(i + "是水仙花");
count += 1;
}
i++;
} while (i <= 999);
System.out.println(count);
}
}
for 和 while 的区别:
总结:for循环能实现的,while循环都能实现,反之如此。根据自己的喜好选择即可。
brake语句:当代码执行到了该语句,直接跳出循环,终止循环。(前面Switch也用到过break语句)
下面用代码来看看他的使用场景:
package com.liujintao.control;
import java.util.Scanner;
public class BreakDemo {
/*
需求:模拟用户登录:
1、默认密码为123456;
2、只有三次机会
3、成功则跳出循环
*/
public static void main(String[] args) {
login();
}
public static void login() {
int psd = 123456;
int count = 3;
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("input password:");
int password = sc.nextInt();
if (! (psd == password)) {
System.out.println("password error" + " residue:" + (3 - i));
System.out.println("密码错误!");
count -= 1;
if (count < 1) {
System.out.println("登录失败,次数用尽!");
// 我需要结束循环,则使用break语句
break;
}
} else {
System.out.println("password true" + " user:" + (3 - i));
System.out.println("恭喜你,登录成功!");
count -= 1;
// 登录成功,我也是要使用break语句终止循环语句。
break;
}
}
}
}
break是终止循环,不在执行
这里的continue则是跳过当前循环,直接调到下一次循环。
for (int i = 1; i<= 10; i++) {
if (i == 3 || i == 7) {
continue;
}
System.out.println("我是:" + i); // 3 和 7 则不会被打印
}
for:
for (;;) {
}
while :
while (true) {
}
do…while :
do {
} while (true)
需求:用户选择 1:添加学生、2:删除学生、3:修改学生、4: 查看学生、 5:退出
实现步骤:
1、用户选择,需要键盘录入;
2、退出需要break语句(continue:结束当前)
3、没有明确次数需求;那么就用上面的死循环。通过break结束控制完成需求
代码示例:
package com.liujintao.control;
import java.util.Scanner;
public class ControalDemo {
public static void main(String[] args) {
initMenu();
}
public static void initMenu() {
Scanner sc = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("用户选择 1:添加学生、2:删除学生、3:修改学生、4: 查看学生、 5:退出");
int num = sc.nextInt();
switch(num) {
case 1:
System.out.println("添加学生");
break;
case 2:
System.out.println("删除学生");
break;
case 3:
System.out.println("修改学生");
break;
case 4:
System.out.println("查看学生");
break;
case 5:
System.out.println("退出");
// 退出:表示要结束循环,不在执行了。将循环的boolean值改成false,不成立 (注意: 写在break上面)
flag = false;
break;
}
}
System.out.println("退出成功!");
}
}
flag = true (开关变量),控制循环
特别注意:上面在的 case5 中,使用了修改while(死循环的问题),来结束循环。
Random:产生随机数 (0, 10] == 0 - 9 → 不包含10 那么我 + 1 表示在每一个随机数的基础上 + 1。
使用方法和 Scanner 相似:
1、 导入Random
2、创建实例对象
3、使用产生随机数
import java.util.Random;
Random r = new Randem(); // 这里不需要写 System.in
r.nextInt(10) + 1; // 表示产生:1 - 10 之间的随机数
package com.liujintao.random;
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
getRandom();
}
public static void getRandom () {
Random r = new Random();
int num = r.nextInt(10) + 1; // 表示 1- 10
System.out.println(num);
}
}
再例如:我需要。产生 66 - 88 之间的随机数:
int num = r.nextInt(23) + 66;
0 + 66 == 66
23 + 66 == 89 (只能取到88)
1-100之间猜数字游戏(十次机会),注意提示哦!
package com.liujintao.test;
import java.util.Random;
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
getRandom();
}
public static void getRandom() {
Random r = new Random();
Scanner sc = new Scanner(System.in);
int num = r.nextInt(100) + 1;
int count = 10;
for (int i = 1; i <= 10; i++) {
count -= 1;
System.out.println("请输入数字;");
int inputNum = sc.nextInt();
if (inputNum == num) {
System.out.println("恭喜你! 第" + i + "次就猜中了!");
break;
} else {
if (inputNum < num) {
System.out.println("数字小了");
} else {
System.out.println("数字大了");
}
}
}
if (count < 1) {
System.out.println("次数用尽,欢迎下次继续!");
}
}
}