要求
a.选10个女生
b.计算10个美女的身高和 和 体重和
c.计算10美女的平均身高和平均体重
d. 判断这批美女是否合格 平均身高>=150 平均体重<=200
List
package beautifulgirl;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.ArrayList;
import java.util.List;
public class Tenbeautifulgirllist {
public static void main(String[] args){
List<Integer>height=new ArrayList<>();
List<Integer>weight=new ArrayList<>();
height.add(150);
height.add(160);
height.add(145);
height.add(170);
height.add(160);
height.add(180);
height.add(147);
height.add(157);
height.add(156);
height.add(160);
weight.add(100);
weight.add(110);
weight.add(100);
weight.add(90);
weight.add(130);
weight.add(100);
weight.add(110);
weight.add(89);
weight.add(99);
weight.add(120);
System.out.println(height);
System.out.println(weight);
int sunh=0;
for (int h:height){
sunh+=h;
}
int sumw=0;
for (int w:weight){
sumw+=w;
}
int avgh=sunh/height.size();
int avgw=sumw/weight.size();
System.out.println("十个女生的平均身高为%d(cm):"+avgh);
System.out.println("十个女生的平均体重为%d(500g):"+avgw);
if (sunh/height.size()>=150 && sumw/weight.size()<=200){
System.out.printf("都是美女");
}
else {System.out.printf("都是丑女");}
}
}
List进化版
package beautifulgirl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TenBeautifulGirlListFenshan {
public List<Integer> getHeight() {
List<Integer> height = new ArrayList<>();
height.add(150);
height.add(160);
height.add(145);
height.add(170);
height.add(160);
height.add(180);
height.add(147);
height.add(157);
height.add(156);
height.add(160);
return height;
}
public List<Integer> getWeight() {
List<Integer> weight = new ArrayList<>();
weight.add(100);
weight.add(110);
weight.add(100);
weight.add(90);
weight.add(130);
weight.add(100);
weight.add(110);
weight.add(89);
weight.add(99);
weight.add(120);
return weight;
}
public Map<String,Integer> getSumhAndSumw(List<Integer>height,List<Integer>weight){
int Sumh=0;
for (int h:height){
Sumh+=h;
}
int Sumw=0;
for (int w:weight){
Sumw+=w;
}
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/10;
int avgw=sumw/10;
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<=200){
return "都是美女";
}
else {return "都是丑女";}
}
public static void main(String[] args) {
TenBeautifulGirlListFenshan tenBeautifulGirlListFenshan=new TenBeautifulGirlListFenshan();
List<Integer>height=tenBeautifulGirlListFenshan.getHeight();
List<Integer>weight=tenBeautifulGirlListFenshan.getWeight();
Map<String,Integer> map=tenBeautifulGirlListFenshan.getSumhAndSumw(height,weight);
Map<String,Integer> avgM=tenBeautifulGirlListFenshan.getAvg(map);
String s=tenBeautifulGirlListFenshan.getResult(avgM);
System.out.println(s);
}
}
Scanner版
package beautifulgirl;
import java.util.Scanner;
public class tenbeautifulgirl {
public static void main(String[] args){
float[] heights=new float[10];
float[] weights=new float[10];
for (int i=1;i<11;i++){
Scanner xuanba=new Scanner(System.in);
System.out.printf("请输入%d号美女的身高(cm)",i);
int height=xuanba.nextInt();
System.out.printf("请输入%d号美女的体重(500g)",i);
int weight=xuanba.nextInt();
heights[i-1]=height;
weights[i-1]=weight;
}
float he=0;
float we=0;
for (int j=0;j<10;j++){
he+=heights[j];
we+=weights[j];
}
System.out.printf("10个美女身高的平均值是%f\n",he/10);
System.out.printf("10个美女体重的平均值是%f\n",we/10);
if (he/10>=150 && we/10<=200){
System.out.printf("这一批全是美女");
}
else {System.out.printf("这一批全是丑女");}
}
}