ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

动手动脑(二)

2019-09-27 19:54:40  阅读:180  来源: 互联网

标签:java String int 动脑 动手 new import public


动手动脑:

一、编写一个方法,使用以上算法生成指定数目(比如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为字符串类型,对象value为Integer类型

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"); //如果存在,则把该关键字对应的对象value加1

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

}

}

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

br.close();

fr.close();

}catch(Exception e){

e.printStackTrace();

}

 

}

 

 

 

}

标签:java,String,int,动脑,动手,new,import,public
来源: https://www.cnblogs.com/chenaiiu/p/11599966.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有