1 if-else结构

如何从键盘获取不同类型的变量:需要使用Scanner类
具体实现步骤:
1、导包:import java.util.Scanner;
2、Scanner的实例化:Scanner sc = new Scanner(System.in);
3、调用Scanner的相关方法,来获取指定类型的变量

  1. System.out.println("小鹏的期末成绩是(0-100):");
  2. Scanner sc = new Scanner(System.in);
  3. int score = sc.nextInt();
  4. if (score == 100) {
  5. System.out.println("奖励一台BMW");
  6. } else if (score > 80 & score <= 99) {
  7. System.out.println("奖励一台iPhone");
  8. } else if (score >= 60 & score <= 80) {
  9. System.out.println("奖励一台iPad");
  10. } else {
  11. System.out.println("没有啥");
  12. }

课后练习题

  1. int x = 4;
  2. int y = 1;
  3. if (x > 2) {
  4. if (y > 2)
  5. System.out.println(x + y);
  6. System.out.println("atguigu");
  7. } else
  8. System.out.println("x is " + x);
  9. 程序结果输出为:atguigu
  10. 因为if处没有{}所以只会认为仅有 System.out.println(x + y);这一行代码。
  11. int x = 4;
  12. int y = 1;
  13. if (x > 2)
  14. if (y > 2)
  15. System.out.println(x + y);
  16. //System.out.println("atguigu");
  17. else//就近原则,与最近的if配对
  18. System.out.println("x is " + x);
  19. 程序结果输出为:4
  1. boolean b = true;
  2. if(b == false)
  3. System.out.println("a");
  4. else if(b)
  5. System.out.println("b");
  6. else if(!b)
  7. System.out.println("c");
  8. else
  9. System.out.println("d");
  10. 程序结果输出为:b
  11. //如果写成if(b=false)能编译通过吗?如果能,结果是?
  12. 程序结果输出为:c

注意:if-else语句中范围大的写在前,范围小的写在后。
if-else语句可以嵌套,

  1. package com.atguigu.study;
  2. public class Practice5 {
  3. public static void main(String[] args) {
  4. /*
  5. 编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行
  6. 上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印
  7. 输出“baz”。
  8. */
  9. for (int i = 1; i <= 150; i++) {
  10. System.out.print(i + " ");
  11. if (i % 3 == 0) {
  12. System.out.print("foo ");
  13. }
  14. if (i % 5 == 0) {
  15. System.out.print("biz ");
  16. }
  17. if (i % 7 == 0) {
  18. System.out.print("baz ");
  19. }
  20. System.out.println();
  21. }
  22. }
  23. }

2 switch-case

QQ截图20211126214012.png
1、break的作用是跳出switch-case结构。
2、根据switch-case表达式的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应的case结构中,调用其执行语句。当调用完执行语句后,则继续向下执行其他case结构的执行语句,直到遇到break或结构末尾才结束。
3、switch结构中的表达式,只能是如下的6种数据类型之一:byte、short、char、int、枚举类型(jdk 5.0)、String类型 (jdk 7.0)(重要)
4、case子句中的值必须是常量,不能是变量名或不确定的表达式值。
5、default子句是可任选的,相当于else。同时,位置是灵活的。当没有匹配的case时,执行default。

switch和if语句的对比
if和switch语句很像,具体什么场景下,应用哪个语句呢?
如果判断的具体数值不多,而且符合byte、short 、char、int、String、枚举等几种类型。虽然两个语句都可以使用,建议使用swtich语句。因为效率稍高。
其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广。也就是说,使用switch-case的,都可以改写为if-else。反之不成立。

  1. package com.atguigu.study;
  2. import java.util.Scanner;
  3. public class Practice4 {
  4. public static void main(String[] args) {
  5. /*
  6. 编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期,
  7. 每年用一个动物代表:rat、ox、tiger、rabbit、dragon、snake、horse、sheep、monkey、
  8. rooster、dog、pig。
  9. 提示:2019年:猪 2019 % 12 == 3
  10. */
  11. Scanner sc = new Scanner(System.in);
  12. System.out.println("请输入年份:");
  13. int year = sc.nextInt();
  14. switch (year%12){
  15. case 0:
  16. System.out.println("猴");
  17. break;
  18. case 1:
  19. System.out.println("鸡");
  20. break;
  21. case 2:
  22. System.out.println("狗");
  23. break;
  24. case 3:
  25. System.out.println("猪");
  26. break;
  27. case 4:
  28. System.out.println("鼠");
  29. break;
  30. case 5:
  31. System.out.println("牛");
  32. break;
  33. case 6:
  34. System.out.println("虎");
  35. break;
  36. case 7:
  37. System.out.println("兔");
  38. break;
  39. case 8:
  40. System.out.println("龙");
  41. break;
  42. case 9:
  43. System.out.println("蛇");
  44. break;
  45. case 10:
  46. System.out.println("马");
  47. break;
  48. case 11:
  49. System.out.println("羊");
  50. break;
  51. default:
  52. System.out.println("输入有问题");
  53. break;
  54. }
  55. }
  56. }
  1. package com.atguigu.study;
  2. import java.util.Scanner;
  3. public class Practice3 {
  4. public static void main(String[] args) {
  5. /*
  6. 从键盘分别输入年、月、日,判断这一天是当年的第几天
  7. 注:判断一年是否是闰年的标准:
  8. 1)可以被4整除,但不可被100整除
  9. 或 2)可以被400整除
  10. */
  11. Scanner sc = new Scanner(System.in);
  12. System.out.println("请输入year:");
  13. int year = sc.nextInt();
  14. System.out.println("请输入month:");
  15. int month = sc.nextInt();
  16. System.out.println("请输入day:");
  17. int day = sc.nextInt();
  18. int sumDays = 0;
  19. switch (month) {
  20. case 12:
  21. sumDays += 31;
  22. case 11:
  23. sumDays += 31;
  24. case 10:
  25. sumDays += 30;
  26. case 9:
  27. sumDays += 31;
  28. case 8:
  29. sumDays += 31;
  30. case 7:
  31. sumDays += 30;
  32. case 6:
  33. sumDays += 31;
  34. case 5:
  35. sumDays += 30;
  36. case 4:
  37. sumDays += 31;
  38. case 3:
  39. //sumDays+=28;
  40. if (year % 4 == 0 && year % 100 != 0) {
  41. sumDays += 29;
  42. } else {
  43. sumDays += 28;
  44. }
  45. case 2:
  46. sumDays += 31;
  47. case 1:
  48. sumDays += day;
  49. break;
  50. default:
  51. System.out.println("输入的月份有问题");
  52. break;
  53. }
  54. }
  55. }

3 循环结构

循环语句分类 :
for 循环
while 循环
do-while 循环
QQ截图20211126230843.png

3.1 for循环

语法格式
for (①初始化部分; ②循环条件部分; ④迭代部分){
③循环体部分;

执行过程:
①-②-③-④-②-③-④-②-③-④-…..-②
说明:
②循环条件部分为boolean类型表达式,当值为false时,退出循环
①初始化部分可以声明多个变量,但必须是同一个类型,用逗号分隔
④可以有多个变量更新,用逗号分隔

  1. for(int i = 1;i<5;i++){
  2. System.out.println("1");
  3. }//int i,只在for中有效。
  1. package com.atguigu.study;
  2. import java.util.Scanner;
  3. public class Practice6 {
  4. public static void main(String[] args) {
  5. /*
  6. 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
  7. 比如:12和20的最大公约数是4,最小公倍数是60。
  8. */
  9. Scanner sc = new Scanner(System.in);
  10. System.out.println("请输入正整数m:");
  11. int m = sc.nextInt();
  12. System.out.println("请输入正整数n:");
  13. int n = sc.nextInt();
  14. int minBeiShu = m * n;
  15. if (m > n) {
  16. for (int yuShu = m % n; yuShu != 0; ) {
  17. m = n;
  18. n = yuShu;
  19. yuShu = m % n;
  20. }
  21. System.out.println("最大公约数是:" + n);
  22. System.out.println("最小公倍数是:"+(minBeiShu/n));
  23. } else {
  24. int temp = 0;
  25. temp = m;
  26. m = n;
  27. n = temp;
  28. for (int yuShu = m % n; yuShu != 0; ) {
  29. m = n;
  30. n = yuShu;
  31. yuShu = m % n;
  32. }
  33. System.out.println("最大公约数是:" + n);
  34. System.out.println("最小公倍数是:"+(minBeiShu/n));
  35. }
  36. }
  37. }
  38. //我写的,取巧了
  1. package com.atguigu.study;
  2. import java.util.Scanner;
  3. /*
  4. 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
  5. 比如:12和20的最大公约数是4,最小公倍数是60。
  6. 说明:break关键字的使用
  7. */
  8. public class Practice9 {
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. System.out.println("请输入第一个正整数:");
  12. int m = sc.nextInt();
  13. System.out.println("请输入第二个正整数:");
  14. int n = sc.nextInt();
  15. int min = (m <= n) ? m : n;
  16. for (int i = min; i > 1; i--) {
  17. if (m % i == 0 && n % i == 0) {
  18. System.out.println("最大公约数:" + i);
  19. break;
  20. }
  21. }
  22. int max = (m >= n) ? m : n;
  23. for (int i = max; i <= m*n; i++) {
  24. if (i % m == 0 && i % n == 0) {
  25. System.out.println("最小公倍数:" + i);
  26. break;
  27. }
  28. }
  29. }
  30. }
  31. //老师写的
  1. package com.atguigu.study;
  2. /*
  3. 输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数
  4. 字立方和等于其本身。
  5. 例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
  6. */
  7. public class Practice10 {
  8. public static void main(String[] args) {
  9. System.out.println("水仙花数有:");
  10. for (int i = 100; i <= 999; i++) {
  11. int iBai = i / 100;
  12. int iGe = i % 10;
  13. int iShi = i / 10 % 10;
  14. if (iBai * iBai * iBai + iGe * iGe * iGe + iShi * iShi * iShi == i) {
  15. System.out.println(i);
  16. }
  17. }
  18. }
  19. }

3.2 while

语法格式
①初始化部分
while(②循环条件部分){
③循环体部分;
④迭代部分;
}
执行过程:
①-②-③-④-②-③-④-②-③-④-…-②
说明:
注意不要忘记声明④迭代部分。否则,循环将不能结束,变成死循环。
for循环和while循环可以相互转换。
区别:for循环和while循环的初始化条件部分的作用范围不同。

3.2 do-while

语法格式
①初始化部分;
do{
③循环体部分
④迭代部分
}while(②循环条件部分);
执行过程:
①-③-④-②-③-④-②-③-④-…②
说明:
do-while循环至少执行一次循环体。