一、知识点题目
1. IfDemo1.java(重点)
需求:演示if语句的使用
步骤:
(1)定义一个整数类型的变量a
(2)判断a大于5,大于打印“a大于5”
(3)判断a大于20,大于打印“a大于20”
import java.util.Scanner;
public class IfDemo1 {
public static void main(String[] args) {
// 从控制台中读取输出的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入变量a的值:");
// 获取控制台输入的a的值
int a = s.nextInt();
if (a > 5) System.out.println("a大于5");
if (a > 20) System.out.printf("a大于20");
}
}
2. IfElseDemo.java(重点)
需求:演示if-else语句的使用
步骤:
(1)定义一个整数类型的变量a
(2)判断a是奇数还是偶数,如果是偶数打印“a是偶数”,否则打印“a是奇数”
import java.util.Scanner;
public class IfElseDemo {
public static void main(String[] args) {
// 从控制台中读取输入的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入变量a的值:");
// 获取控制台输入a的值
int a = s.nextInt();
if (a % 2 == 0) {
System.out.println("a是偶数");
} else {
System.out.println("a是基数");
}
}
}
3. IfElseIfElseDemo.java(重点)
需求:演示if-else if-else语句的使用
步骤:
(1)定义整数类型的两个数a和b
(2)判断a和b的大小关系
import java.util.Scanner;
public class IfElseIfElseDemo {
public static void main(String[] args) {
// 从控制台中读取输入的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入a和b的值:");
// 获取控制台输入a的值
int a = s.nextInt();
// 获取控制台输入b的值
int b = s.nextInt();
if (a > b) {
System.out.println("a大于b");
} else if (a < b) {
System.out.println("a小于b");
} else {
System.out.println("a等于b");
}
}
}
4. IfElseIfElseDemo2.java
需求:根据输入的一个数字,判断是星期几?
步骤:
(1)定义一个整数类型的变量weekday
(2)判断变量weekday,如果weekday为1输出“周一”,如此类推
import java.util.Scanner;
public class IfElseIfElseDemo2 {
public static void main(String[] args) {
// 从控制台中读取输入的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入1~7之间的数字,判断是星期几?");
// 获取控制台中输入weekday的值
int weekday = s.nextInt();
if (weekday == 1) {
System.out.println("周一");
} else if (weekday == 2) {
System.out.println("周二");
} else if (weekday == 3) {
System.out.println("周三");
} else if (weekday == 4) {
System.out.println("周四");
} else if (weekday == 5) {
System.out.println("周五");
} else if (weekday == 6) {
System.out.println("周六");
} else if (weekday == 7) {
System.out.println("周七");
} else {
System.out.println("输入的数据无效");
}
}
}
5. IfElseIfElseDemo3.java
需求:根据QQ在线的天数来判断QQ的等级
步骤:
(1)定义一个变量days,表示天数
(2)判断天数范围
import java.util.Scanner;
public class IfElseIfElseDemo3 {
public static void main(String[] args) {
// 从控制台中读取输入的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入QQ在线天数:");
// 获取控制台中输入的days的值
int days = s.nextInt();
if (days >= 0 && days < 5) {
System.out.println("QQ无等级");
} else if (days < 12) {
System.out.println("QQ等级为⭐");
} else if (days < 21) {
System.out.println("QQ等级为⭐⭐");
} else if (days < 32) {
System.out.println("QQ等级为⭐⭐⭐");
} else{
System.out.println("QQ等级为🌙");
}
}
}
6. SwitchDemo.java
需求:使用switch来完成,根据输入的一个数字,判断是星期几?
步骤:
(1)定义一个整数类型的变量weekday
(2)判断变量weekday,如果weekday为1输出“周一”,如此类推
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
// 从控制台中读取输入的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入1~7之间的数字,判断是星期几?");
// 获取控制台中输入的weekday的值
int weekday = s.nextInt();
switch (weekday) {
case 1:
System.out.println("周一");
break;
case 2:
System.out.println("周二");
break;
case 3:
System.out.println("周三");
break;
case 4:
System.out.println("周四");
break;
case 5:
System.out.println("周五");
break;
case 6:
System.out.println("周六");
break;
case 7:
System.out.println("周日");
break;
default:
System.out.println("输入的数字无效");
}
}
}
7. WhileDemo.java(重点)
需求:使用while循环,打印10次帅哥
步骤:
(1)定义一个变量count,表示计数器
(2)使用while循环,判断count是否小于10
(3)打印“帅哥”和循环次数
(4)计数器+1
public class WhileDemo {
public static void main(String[] args) {
// 需求: 使用while循环,打印10次帅哥
// 1.定义一个变量count,表示计数器
int count = 0;
// 2.使用while循环,判断count是否小于10
while (count < 10) {
// 3.打印帅哥和循环次数
System.out.println("帅哥,当前循环次数为" + (count + 1));
// 4.计数器+1
count++;
}
// 5.打印循环次数
System.out.println("总共循环次数为" + count);
}
}
8. WhileDemo2.java
需求:打印从1到100的数
步骤:
(1)定义一个变量number
(2)循环打印number
public class WhileDemo2 {
public static void main(String[] args) {
// 需求: 打印从1到100的数
// 1.定义一个变量number
int number = 1;
while (number <= 100) {
// 2.循环打印number
System.out.println(number);
number++;
}
}
9. WhileDemo3.java
需求:计算100以内的正整数和
步骤:
(1)定义一个变量number,表示加数
(2)定义一个变量sum,表示前N个数之和
(3)使用while循环,每循环一次sum和当前的number相加一次
(4)number自增1
public class WhileDemo3 {
public static void main(String[] args) {
// 需求: 计算100以内的正整数和
// 1.定义一个变量number,表示加数
int number = 1;// 初始值为1
// 2.定义一个变量sum,表示前N个数之和
int sum = 0;// 初始化为0
// 3.使用while循环,每循环一次sum和当前的number相加一次
while (number <= 100) {
sum += number;
// 4. number自增1
number++;
}
// 5. 打印sum
System.out.println(sum);
}
}
10. DoWhileDemo1.java(一遍)
需求:演示while和do-while的区别
步骤:
(1)定义两个整数变量a和b
(2)分别使用while和dowhile判断a > b
注意:出来结果while没有打印任何内容,do-while打印了一次“a > b”,才算作业完成。
public class DoWhileDemo1 {
public static void main(String[] args) {
// 需求: 演示while和do-while的区别
// 1.定义两个整数变量a和b;
int a = 10;
int b = 20;
// 2. 分别使用while和dowhile判断a>b
// while
while (a > b) {
System.out.println("a大于b"); // a大于b 不会在控制台输出
}
// dowhile
do {
System.out.println("a大于b"); // a大于b 会在控制台输出1次
} while (a > b);
// 总结:while和dowhile的区别就在于,while语句中,只有当boolean条件满足true时,才会执行循环体,否则不执行;dowhile语句中,无论boolean条件是否满足true,都会先执行一次循环体
}
}
11. DoWhileDemo2.java(一遍)
需求:演示do-while语句的使用
步骤:
(1)使用do-while循环打印500次“帅哥”
(2)使用do-while循环求出100以内的正整数之和
public class DoWhileDemo2 {
public static void main(String[] args) {
// 需求: 演示do-while语句的使用
// 使用do-while循环打印500次"帅哥"
int count = 1;
do {
System.out.println("帅哥" + count);
count++;
} while (count <= 500);
// 使用do-while循环求出100以内的正整数之和
int i = 1;
int sum = 0;
do {
sum += i;
i++;
} while (i <= 100);
System.out.println(sum);
}
}
12. ForDemo.java(重点)
需求:演示for语句的使用
步骤:
(1)使用for循环打印1到10
(2)使用for循环计算100以内正整数之和
public class ForDemo {
public static void main(String[] args) {
// 需求: 演示for语句的使用
// 1.使用for循环打印1到10
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
// 2.使用for循环计算100以内正整数之和
int sum = 0;
for (int count = 1; count <= 100; count++) {
sum += count;
}
System.out.println(sum);
}
}
13. LoopInLoopDemo.java
需求:输出直角三角形
思路:
(1)最简单地方式,逐行打印
(2)普通方式,使用循环打印每一行
(3)通过普通方式找到规律,写出最终通用版本
作业要求:把得到最终版本的过程写出来
public class LoopInLoopDemo {
public static void main(String[] args) {
// 需求:输出直角三角形
// 1.最简单的方式,逐行打印
System.out.println("最简单的方式,逐行打印");
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("===============");
// 2.普通方式,使用循环打印每一行
System.out.println("普通方式,使用循环打印每一行");
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= i) {
System.out.print("*");
j++;
}
i++;
System.out.println();
}
// 规律:每一行的*的个数与该行数或列数相等
System.out.println("==============");
// 3.最终通用版本
// 3.1外层循环代表行
for (int row = 1; row <= 3; row++) {
// 3.2内存循环代表列
for (int col = 1; col <= row; col++) {
// 3.3每一行都要输出的*的个数都与行数或者列数相等
System.out.print("*");
}
// 3.4打印完一行的*后,必须得换行
System.out.println();
}
// 总结:外层循环执行一次,内层循环执行一轮
}
}
14. Table99Demo.java
需求:打印九九乘法表
思路:
(1)傻B方式,逐行打印
(2)普通方式,使用循环打印每一行
(3)通过普通方式找到规律,写出最终通用版本
作业要求:把得到最终版本的过程写出来
public class Table99Demo {
public static void main(String[] args) {
// 需求:打印九九乘法表
// 1.傻B方式,逐行打印
System.out.println("1×1=1");
System.out.println("1×2=2 2×2=4");
System.out.println("1×3=3 2×3=6 3×3=9");
System.out.println("1×4=4 2×4=8 3×4=12 4×4=16");
System.out.println("1×5=5 2×5=10 3×5=15 4×5=20 5×5=25");
System.out.println("1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36");
System.out.println("1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49");
System.out.println("1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64");
System.out.println("1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81");
System.out.println("========================");
// 2.普通方式,使用循环打印每一行
int r = 1;
while (r <= 9) {
int c = 1;
while (c <= r) {
System.out.print(c + "×" + r + "=" + (c * r) + " ");
c++;
}
System.out.println();
r++;
}
System.out.println("======================");
// 3.通过普通方式,写出最终版本
// 3.1外层循环代表行
for (int row = 1; row <= 9; row++) {
// 3.2内层循环代表列
for (int col = 1; col <= row; col++) {
// 3.3打印出每一行的乘法口诀
System.out.print(col + "×" + row + "=" + (col * row) + " ");
}
// 3.4打印完每行数据,记得换行
System.out.println();
}
}
}
15. BreakDemo.java(重点)
需求:从1输出到10,当迭代变量为7,就停止循环
步骤:
(1)循环打印1到10
(2)判断循环次数是否为7,是就停止循环,否则打印循环次数
public class BreakDemo {
public static void main(String[] args) {
// 需求:从1输出到10,当迭代变量为7,就停止循环
// 1.while循环
// 1.1.循环打印1到10
int i = 1;
while (i <= 10) {
// 1.2判断循环次数是否为7,是就停止循环,否则打印循环次数
if (i == 7) {
break;
} else {
System.out.println(i);
}
i++;
}
System.out.println("===================");
// 2.do-while循环
// 2.1循环打印1到10
int j = 1;
do {
// 2.2 判断循环次数是否为7,是就停止循环,否则打印循环次数
if (j == 7) {
break;
} else {
System.out.println(j);
}
j++;
} while (i <= 10);
System.out.println("====================");
// 3.for循环
// 3.1 循环打印1到10
for (int count = 1; count <= 10; count++) {
// 3.2 判断循环次数是否为7,是就停止循环,否则打印循环次数
if (count == 7) {
break;
} else {
System.out.println(count);
}
}
}
}
16. ReturnDemo.java(重点)
需求:从1输出到10,当迭代变量为7,就终止程序
步骤:
(1)循环打印1到10
(2)判断循环次数是否为7,是就终止程序,否则打印循环次数
public class Practice {
public static void main(String[] args) {
// 需求:从1输出到10,当迭代变量为7,就停止循环
// 1.while循环
// 1.1.循环打印1到10
int i = 1;
while (i <= 10) {
// 1.2判断循环次数是否为7,是就停止循环,否则打印循环次数
if (i == 7) {
return;
} else {
System.out.println(i);
}
i++;
}
System.out.println("===================");
// 2.do-while循环
// 2.1循环打印1到10
int j = 1;
do {
// 2.2 判断循环次数是否为7,是就停止循环,否则打印循环次数
if (j == 7) {
return;
} else {
System.out.println(j);
}
j++;
} while (i <= 10);
System.out.println("====================");
// 3.for循环
// 3.1 循环打印1到10
for (int count = 1; count <= 10; count++) {
// 3.2 判断循环次数是否为7,是就停止循环,否则打印循环次数
if (count == 7) {
return;
} else {
System.out.println(count);
}
}
}
}
17. ContinueDemo.java(重点)
需求:从1输出到10,不要输出4
步骤:
(1)循环遍历1到10的数
(2)判断如果i为4,跳过当前循环,否则打印i
public class ContinueDemo {
public static void main(String[] args) {
// 需求: 从1输出到10,不要输出4
// while循环
int i = 1;
while (i <= 10) {
if (i == 4) {
i++;
continue;
}
System.out.println(i);
i++;
}
System.out.println("================");
// do while
int j = 1;
do {
if (j == 4) {
j++;
continue;
}
System.out.println(j);
j++;
} while (j <= 10);
System.out.println("=========");
// for
for (int count = 1; count <= 10; count++) {
if (count == 4) {
continue;
}
System.out.println(count);
}
}
}
二、综合练习(必做):
1. MonthChooseDemo.java
需求:定义一个 int 类型 变量存放当前月份(month),使用 switch 进行判断,例如 3 月到 5 月是打印春季,6 月到 8 月打印夏季,依次类推打印秋季(9 10 11)和冬季(12 1 2),但月份不是 1 月到 12 月,打印月份非法。
知识点:switch,switch的穿透
/**
* 分析:
* 找出题中的知识点,复习
* 1. 变量
* 2. 分支switch
//[1]
* switch(整形表达式(byte short int long char(类整形)){
case 常量值1:{
statement;
break;
}
case 常量值2:{
statement;
break;
}
...
[default:{
statement;
break;
}]
}
[2] 整形表达式(byte short int long char(类整形)
[3] break可以省略,穿透
*/
// 1> 声明一个变量
int month = 13;
// 2> 对月份进行等值匹配分支
switch(month){
case 3:
case 4:
case 5:{
System.out.println("春季");break;
}
case 6:
case 7:
case 8:{
System.out.println("夏季");break;
}
default:{
System.out.println("非法月份");
}
}
shift + tab
import java.util.Scanner;
public class MonthChooseDemo {
public static void main(String[] args) {
// 从控制台中读取输入的数据
Scanner s = new Scanner(System.in);
System.out.println("请输入1~12月份中的任意一月份,判断该月份属于哪个季节?");
// 获取控制台中输入的month的值
int month = s.nextInt();
switch (month) {
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;
}
case 12:
case 1:
case 2: {
System.out.println("当前月份属于冬季");break;
}
default: {
System.out.println("你确定你输入的是1~12月份之中的数字?");break;
}
}
}
}
\