//第一个类
package problem17;
public class Info {
public String Province;
public int num;
public String name;
/**
*
*/
public Info() {
//super();
// TODO Auto-generated constructor stub
Province=null;
int num=0;
name=null;
}
//导航系统使用第一个类作为对象
package problem17;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Count {
public static int totalNum=0;
public static int male=0;
public static int female=0;
public static Info infma[]=new Info[50];
public static void main(String[] args) throws Exception{
String data="";
do {
data="";
Scanner sn=new Scanner(System.in);
System.out.println("请输入数据以逗号分隔,如“001,张三,男,湖北”,当输入“000”时结束输入");
data=sn.nextLine();
String s[]=data.split(",|,");
String id=s[0];
String name=s[1];
String gender=s[2];
String province=s[3];
if(gender.equals("男"))
{male++;totalNum++;}
else
{female++;totalNum++;}
boolean success=false;
Add(province,name);
}while(!data.equals("000"));
displayAll();
}
public static void displayAll() {
/*
总人数:6人
其中男:2人,33.3%,女:4人,66.7%
学生来自于以下省份:
湖北:2人(张珊,李云)
湖南:1人(张罗)
北京:2人(王思,丽丽)
上海:1人(王杰)
*/
System.out.println("总人数:"+totalNum+"人");
System.out.print("其中男:"+male+"人,"+txfloat(male*100,totalNum)+"%, ");
System.out.println("女:"+female+""+txfloat(female*100,totalNum)+"%");
System.out.println("学生来自于以下省份:");
for(int i=0;!infma[i].equals(null);i++) {
System.out.println(infma[i].Province+":"+infma[i].num+"人"+"("+infma[i].name+")");
}
}
public static void Add(String p,String n) {
int i=0;
while(true) {
if(!infma[i].Province.equals("")&&infma[i].Province!=null) {
if(infma[i].Province.equals(p)) {
infma[i].name+=","+n;
infma[i].num++;
System.out.println("添加信息成功!!");
break;
}}
else if
(infma[i].Province==null) {
infma[i].Province=p;
infma[i].name+=","+n;
infma[i].num++;
System.out.println("添加信息成功!!");
break;
}
i++;
}
}
public static String txfloat(int a,int b) {
DecimalFormat df=new DecimalFormat("00.0");
return df.format((float)a/b);
}
}
}