一、知识点题目
IfDemo1.java(重点)
需求:演示if语句的使用
步骤:
(1)定义一个整数类型的变量a
(2)判断a大于5,大于打印“a大于5”
(3)判断a大于20,大于打印“a大于20”
public class IfDemo1{public static void main(String[] args){int a = 6;if (a > 5) {System.out.println("a大于5");} else if (a > 20) {System.out.println("a大于20");}}}
IfElseDemo.java(重点)
需求:演示if-else语句的使用
步骤:
(1)定义一个整数类型的变量a
(2)判断a是奇数还是偶数,如果是偶数打印“a是偶数”,否则打印“a是奇数”
public class IfElseDemo {public static void main(String[] args) {int a = 12;if (a % 2 == 0) {System.out.println("a是偶数");} else {System.out.println("a是奇数");}}}
IfElseIfElseDemo.java(重点)
需求:演示if-else if-else语句的使用
步骤:
(1)定义整数类型的两个数a和b
(2)判断a和b的大小关系
public class IfElseIfElseDemo{public static void main(String[] args) {int a = 2;int b = 5;if (a > b) {System.out.println("a大于b");} else if (a == b) {System.out.println("a等于b");} else {System.out.println("a小于b");}}}
IfElseIfElseDemo2.java
需求:根据输入的一个数字,判断是星期几?
步骤:
(1)定义一个整数类型的变量weekday
(2)判断变量weekday,如果weekday为1输出“周一”,如此类推
public class IfElseIfElseDemo2 {public static void main(String[] args) {int weekday = 1;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("周日");}}}
IfElseIfElseDemo3.java
需求:根据QQ在线的天数来判断QQ的等级
步骤:
(1)定义一个变量days,表示天数
(2)判断天数范围
public class IfElseIfElseDemo3 {public static void main(String[] args) {int days = 1;if (days <= 5 && days >= 0) {System.out.println("无等级");}if (days > 5) {System.out.println("一等级");}if (days > 15) {System.out.println("二等级");}if (days > 25) {System.out.println("三等级");}if (days > 30) {System.out.println("四等级");}}}
SwitchDemo.java
需求:使用switch来完成,根据输入的一个数字,判断是星期几?
步骤:
(1)定义一个整数类型的变量weekday
(2)判断变量weekday,如果weekday为1输出“周一”,如此类推
public class SwitchDemo {public static void main(String[] args) {int weekday = 1;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("不合逻辑");break;}}}
WhileDemo.java(重点)
需求:使用while循环,打印10次帅哥
步骤:
(1)定义一个变量count,表示计数器
(2)使用while循环,判断count是否小于10
(3)打印“帅哥”和循环次数
(4)计数器+1
public class WhileDemo {public static void main(String[] args) {int count = 1;while (count < 10) {System.out.println("帅哥"+count);count++;}}}
WhileDemo2.java
需求:打印从1到100的数
步骤:
(1)定义一个变量number
(2)循环打印number
public class WhileDemo2 {public static void main(String[] args) {int number = 1;while (number <= 100) {System.out.println(number);number++;}}}
WhileDemo3.java
需求:计算100以内的正整数和
步骤:
(1)定义一个变量number,表示加数
(2)定义一个变量sum,表示前N个数之和
(3)使用while循环,每循环一次sum和当前的number相加一次
(4)number自增1
public class WhileDemo3 {public static void main(String[] args) {int number = 1;int sum = 1;while (number <= 100) {sum += number;number++;System.out.println(sum);}}}
二、综合练习(必做):
PageDemo.java
需求:
(1)一共有55条数据,每页显示10条数据,当前页为2,请求出上一页
(2)一共有55条数据,每页显示10条数据,当前页为2,请求出下一页
知识点: 三元运算符
说明:
总页数: 如果总条数和页面显示的条数相除, 能除尽则取商, 否则取商加1作为总页数
当前页是未知数, 可能是第1页到最后一页的任何页
上一页: 如果当前页是第1页, 则上一页还是1, 否则当前页减1
currentPage == 1 ? 1 : currrentPage -1下一页:如果当前页为最后一页,下一页为 最后一页, 否则下一页为 当前页加1
public class PageDemo{public static void main(String[] args) {int currentPage = 2;int perviousPage, nextPage;perviousPage = currentPage == 1 ? 1 : currentPage - 1;nextPage = currentPage == 1 ? 1 : currentPage + 1;System.out.println("上一页 " + perviousPage);System.out.println("下一页 " + nextPage);}}
MonthChooseDemo.java
需求:定义一个 int 类型 变量存放当前月份(month),使用 switch 进行判断,例如 3 月到 5 月是打印春季,6 月到 8 月打印夏季,依次类推打印秋季和冬季,但月份不是 1 月到 12 月,打印月份非法。
知识点:switch,switch的穿透
public class MonthChooseDemo {public static void main(String[] args) {int month = 1;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("不合逻辑");break;}}}
EvenDemo.java
需求:找出 [1, 500] 之间偶数的个数。
步骤:
(1)遍历1到500的数字
(2)判断是否是偶数,能被2整除的就是偶数
(3)定义变量count作为计数器,如果为偶数,count加1.
知识点:循环语句,选择语句,计数器
public class EvenDemo {public static void main(String[] args) {int a = 1;int count = 0;while (a <= 500) {if (a >= 0 && a <= 500 && a % 2 == 0) {count++;}a++;}System.out.println("偶数有" + count);}}
SevenDemo.java
需求:求 [1, 500] 之间能整除 7 的数的总和。
思路:
(1)遍历1-500的数
(2)定义一个变量sum,表示能被7整除的数的和
(3)判断当前的数能被7整除,变量sum和当前遍历的数进行相加
知识点:循环语句,选择语句,求和
public class a {public static void main(String[] args) {int a = 1;int sum = 0;while (a <= 500) {if (a >= 1 && a <= 500 && a % 7 == 0) {sum += a;}a++;} System.out.println("总和是 " + sum);}}
