1.闰年判断
package test;
import java.util.Scanner;
public class Task02 {
public static void main(String[] args) {
// 声明部分
int year;
String result;
Scanner sc = new Scanner(System.in);
// 输入部分
System.out.print("year = ");
year = sc.nextInt();
// 处理部分
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
result = year + "是闰年。";
} else {
result = year + "是平年。";
}
// 输出部分
System.out.println(result);
}
}
2.计算圆面积(选择结构版)
package test;
import java.util.Scanner;
public class Task02_1 {
public static void main(String[] args) {
double r, s;
final double PI = 3.14;
Scanner sc = new Scanner(System.in);
System.out.print("r = ");
r = sc.nextDouble();
if (r < 0) {
System.out.println("温馨提示,半径不能为负!");
return;
}
s = PI * r * r;
System.out.println("s = " + s);
}
}
3.成绩等级评定
package test;
import java.util.Scanner;
public class Task02_2 {
public static void main(String[] args) {
// 声明部分
int score;
String level;
Scanner sc = new Scanner(System.in);
// 输入部分
System.out.print("score = ");
score = sc.nextInt();
// 处理部分
level = "";
if (score > 100) level = "超出范围";
if (score >= 90 && score <= 100) level = "优秀";
if (score >= 80 && score < 90) level = "良好";
if (score >= 70 && score < 80) level = "中等";
if (score >= 60 && score < 70) level = "及格";
if (score >= 0 && score < 60) level = "不及格";
if (score < 0) level = "超出范围";
// 输出部分
System.out.println(level);
}
}