13-计算最长英语单词链
单词接龙,找出最长单词链,例如, 文件里有: Apple Zoo Elephant Under Fox Dog Moon Leaf Tree Pseudopseudohypoparathyroidism 最长的相连英语单词串为: apple - elephant – tree, 输出到文件里面,是这样的: Apple Elephant Tree
统一输入文件名称:input1.txt, input2.txt 统一输出文件名称:output1.txt,output2.txt 程序需要考虑下列异常状况: 例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息? 如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出? 如果输入文件有一万个单词,你的程序能多快输出结果?
看到这个题,想了想老师提醒的思路,把每个单词的第一个字母和最后一个字母分别放入数组中,然后第一个数组中的每个数据跟第二个数组中的字母一一进行比较,有相同的就将这两个字母提取,并且在往后比较,再从对应字母确定单词,确定最长单词链。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Lane4 {public static void main(String[] args) throws IOException {String filename ="C:\\input.txt";File a=new File(filename);//judeFileExists(a);if(judeFileExists(a)){danci(filename);}else{}}public static void danci(String s) throws IOException {BufferedReader br = new BufferedReader(new FileReader(s));StringBuffer sb = new StringBuffer();String text = null;while ((text = br.readLine()) != null) {sb.append(text);}br.close();String str = sb.toString().toLowerCase();String[] words = str.split("[^(a-zA-Z)]+");StringBuffer yao = new StringBuffer();String b1=words[0];yao.append(b1);yao.append(" ");//System.out.println(b1);String end=b1.substring(b1.length()-1,b1.length());//System.out.println(end);for(int i=1;i<words.length;i++){ String start=words[i].substring(0,1);if(end.equals(start)){end=words[i].substring(words[i].length()-1,words[i].length());yao.append(words[i]);yao.append(" ");}}// for( String a:words)// {// System.out.println(a);// }// System.out.println(yao.toString());File file =new File("C:\\output.txt");try {file.createNewFile();} catch (IOException e) {e.printStackTrace(); }try {FileWriter fw =new FileWriter(file);fw.write(yao.toString());fw.flush();fw.close();}catch (IOException e) {e.printStackTrace(); }}public static boolean judeFileExists(File file) {if (file.exists()) {System.out.println("查看输出文件");return true;} else {System.out.println("不存在");return false;}
}public static void judeDirExists(File file) {if (file.exists()) {if (file.isDirectory()) {System.out.println("dir exists");} else {System.out.println("the same name file exists, can not create dir");}} else {System.out.println("dir not exists, create it ...");file.mkdir();}}
}
转载于:https://www.cnblogs.com/jhl1234/p/11007106.html
13-计算最长英语单词链相关推荐
- 结对项目-最长英语单词链
文章目录 结对项目-最长英语单词链 项目信息 PSP 表格 接口设计参考理念 Information Hiding Interface Design Loose Coupling **计算模块接口的设 ...
- 结对编程项目——最长英语单词链
目录 结对编程项目--最长英语单词链 1. 项目地址 2. PSP 表格记录花费的时间 3. UML 图 4. 计算模块接口的设计与实现过程 5. 参考资料中 Information Hiding.I ...
- 2023软工第三次作业-最长英语单词链
结对项目-最长英语单词链 项目 内容 这个作业属于哪个课程 2023北航软件工程 这个作业的要求在哪里 结对项目-最长英语单词链 我在这个课程的目标是 帮助我初步建立软件工程敏捷开发的整体流程和概念, ...
- [2022软工第三次作业]结对编程项目——最长英语单词链
项目 内容 本作业所属课程 2022年北航敏捷软件工程教学实践 本作业要求 结对编程项目-最长英语单词链 个人课程目标 学习到软件工程的方法论,了解整个过程,并进行亲自实践 本作业在哪个具体方面帮助我 ...
- 北航2022软件工程第三次作业——结对编程(最长英语单词链)
软件工程第三次结对编程作业 项目 内容 这个作业属于哪个课程 北京航空航天大学2022春季软件工程(罗杰 任健) 这个作业的要求在哪里 结对编程项目-最长英语单词链 我在这个课程的目标是 学习软件工程 ...
- 软件工程结对项目- 最长英语单词链
项目 内容 这个作业属于哪个课程 2023年北航敏捷软件工程 这个作业的要求在哪里 结对项目-最长英语单词链 我在这个课程的目标是 学习现代化的软件开发方法 这个作业在哪个具体方面帮助我实现目标 对结 ...
- 结对项目-最长英语单词链-20373974阮正浩
项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 结对项目-最长英语单词链 我在这个课程的目标是 学习软件工程的一般方法并实践 这个作业在哪个具体方面帮助我实现目标 实践结对编程方法, ...
- 「软工结对编程」:最长英语单词链
项目 内容 这个作业属于哪个课程 2023年北航敏捷软件工程社区 这个作业的要求在哪里 结对项目-最长英语单词链 我在这个课程的目标是 学习有关软件开发的方法论,熟悉基本的软件开发流程,通过" ...
- 结对编程项目-最长英语单词链
项目 内容 这个作业属于哪个课程 2022 年北航敏捷软件工程 这个作业的要求在哪里 结对编程项目-最长单词链 我在这个课程的目标是 学习软件工程相关知识,提高自己的代码能力与团队协作能力. 这个作业 ...
- 结对项目——最长英语单词链
项目 内容 这个作业属于哪个课程 https://bbs.csdn.net/forums/buaa-ase2023 这个作业的要求在哪里 https://bbs.csdn.net/topics/613 ...
最新文章
- 在代码中定义一个执行线程的步骤
- pandas模块学习笔记2--基本功能
- 【模型开发】风控评分模型开发流程
- 自适应小波阈值去噪python_基于python的小波阈值去噪算法
- OC如何跳到系统设置里的各种设置界面
- nano-pc-t1 4412 显示驱动分析
- python中 numpy_Python中的Numpy
- 使用 dpu 检视 dump 中的字符串.
- java 内存接口_java中多态机制的内存解析、抽象类、接口
- syslog-ng配置说明
- (day 13 - 双指针or递归)剑指 Offer 24. 反转链表
- 几个免费的中文分词模块
- linux 卸载theano,centos 安装theano
- Frida cmd常用命令
- VUE面试题-2022
- 在线上课中设备突然关机如何关闭直播
- 中国移动、联动、电信
- Redis缓存详解(黑马-未完结)
- window 开启 Telnet 客户端
- Facebook的利润创下历史新高,不受最近的丑闻影响