新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java作业4--小孩名字流行度排名(简单实现)-创新互联

如图所示 是我们的题目要求 

为台儿等地区用户提供了全套网页设计制作服务,及台儿网站建设行业解决方案。主营业务为成都网站设计、成都做网站、台儿网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

有两个附件是名字流行度排名

接下来是我们的代码实现

这里我使用的是逐行读入文件内容,再利用字符串的spilt方法对字符串按“,”进行分割,取出名字和性别进行比较。这里的比较我用的是equalIgnorance方法,忽略大小写比较。

import java.io.*;
import java.util.Scanner;

public class name_popularity {
    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the year");
        int year= sc.nextInt();

        System.out.println("Enter the gender");
        String gender=sc.next();

        System.out.println("Enter the name");
        String name=sc.next();

        switch (year)
        {
            case 2020:
            {    //首先定义一个文件对象 路径自己修改到自己的文件地址
                File f1=new File("yob2020.txt");
                BufferedReader bufferedReader = new BufferedReader(new FileReader(f1));
                //每一行的内容
                String strLine = null;
                int lineCount = 1;
                while(null != (strLine = bufferedReader.readLine()))
                {
                    //获取名字 进行比较 利用“,”进行分割 只对名字和性别进行比较
                    String strname=strLine.split(",")[0];
                    //获取性别
                    String strsex=strLine.split(",")[1];
                    if (strname.equalsIgnoreCase(name)&&strsex.equalsIgnoreCase(gender))
                    {
                        System.out.println(name+" is rankded "+ lineCount+" in year 2020");
                        return;
                    }
                    //读取下一行
                    lineCount++;
                }
                System.out.println(name+" is not ranked in year 2020");
                break;
            }
            case 2021:
            {
                //首先定义一个文件对象 
                File f1=new File("yob2021.txt");
                BufferedReader bufferedReader = new BufferedReader(new FileReader(f1));
                //每一行的内容
                String strLine = null;
                int lineCount = 1;
                while(null != (strLine = bufferedReader.readLine()))
                {
                    //获取名字 进行比较
                    String strname=strLine.split(",")[0];
                    //获取性别
                    String strsex=strLine.split(",")[1];
                    if (strname.equalsIgnoreCase(name)&&strsex.equalsIgnoreCase(gender))
                    {
                        System.out.println(name+" is rankded "+ lineCount+" in year 2021");
                        return;
                    }
                    //读取下一行
                    lineCount++;
            }
                System.out.println(name+" is not ranked in year 2021");
                break;
        }
    }

}
}

最后是运行效果展示:

是可以很好的完成题目的要求 本次题目也就完成了 

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页名称:java作业4--小孩名字流行度排名(简单实现)-创新互联
转载注明:http://www.wjwzjz.com/article/cojsgh.html
在线咨询
服务热线
服务热线:028-86922220
TOP