例一:
- /*
- int a = ..
- int b = ..
- int c = ...
- 求3个数字中的最大值
- */
- public class T1
- {
- public static void main(String[] args)
- {
- int a = 5;
- int b = 10;
- int c = 6;
- int m = a;
- if(b>m) m = b;
- if(c>m) m = c;
- System.out.println(m);
- }
- }
例二:
- /*
- int year = 1990;
- 求是否为闰年
- */
- public class T2
- {
- public static void main(String[] args)
- {
- int year = 2000;
- boolean t = false;
- if(year%4==0) t = true;
- if(year%100==0) t = false;
- if(year%400==0) t = true;
- System.out.println(t);
- }
- }
例三:
- /*“评级”算法
- 某小学要求不能给学生打具体分数,而是给一个评级。
- 当然,这个“评级”也是根据分数计算出来的。
- 规则:
- 设百分制的分数为 n
- 则根据 n 的范围:
- 90-100: 优秀
- 80-89: 良好
- 70-79: 正常
- 60-69: 合格
- 0-59: 加油
- 已经知道了分数 n, 请计算“评级”
- 注意:不允许使用else语句,当然也不能使用 switch,因为题目的目的是训练假设修正法。*/
- public class Homework3 {
- public static void main(String[] args) {
- System.out.println("请输入成绩:");
- double score;
- Scanner scan = new Scanner(System.in);
- score = scan.nextDouble();
- String level = "优秀";
- if(score <= 89 ){
- level = "良好";
- }
- if(score <= 79 ){
- level = "正常";
- }
- if(score <= 69 ){
- level = "合格";
- }
- if(score <= 59 ){
- level = "加油";
- }
- System.out.println(level);
- }
- }