当前位置:   article > 正文

Java初学者练习题_java入门练习

java入门练习

1.编写一个应用程序,使用if else语句判断某年份为闰年

通用规则:

能被4整除并且不能被100整除 或者能被400整除的年份--->闰年

  1. int year = 2000;
  2. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  3. System.out.println(year + "它是一个闰年");
  4. } else {
  5. System.out.println(year + "它是一个平年");
  6. }

 2.编写一个应用程序,使用switch case计算某天是该年中的第几天

  把该天之前的月份天数累加起来+今天的日子就是某天是该年中的第几天

  1. Scanner scanner = new Scanner(System.in);
  2. System.out.println("请输入年份:");
  3. int year = scanner.nextInt();
  4. System.out.println("请输入月份:");
  5. int month = scanner.nextInt();
  6. System.out.println("请输入日期:");
  7. int day = scanner.nextInt();
  8. int sum = 0;
  9. switch (month) {
  10. case 12: sum +=30;
  11. case 11: sum +=31;
  12. case 10: sum +=30;
  13. case 9: sum +=31;
  14. case 8: sum +=31;
  15. case 7: sum +=30;
  16. case 6: sum +=31;
  17. case 5: sum +=30;
  18. case 4: sum +=31;
  19. case 3: sum +=28;
  20. case 2: sum +=31;
  21. case 1: sum +=day;
  22. default:
  23. break;
  24. }
  25. System.out.println(year+"年"+month+"月"+day+"日");
  26. System.out.println("是"+year+"年的第"+sum+"天!");

 3.编写一个应用程序,1 1 2 3 5 8 13...对以上一组数字找规律,找出1000以内都有哪些数字

  1. int i = 1;
  2. int j = 1;
  3. while (i<1000){
  4. System.out.println(i+" ");
  5. i+=j;
  6. j=i-j;
  7. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/492826
推荐阅读
相关标签
  

闽ICP备14008679号