6-2 人口统计 (5 分)java

6-2 人口统计 (5 分)

本题运行时要求键盘输入10个人员的信息(每一个人信息包括:姓名,性别,年龄,民族),要求同学实现一个函数,统计民族是“汉族”的人数。

函数接口定义:

public static int numofHan(String data[])

其中 data[] 是传入的参数。 data[]中的每一个元素都是一个完整的人员信息字符串,该字符串由“姓名,性别,年龄,民族”,各项之间用英文半角的逗号分隔。函数须返回 值是汉族的人数。

裁判测试程序样例:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final int HUMANNUM=10;
        String persons[]=new String[HUMANNUM];
        Scanner in=new Scanner(System.in);
        for(int i=0;i<persons.length;i++)
            persons[i]=in.nextLine();
        int result=numofHan(persons);
        System.out.println(result);

    }

    /*在此处给出函数numofHan()*/


}

输入样例:

Tom_1,男,19,汉族
Tom_2,女,18,汉族
Tom_3,男,20,满族
Tom_4,男,18,汉族
Tom_5,男,19,汉族人
Tom_6,女,17,汉族
Tom_7,男,19,蒙古族
汉族朋友_1,男,18,汉族
Tom_8,male,19,老外
Tom_9,female,20,汉族

输出样例:

7

 public static int numofHan(String data[]){
      int n=0;

       for(int i=0 ; i<data.length ; i++){
           if(data[i].indexOf("汉族")>=0){//也可以写成data[i].indexOf("汉族",5)>=0
                 n++;
           }
       }

       return n;   
   }

indexOf() 方法有以下四种形式:
public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

详情可戳下面链接去菜鸟教程了解更多:
https://www.runoob.com/java/java-string-indexof.html

### 使用Java开发农村人口统计系统 #### 项目概述 为了实现一个高效的农村人口统计系统,可以采用Java作为主要编程语言。该系统旨在收集、管理和析农村地区的人口数据。通过利用Java的强大功能及其生态系统中的各种框架和技术栈,能够构建出稳定可靠的解决方案。 #### 技术选型 考虑到系统的复杂性和性能需求,建议使用以下技术和框架: - **Spring Boot**: 提供快速搭建微服务架构的能力,并简化了依赖管理。 - **MyBatis**: 负责持久层操作,方便地映射数据库表到实体类之间关系。 - **MySQL/MariaDB**: 关系型数据库管理系统用于存储结构化数据记录。 - **Thymeleaf/Freemarker**: Web模板引擎来渲染前端页面视图。 #### 功能模块划 整个应用程序可以根据业务逻辑划为几个核心部: 1. 用户认证与授权 (Authentication and Authorization) 2. 数据录入界面 (Data Entry Interface) 3. 查询报表生成功能 (Query Report Generation) 以下是各个组件的具体描述以及相应的代码片段展示. --- ##### 1. 用户认证与授权 对于安全性的考虑,在登录环节引入JWT(JSON Web Token),确保每次请求都经过验证才能访问受保护资源。 ```java @RestController @RequestMapping("/auth") public class AuthController { @Autowired private JwtUtil jwtTokenUtil; @PostMapping("/login") public ResponseEntity<?> createAuthenticationToken(@RequestBody LoginRequest loginRequest) throws Exception { final UserDetails userDetails = userDetailsService.loadUserByUsername(loginRequest.getUsername()); if (!passwordEncoder.matches(loginRequest.getPassword(), userDetails.getPassword())) { throw new BadCredentialsException("Invalid password"); } final String token = jwtTokenUtil.generateToken(userDetails); return ResponseEntity.ok(new AuthenticationResponse(token)); } } ``` ##### 2. 数据录入界面 创建RESTful API接口允许客户端提交新的人口统计数据条目至服务器端处理并保存入数据库中。 ```java @PostMapping("/population/add") @ResponseBody public Map<String, Object> addPopulationRecord(@RequestParam(value="name") String name, @RequestParam(value="age") int age, @RequestParam(value="gender") char gender){ Population population = new Population(); population.setName(name); population.setAge(age); population.setGender(gender); try{ populationService.save(population); result.put("status", "success"); }catch(Exception e){ result.put("status", "error"); result.put("message",e.getMessage()); } return result; } ``` ##### 3. 查询报表生成功能 提供API支持按条件筛选查询所需的信息,并导出为Excel表格形式下载。 ```java @GetMapping("/report/export") public void exportToExcel(HttpServletResponse response,@RequestParam(required=false,value="filter")String filterCondition)throws IOException{ List<Population> populations=populationRepository.findAllByFilter(filterCondition); Workbook workbook=new XSSFWorkbook(); Sheet sheet=workbook.createSheet("Population Data"); Row headerRow=sheet.createRow(0); CellStyle cellStyle=workbook.createCellStyle(); // 设置列名... headerRow.createCell(0).setCellValue("Name"); headerRow.createCell(1).setCellValue("Age"); headerRow.createCell(2).setCellValue("Gender"); int rowNum=1; for(Population p :populations){ Row row=sheet.createRow(rowNum++); row.createCell(0).setCellValue(p.getName()); row.createCell(1).setCellValue(p.getAge()); row.createCell(2).setCellValue(String.valueOf(p.getGender())); } ServletOutputStream outputStream=response.getOutputStream(); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"); response.setHeader("Content-Disposition","attachment;filename=data.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.flush(); outputStream.close(); } ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值