package day03; public class HomeWork2 { public static void main(String[] args) { //1. 声明两个整型变量a和b并分别赋值,找到a和b中的最大值,并输出。用两种方式实现:条件运算符、if..else分支结构 int a = 8, b= 10; double max = a>b?a:b; System.out.println("max="+max); if (a>b) { System.out.println("max="+a); }else{ System.out.println("max="+b); } /* 2. 声明一个整型变量year并存储年份,判断其是平年还是闰年,若是闰年则输出"某某某年是闰年",否则输出"某某某年是平年"。 闰年的判断公式: 1)4年一闰,百年不闰,就是:能被4整除,并且,不能被100整除 或者 2)400年再闰,就是:能被400整除 */ //1) int year = 2012; if (year%4==0 && year%100!=0){ System.out.println(year+"年是闰年"); }else { System.out.println(year+"不是闰年"); } //2) if (year%4==0 && year%100!=0){ System.out.println(year+"年是闰年单不是400年闰年"); }else if (year%400==0){ System.out.println(year+"年是四百年闰年"); }else { System.out.println(year+"不是闰年"); } /* 3. 成绩等级判断: > 注:考虑成绩的合法性:当不合法成绩时,输出"成绩不合法" */ int score1=60; if (score1<0 || score1>100) { System.out.println("不合法"); } else if (score1>=90){ System.out.println("优秀"); }else if (score1>=80){ System.out.println("中等"); }else if (score1>=60){ System.out.println("及格"); }else if(score1<60){ System.out.println("不及格"); } } }
java练习
最新推荐文章于 2024-07-12 15:51:39 发布