java之嵌套if的问题,

预赛成绩大于 80 分的可进入决赛,低于80分进入女子复赛,低于60分淘汰。然后根据性别再划分为男子组决赛和女子组决赛。
运行结果为: 进入女子组决赛。

package com.golden.servlet.util;

public class Test1 {

    public static void main(String[] args) {
          System.out.println(compute(81, "女"));
    }

    public static String compute(int score, String sex) {
        if (score < 60) {
            return "淘汰";
        }
        if (score < 80) {
            return "进入女子复赛";
        }
        if (score > 80) {
            if (sex.equals("男")) {
                return "进入男子组决赛";
            }
            return "进入女子组决赛";
        }
        throw new RuntimeException("其它未定义规则");
    }

}

 

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-23
for(遍历){//对象e 属性score分数 sex性别 flag分组标志
if(e.score>=80)
e.flag=1;//flag为1标识进决赛

else{ //score<80
if(e.score<60)
{
e.flag=3;//flag=3表示淘汰
}
else
{
e.flag=2;//60=<score<80 进入复赛
}
}
}
for(遍历){
if(e.flag==1 && e.sex=="男")
list1.add(e);
if(e.flag==1 && e.sex=="女")
list2.add(e); //list1 存储男子组决赛 list2存储女子组决赛

}