(1)简单的成绩划分
package cn.itcast.day;
import java.util.Scanner;
public class Demo9{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入考试分数:");
double score = sc.nextDouble();
char grade;
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
System.out.println("你的成绩是:" + grade);
}
}
运行结果如下:
请输入考试分数:
78
你的成绩是:C
Process finished with exit code 0
(2)简单的闰年、平年判断
package cn.itcast.day;
import java.util.Scanner;
public class Demo9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
// 判断年份能否被4整除
boolean isLeapYear = (year % 4 == 0);
// 年份能被4整除,并且不能被100整除并且使用&&(and)
isLeapYear = isLeapYear && (year % 100 != 0);
// 年份或者能够被400整除
isLeapYear = isLeapYear || (year % 400 == 0);
if (isLeapYear) {
System.out.println(year + "是闰年!");
}else{
System.out.println(year+"不是闰年");
}
// 简写格式;
/*if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "是闰年!");
}*/
}
}
运行结果为:
请输入年份:
2000
2000是闰年!
Process finished with exit code 0