1. 键盘输入五位数字的会员卡号:38503

使用“/”和“%”运算符分解获得的会员卡每一位上的数字
将每一位数字求和
image.png

  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void fun(){
  5. System.out.println("请输入五位会员卡号:");
  6. Scanner scanner = new Scanner(System.in);
  7. int num;
  8. do{
  9. num = scanner.nextInt();
  10. if(num % 100000 !=0){
  11. System.out.println("输入有误!");
  12. continue;
  13. }else{
  14. break;
  15. }
  16. }while(true);
  17. int num1 = num/10000;
  18. int num2 = num/1000%10;
  19. int num3 = num/100%10;
  20. int num4 = num/10%10;
  21. int num5 = num%10;
  22. System.out.println("万位数:"+num1);
  23. System.out.println("千位数:"+num2);
  24. System.out.println("百位数:"+num3);
  25. System.out.println("十位数:"+num4);
  26. System.out.println("个位数:"+num5);
  27. }
  28. public static void main(String[] args) {
  29. // write your code here
  30. fun();
  31. }
  32. }

2. 键盘输入学生学号:20161103(2016 年+11 期+03 号)

使用“/”和“%”运算符分解学生学号获得年份期数和序号
并且输出
学生学号:20161103
学生入学年份:2016 年
学生期数:11 期
学生序号:03 号

  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void fun(){
  5. System.out.println("请输入学生学号:");
  6. Scanner scanner = new Scanner(System.in);
  7. int num = scanner.nextInt();
  8. int num1 = num/10000;
  9. int num2 = num/100%100;
  10. int num3 = num%100;
  11. System.out.println("学生学号:"+num);
  12. System.out.println("学生入学年份:"+num1);
  13. System.out.println("学生期数:"+num2);
  14. System.out.println("学生序号:"+num3);
  15. }
  16. public static void main(String[] args) {
  17. // write your code here
  18. fun();
  19. }
  20. }

3. 编写万年历小程序

image.png

  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static boolean isLeapYear(int year){
  5. boolean flag1 = (year%4==0);
  6. boolean flag2 = (year%100==0);
  7. boolean flag3 = (year%400==0);
  8. if((flag1&&!flag2)||(flag3)){
  9. return true;
  10. }
  11. return false;
  12. }
  13. private static int getDays(int month,boolean isLeap){
  14. int days = 30;
  15. switch (month){
  16. case 1:
  17. case 3:
  18. case 5:
  19. case 7:
  20. case 8:
  21. case 10:
  22. case 12:
  23. days++;
  24. break;
  25. case 4:
  26. case 6:
  27. case 9:
  28. case 11:
  29. days = 30;
  30. break;
  31. case 2:
  32. days = 28;
  33. if(isLeap)days++;
  34. break;
  35. }
  36. return days;
  37. }
  38. public static void fun(){
  39. System.out.println("请输入年:");
  40. Scanner scanner = new Scanner(System.in);
  41. int year = scanner.nextInt();
  42. System.out.println("请输入月:");
  43. int month = scanner.nextInt();
  44. boolean isLeap = isLeapYear(year);
  45. String string1 = isLeap ? "闰年" : "平年";
  46. System.out.println(year+"年是"+string1);
  47. int days = getDays(month,isLeap);
  48. System.out.println(year+"年"+month+"月天数:"+days);
  49. }
  50. public static void main(String[] args) {
  51. // write your code here
  52. fun();
  53. }
  54. }

4.循环录入 5 个学生 Java 课的学生成绩,统计分数大于等于 80 分的学生比例

image.png

  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void fun(){
  5. int count = 0;
  6. for(int i = 1 ; i <= 5 ; i++){
  7. System.out.println("请输入第"+i+"位同学的成绩");
  8. Scanner input = new Scanner(System.in);
  9. int n = input.nextInt();
  10. if(n>=80){
  11. count++;
  12. }
  13. }
  14. System.out.println("超过80分以上的学生人数:"+count);
  15. System.out.println("超过80分以上的学生比例:"+count*100/5+"%");
  16. }
  17. public static void main(String[] args) {
  18. // write your code here
  19. fun();
  20. }
  21. }