2.27课堂小测

课堂练习01题目:计算最长英语单词链。

、题目内容:

大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N 个不同的英语单词, 我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。

最长的定义是:最多单词数量,和单词中字母的数量无关。

、题目要求:

1、统一输入文件名称:input1.txtinput2.txt

2、统一输出文件名称:output1.txtoutput2.txt

3、程序需要考虑下列异常状况:

1例如,文件不存在,你的程序会崩溃么,还是能优雅地退出并给用户提示信息?

2如果文件没有任何单词、只有一个单词、没有可以首尾相连的单词,程序应该如何输出?

3如果输入文件有一万个单词,你的程序能多快输出结果?

 

思路:计算最长英语单词链,就是找到文件中可以首尾相接的单词,找到一个这样首尾相接单词数目最多的链

首先要读取文件,在代码中要有 若文件不存在,要给出提示,当文件中没有单词时输出为空,当文件里只有一个单词时,输出的就为这个单词

代码没有完全完成以上的任务

代码为:

复制代码
package com;

import java.io.*;

public class demo2 {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
        String filename = "src\\input1.txt";
        File a = new File(filename);
        File file = new File("src\\output1.txt");
        long startTime = System.currentTimeMillis();
        // judeFileExists(a);
        if (judeFileExists(a)) {
            danci(filename);
        } else {
            try {

                FileWriter fw = new FileWriter(file);
                fw.write("无此文件");
                fw.flush();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis(); // 获取结束时间
        System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); // 输出程序运行时间
    }

    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);// 将读取出的字符追加到stringbuffer中
        }
        br.close(); // 关闭读入流
        String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
        String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
        StringBuffer yao = new StringBuffer();
        String b1 = words[0];
        yao.append(b1);
        yao.append(" ");
        // System.out.println(b1);
        if (b1.equals("")) {
            System.out.println("文件为空");
            yao.append("文件为空");
        } else {
            if (words.length == 1) {
                System.out.println("只有一个单词");
            }

            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)) {
                    if (judechong(yao, words[i])) {
                    } else {
                        end = words[i].substring(words[i].length() - 1,
                                words[i].length());
                        yao.append(words[i]);
                        yao.append(" ");
                    }

                }
            }

            if (yao.toString().equals(words[0] + " ")) {
                yao.append("无互联语句");
                System.out.println("无互联词");
            }
        }

        // for( String a:words)
        // {
        // System.out.println(a);
        // }
        // System.out.println(yao.toString());
        File file = new File("src\\output1.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 judechong(StringBuffer yao, String word) {
        String a = yao.toString();
        boolean flag = false;
        String[] words = a.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
        for (int i = 0; i < words.length; i++) {
            if (word.equals(words[i])) {
                flag = true;
            }
        }
        return flag;
    }

    // 判断文件是否存在
    public static boolean judeFileExists(File file) {

        if (file.exists()) {
            System.out.println("文件存在");
            return true;
        } else {
            System.out.println("文件不存在");
            // try {
            // file.createNewFile();
            // } catch (IOException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            // }
            return false;
        }
    }

}
复制代码

 

posted @   想不起来好名字  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
点击右上角即可分享
微信分享提示