写一段根据数据统计奥运男女比例的java代码
时间: 2023-03-13 14:19:25 浏览: 152
public class Olympics {
public static void main(String[] args) {
int maleCount = 0;
int femaleCount = 0;
int totalCount = 100;
// 遍历所有选手
for (int i = 0; i < totalCount; i++) {
// 根据选手性别分别计数
if (isMale(i)) {
maleCount++;
} else {
femaleCount++;
}
}
// 计算男女比例
float maleRate = (float)maleCount / totalCount;
float femaleRate = (float)femaleCount / totalCount;
// 输出男女比例
System.out.println("男性比例: " + maleRate);
System.out.println("女性比例: " + femaleRate);
}
// 判断选手是否为男性
private static boolean isMale(int i) {
// 省略具体判断逻辑
return true;
}
}
相关问题
写一段根据数据统计奥运男女比例的hadoop代码
示例代码:
val dataRDD = sc.textFile("olympic_data.csv")
val maleRatio = dataRDD.map(line => line.split(",")).filter(x => x(2).equals("M")).count() / dataRDD.count()
println("比例:" + maleRatio)
阅读全文