zoukankan      html  css  js  c++  java
  • 动手动脑(二)

    动手动脑:

    一、编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

    程序源代码:

    package math;

    public class Do

    {

    public static void main(String[] args)

    {

    int []a=new int[2000];

    a[0]=(int)(Math.random()*1000);

    for(int i=0;i<1000;i++)

    {

    a[i+1]=(int) ((16807*a[i]+0)%(Math.pow(2,31)-1));

    System.out.println(a[i]);

    }

    }

    }

    运行截图:
    二、请看以下代码,你发现了有什么特殊之处吗?

    public class MethodOverload {

    public static void main(String[] args) {

    System.out.println("The square of integer 7 is " + square(7));

    System.out.println("\nThe square of double 7.5 is " + square(7.5));

    }

    public static int square(int x) {

    return x * x;

    }

    public static double square(double y) {

    return y * y;

    }

    }

    上述示例代码展示了Java的“方法重载(overload)”特性。

    满足以下条件的两个或多个方法构成“重载”关系:

    1)方法名相同;

    2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

    注意:方法的返回值不作为方法重载的判断条件。

    三、用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《飘》 中最常用的短语是什么,等等。

    要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。

    package math;

    import java.io.BufferedReader;

    import java.io.File;

    import java.io.FileReader;

    import java.io.FileNotFoundException;

    public class StringJun {

    public static void main(String[] args)

    {

    File file =new File("C:/Users/香蕉皮/Desktop/wo.txt");

    try

    {

    FileReader fr=new FileReader(file);

    BufferedReader br=new BufferedReader(fr);

    String s=null;

    String tmp=null;

    int i=0;

    while((s=br.readLine())!=null)

    {

    tmp+=s;

    }

    String []str1=tmp.split("[^a-zA-Z\']+");

    int []sum=new int[str1.length];

    for(int j=0;j<str1.length;j++)

    {

    sum[j]=1;

    for(int k=j+1;k<str1.length;k++)

    {

    if(str1[j].equalsIgnoreCase(str1[k]))

    {

    sum[j]++;

    }

    }

    }

    int t=0,max=sum[0];

    for(int m=1;m<str1.length;m++)

    {

    if(sum[m]>max)

    {

    t=m;

    max=sum[m];

    }

    }

    System.out.println(max+" "+str1[t]);

    br.close();

    fr.close();

    }catch(Exception e){

    e.printStackTrace();

    }

    }

    }

    用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《飘》 中最常用的短语是什么,等等。

    要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。

    package math;

    import java.io.BufferedReader;

    import java.io.BufferedWriter;

    import java.io.File;

    import java.io.FileReader;

    import java.io.FileWriter;

    import java.util.ArrayList;

    import java.util.Comparator;

    import java.util.HashMap;

    import java.util.Hashtable;

    import java.util.List;

    import java.util.Map;

    public class Fo {

        private static void mapValueSort(Hashtable<String, Integer> labelsMap) //哈希表排序函数

        {

            List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(labelsMap.entrySet());//创建集合list,并规范集合为哈希表类型,并用labelsMap.entrySet()初始化

            list.sort(new Comparator<Map.Entry<String, Integer>>() //定义list排序函数

            {

             public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)

             {

                   return o1.getValue() < o2.getValue() ? 1 : ((o1.getValue() == o2.getValue()) ? 0 : -1);

                }

            });

            int k=0;

            for (Map.Entry<String, Integer> mapping :list) //遍历排序后的集合

            {

             k++;

                System.out.println(mapping.getKey() + ":" + mapping.getValue());

                if(k>=6)

                {

                 break; //输出前6个后退出循环

                }

                

            }

            File file1 =new File("C:/Users/香蕉皮/Desktop/wo.txt"); //创建存储数据的文件

    try{

    FileWriter fw=new FileWriter(file1);

    BufferedWriter bw=new BufferedWriter(fw);

    int m=0;

            for (Map.Entry<String, Integer> mapping :list) //遍历集合

            {

             m++;

             bw.write(mapping.getKey()); //把关键词写入

             bw.write("=");

             bw.write(toString(mapping.getValue())); //把对象写入

             bw.newLine(); //换行

                if(m>=6)

                {

                 break; //输入6个后结束

                }

            }

            bw.close();

    fw.close();

    }catch(Exception e){

    e.printStackTrace();

    }

        }

    private static String toString(Integer value) {

    // TODO 自动生成的方法存根

    Integer a=new Integer(value);

    return a.toString();

    }

    public static void main(String[] args)

    {

    File file =new File("C:/Users/香蕉皮/Desktop/piao.txt"); //创建文件(引入飘的文本)

    try

    {

    FileReader fr=new FileReader(file);

    BufferedReader br=new BufferedReader(fr); //缓冲区 //

    String tmp=null;

    String s=null;

    int i=0;

    while((s=br.readLine())!=null) //把文本中每行中所有的字符都赋给一个字符串s,若s不为空,则继续循环

    {

    tmp+=s; //把文本所有的字符串都连接起来并赋给tmp

    }

    tmp=tmp.toLowerCase(); //把所有的字符都变为小写

    String []str1=tmp.split("[^a-zA-Z\']+"); //用正则表达式和字符串分隔符把文本分割成一个一个的字符串并存到一个字符串数组中

    Hashtable<String,Integer> hash = new Hashtable<String,Integer>();//定义一个哈希表并规范关键词key为字符串类型,对象valueInteger类型

    for(i=0;i<str1.length;i++) //遍历字符串数组

    {

    if(!hash.containsKey(str1[i])) //判断该关键词是否已经存在哈希表中

    {

    hash.put(str1[i],new Integer(1)); //如果不存在,则把该字符串当作关键词存到哈希表中,并把把该对象value设为1

    }

    else

    {

    int a=hash.get(str1[i])+new Integer("1"); //如果存在,则把该关键字对应的对象value1

    hash.put(str1[i],a);

    }

    }

    mapValueSort((Hashtable<String,Integer>)hash); //把哈希表排序

    br.close();

    fr.close();

    }catch(Exception e){

    e.printStackTrace();

    }

    }

    }

  • 相关阅读:
    Spring Boot中的测试
    从Spring迁移到Spring Boot
    在Spring Boot中配置web app
    自定义spring boot的自动配置
    Spring Boot @EnableAutoConfiguration和 @Configuration的区别
    Scala的Higher-Kinded类型
    Scala的存在类型
    Spring Boot注解
    Maven Wrapper简介
    spring boot 使用maven和fat jar/war运行应用程序的对比
  • 原文地址:https://www.cnblogs.com/chenaiiu/p/11599966.html
Copyright © 2011-2022 走看看