题目:从浙江工贸选10个美女,计算10个美女的身高和和体重并计算计算10美女的平均身高和平均体重判断这批美女是否合格, 平均身高>150 平均体重<200
则表示是“是美女!” ,反之表示“不是美女!”。
代码如下(有两种方法):
一:
package com.zhongruan;
import java.util.ArrayList;
import java.util.List;
public class xuanfei{
public static void main(String[] args) {
List<Integer> highs = new ArrayList<>();
List<Integer> wights = new ArrayList<>();
highs.add(180);
highs.add(170);
highs.add(150);
wights.add(50);
wights.add(90);
wights.add(55);
System.out.println(highs);
System.out.println(wights);
//增强for循环
int sumH=0;
for(int h:highs){
sumH=sumH+h;
}
System.out.println(sumH);
int sumW=0;
for(int w:wights){
sumW=sumW+w;
}
System.out.println(sumW);
int avgH=sumH/highs.size();
int avgW=sumW/highs.size();
System.out.println(avgH);
System.out.println(avgW);
// avgH=50
if(avgH>150 && avgW<200 ){
System.out.println("是美女");
}else{
System.out.println("不是美女");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
}
结果如下
二:
package com.zhongruan;
import com.sun.javafx.collections.MappingChange;
import javax.swing.plaf.PanelUI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class mm {
public List getHights() {
List highs = new ArrayList<>();
highs.add(180);//添加身高
highs.add(167);
highs.add(175);
return highs;
}
public List<Integer> getWights() {
List<Integer> Wights = new ArrayList<>();
Wights.add(56);//添加体重
Wights.add(67);
Wights.add(45);
return Wights;
}
public Map<String, Integer> getSumHandSumW(List<Integer> highs, List<Integer> wights) {
int sumH = 0;
for (int h : highs) {
sumH = sumH + h;
}
System.out.println(sumH);
int sumW = 0;
for (int w : wights) {
sumW = sumW + w;
}
System.out.println(sumW);
Map map = new HashMap();
map.put("sgh", sumH);
map.put("tzh", sumW);
return map;
}
public Map<String, Integer> getAvg(Map<String, Integer> map) {
int sumH = map.get("sgh");
int sumW = map.get("tzh");
int avgH = sumH / 3;
int avgW = sumW / 3;
Map<String, Integer> avgMap = new HashMap<>();
avgMap.put("pjsg", avgH);
avgMap.put("pjtz", avgW);
return avgMap;
}
public String getResult(Map<String, Integer> avgM) {
int avgH = avgM.get("pjsg");
int avgW = avgM.get("pjtz");
if (avgH > 150 && avgW < 100) {
return "是美女";
} else {
return "不是美女";
}
}
public static void main(String[] args) {
mm mm = new mm();
List<Integer> hights = mm.getHights();
List<Integer> wights = mm.getWights();
Map<String, Integer> map = mm.getSumHandSumW(hights, wights);
Map<String, Integer> avgM = mm.getAvg(map);
String s = mm.getResult(avgM);
System.out.println(s);
}
}