想了解Java成语接龙的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java实现成语接龙的相关问题,此外,我们还将为您介绍关于java-英语单词接龙、Java实验--关于英文短语词语接龙、
想了解Java成语接龙的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于java实现成语接龙的相关问题,此外,我们还将为您介绍关于java -英语单词接龙、Java实验--关于英文短语词语接龙、java成功javac不成功怎么解决、java成员内部类的新知识。
本文目录一览:Java成语接龙(java实现成语接龙)
package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\input.txt");
if (!file.exists()) {
System.out.println("文件不存在");
return;
}
String[] strs=new String[99999999];
Scanner x = new Scanner(file);
int i=0;
boolean flag=false;
while(x.hasNextLine()) {
String[] str=x.nextLine().split("\\W+");
for(int ms=0;ms<str.length;ms++) {
if(!str[ms].equals("")&&str[ms].length()>2) {
flag=false;
if(i!=0) {
for(int t=0;t<i;t++) {
if(!str[ms].equals(strs[t])) {
flag=true;
}
}
}else {
flag=true;
}
if(flag) {
strs[i]=str[ms];
i++;
}
}
}
}
String sentence = "";
String word="";
String max="";
for(int m=0;m<i;m++) {
sentence = strs[m];
word = sentence;
for(int j=m+1;j<i;j++) {
if(strs[j].toLowerCase().subSequence(0, 1).equals(word.toLowerCase().subSequence(word.length()-1, word.length()))) {
word = strs[j];
sentence+="-"+word;
}
}
if(sentence.indexOf("-")!=-1) {
if(sentence.length()>max.length()) {
max = sentence;
}
// System.out.println(sentence);
}
}
File w = new File("D://output.txt");
w.createNewFile(); // 创建新文件
BufferedWriter out = new BufferedWriter(new FileWriter(w));
out.write(max); // \r\n即为换行
out.flush(); // 把缓存区内容压入文件
out.close(); // 最后记得关闭文件
if(max.length()!=0) {
System.out.println(max);
}else {
System.out.println("没有首尾相连");
}
}
}
读取文件为input.txt 输出文件为output.txt.
java -英语单词接龙
设计思想:
将文件中的单词存入ArrayList数组中,分为前后两个数组,读入单词,经单词字母分解并且通过循环比较单词字母是否相同,相同写入结果文件,不同继续比较,直至找到最大接龙单词长度。
源程序代码:
package 单词接龙;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
public class FineWord
{
public ArrayList<String> words = new ArrayList<>();
public ArrayList<String> wordsList = new ArrayList<>();
public boolean compare(String a, String b)
{
a = a.toLowerCase();
b = b.toLowerCase();
return (a.substring(a.length() - 1).equals(b.substring(0, 1)));
}
public void fileSplit(String path) throws Exception
{
MyFile file = new MyFile();
String theFileString = file.get(path);
if (theFileString == null)
{
return;
}
if (theFileString.equals(""))
{
throw new Exception("空文件");
}
for (String word : theFileString.split("\\,|\\.| |\\(|\\)|\\;|\\:|\"|\\?|\\!|\\''| |\\、|\\”|\\“"))
{
if (!word.equals(""))
{
words.add(word);
}
}
if (words.size() <= 1)
{
throw new Exception("文件内单词过少(只有" + words.size() + "个词)");
}
}
public void wordWrite(int index, String path) throws Exception
{
MyFile file = new MyFile();
BufferedWriter bf = file.put(path);
wordsList.add(words.get(index));
try
{
for (String string : words)
{
if (compare(wordsList.get(wordsList.size() - 1), string))
{
wordsList.add(string);
bf.append(string);
bf.newLine();
}
}
bf.close();
} catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
if (wordsList.size() <= 1)
{
throw new Exception("文件内无单词链");
}
}
public static void main(String[] args)
{
FineWord aFineWord = new FineWord();
try
{
aFineWord.fileSplit("d://input1.txt");
aFineWord.wordWrite(0, "d://output1.txt");
System.out.println(aFineWord.wordsList);
} catch (IOException e)
{
System.out.println("无此文件");
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
程序结果:
单个单词无接龙输出首个单词
总结:这次计算单词链最长长度,使我清楚地认识到将单词长句分解,可以实现很多功能,遇到问题不要慌张,把问题步揍化这样会更加简单。
Java实验--关于英文短语词语接龙
在课堂上经过实验之后,重新在宿舍里面从0开始编写大概30分钟左右能够完成这个实验,不是原来的思路。
该实验的表述为:从两个文本input1.txt和input2.txt中读取英文单词,若前面的英文单词的尾字母和后面的英文单词的未字母相同的话,则构成一个英文词语接龙,直到文章结尾,求出整篇文章中词语接龙最长的词语接龙词组,并将其输出到output1.txt和output2.txt文件夹中。
实验代码:
package ctn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Ctn {
static List<String> words=new ArrayList<String>();
public static void main(String args[]) throws IOException
{
words.clear();
if(daoru("input1.txt"))
{
int size=0;
List<String> result = null;
while(words.size()>0)
{
List<String> temp=returnList(words);
if(temp.size()>size)
{
result=temp;
size=temp.size();
}
}
String all=new String();
if(result.size()>1)
{
for(String it:result)
{
all+=it+"\r\n";
}
daochu(all,"output1.txt");
System.out.println("文件output1.txt导出");
}
else System.out.println("单词数量过少无法导出");
}
else System.out.println("文件input1.txt不存在");
words.clear();
if(daoru("input2.txt"))
{
int size=0;
List<String> result = null;
while(words.size()>0)
{
List<String> temp=returnList(words);
if(temp.size()>size)
{
result=temp;
size=temp.size();
}
}
String all=new String();
if(result.size()>1)
{
for(String it:result)
{
all+=it+"\r\n";
}
daochu(all,"output2.txt");
System.out.println("文件output2.txt导出");
}
else System.out.println("单词数量过少无法导出");
words.clear();
}
else System.out.println("文件input2.txt不存在");
}
public static List<String> returnList(List<String> in)
{
char cold=0;
char cnew=0;
List<String> temp=new ArrayList<String>();
List<Integer> tempNum=new ArrayList<Integer>();
for(int i=0;i<in.size();i++)
{
String now=in.get(i);
if(i==0)
{
cold=now.charAt(now.length()-1);
tempNum.add(i);
temp.add(now);
continue;
}
cnew=now.charAt(0);
if(cold==cnew)
{
tempNum.add(i);
if(!temp.contains(now))
{
temp.add(now);
cold=now.charAt(now.length()-1);
}
}
}
for(int j=tempNum.size()-1;j>=0;j--)
{
in.remove((int)tempNum.get(j));
}
return temp;
}
public static boolean daoru(String path) throws IOException
{
File a=new File(path);
if(!judeFileExists(a))
{
System.out.println("文件不存在");
return false;
}
FileInputStream b = new FileInputStream(a);
InputStreamReader c=new InputStreamReader(b,"UTF-8");
{
BufferedReader bufr =new BufferedReader(c);
String line = null;
while((line = bufr.readLine())!=null){
//line是每一行的数据
String ook[]=line.split("[^A-Za-z]");
for(String it:ook)
{
//it是每一个空格的数据
String temp=it.toLowerCase().replace("\''", "").replace(",", "").replace(".", "").replace(":", "").replace("!", "");
if(temp.length()>0)
words.add(temp);
}
}
bufr.close();
}
c.close();
b.close();
return true;
}
//导入文件时判断文件存在
public static boolean judeFileExists(File file) {
if (file.exists()) {
return true;
} else {
return false;
}
}
public static void daochu(String txt,String outfile) throws IOException
{
File fi=new File(outfile);
FileOutputStream fop=new FileOutputStream(fi);
OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8");
ops.append(txt);
ops.close();
fop.close();
}
}
该实验过程中
对input1.txt输入飘的英文小说的第一章内容,输出output1.txt的时间响应应该在毫秒级以内。(单词量1W左右)
对input2.txt输入飘的整本英文小说的内筒后,输出output2.txt的时间响应应该在5分钟左右。(单词量50W左右)
因此上述代码的算法对于数的运算过程响应的时间较长,推断是List中读取N个数据所耗费的时间太长,但是经过了把代码修改成HashMap和Vector对比之后(算法一样的情况下),上面的代码在处理速度已经是最优了
对于Vector来说:处理1w单词就需要耗费数秒,对于50w词的数据就更不用说了
对于Map来说:处理1w单词的时候和List都在1秒以内,50w单词的处理未经过测试
Map的成语接龙实验代码:
package ctn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Ctn2 {
static Map<Integer,String> words=null;
public static void main(String args[]) throws IOException
{
//words.clear();
//System.out.println("导入Input1.txt");
words=new HashMap<Integer,String>();
if(daoru("input1.txt"))
{
//System.out.println("导入成功");
int size=0;
Map<Integer,String> result = null;
int maxSize=words.size();
while(words.size()>0)
{
Map<Integer,String> temp=returnMap(words);
if(temp.size()>size)
{
result=temp;
size=temp.size();
}
}
String all=new String();
if(result.size()>1)
{
for(int i=0;i<maxSize;i++)
{
if(result.get(i)!=null)
all+=result.get(i)+"\r\n";
}
daochu(all,"output1.txt");
System.out.println("文件output1.txt导出");
}
else System.out.println("单词数量过少无法导出");
}
else System.out.println("文件input1.txt不存在");
System.out.println("开始清空Words");
words.clear();
System.out.println("Words清空完毕");
System.out.println("导入Input2.txt");
if(daoru("input2.txt"))
{
System.out.println("导入成功");
int size=0;
Map<Integer,String> result = null;
int maxSize=words.size();
while(words.size()>0)
{
Map<Integer,String> temp=returnMap(words);
if(temp.size()>size)
{
result=temp;
size=temp.size();
}
}
String all=new String();
if(result.size()>1)
{
for(int i=0;i<maxSize;i++)
{
if(result.get(i)!=null)
all+=result.get(i)+"\r\n";
}
daochu(all,"output2.txt");
System.out.println("文件output2.txt导出");
}
else System.out.println("单词数量过少无法导出");
}
else System.out.println("文件input2.txt不存在");
}
//将元素对应到Map上,如何判断已经检查过?
public static Map<Integer,String> returnMap(Map<Integer,String> in)
{
char cold=0;
char cnew=0;
Map<Integer,String> temp=new HashMap<Integer,String>();
List<Integer> tempNum=new ArrayList<Integer>();
boolean g_firstStart=false;
int g_start=0;
for(Integer i:in.keySet())
{
if(!g_firstStart)
{
g_firstStart=true;
g_start=i;
}
String now=in.get(i);
if(i==g_start)
{
cold=now.charAt(now.length()-1);
temp.put(i, now);
tempNum.add(i);
continue;
}
cnew=now.charAt(0);
if(cold==cnew)
{
temp.put(i, now);
tempNum.add(i);
cold=now.charAt(now.length()-1);
}
}
for(Integer z:tempNum)
{
in.remove(z);
}
return temp;
}
public static boolean daoru(String path) throws IOException
{
File a=new File(path);
if(!judeFileExists(a))
{
System.out.println("文件不存在");
return false;
}
FileInputStream b = new FileInputStream(a);
InputStreamReader c=new InputStreamReader(b,"UTF-8");
{
BufferedReader bufr =new BufferedReader(c);
String line = null;
Integer i=0;
while((line = bufr.readLine())!=null){
//line是每一行的数据
String ook[]=line.split("[^A-Za-z]");
for(String it:ook)
{
//it是每一个空格的数据
String temp=it.toLowerCase().replace("\''", "").replace(",", "").replace(".", "").replace(":", "").replace("!", "");
if(temp.length()>1)
{
words.put(i,temp);
i+=1;
}
}
}
bufr.close();
}
c.close();
b.close();
return true;
}
//导入文件时判断文件存在
public static boolean judeFileExists(File file) {
if (file.exists()) {
return true;
} else {
return false;
}
}
public static void daochu(String txt,String outfile) throws IOException
{
File fi=new File(outfile);
FileOutputStream fop=new FileOutputStream(fi);
OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8");
ops.append(txt);
ops.close();
fop.close();
}
}
在算法方面我找不到更优的解法,对于老师要求的几百W个单词中词语接龙的算法,还是得进一步探索后才能知道,虽然单词读入的过程中不会死机,但是要想在1分钟内实现百万级别的单词的找出最长的成语接龙还需要很长的1段路需要走。
现在暂时挂起,以后有能力再继续挑战。
java成功javac不成功怎么解决
解决步骤包括:检查jdk安装、找到jdk安装路径、将jdk的bin目录添加到path环境变量、验证环境变量、检查javac版本、重新安装jdk、使用绝对路径运行javac,并确保在进行更改后重新启动命令行窗口。
当你遇到“java成功但javac不成功”的情况时,通常意味着Java运行环境(JRE)已经正确安装,但是Java开发工具包(JDK)可能没有正确安装,或者JDK的bin目录没有被添加到系统的环境变量中。下面是一些解决这个问题的步骤:
- 首先,检查你是否已经安装了JDK。你可以在命令行中输入java -version来查看Java版本,但这只能确认JRE是否安装。为了确认JDK是否安装,你应该查看是否存在javac命令。
- 如果你已经安装了JDK,找到它的安装路径。这通常是一个包含bin目录的文件夹,其中应该包含javac和其他Java开发工具。
- 在Windows上,你可以通过“控制面板”的“系统”->“高级系统设置”->“环境变量”来添加JDK的bin目录到PATH变量中。
- 在Linux或macOS上,你可以编辑你的shell配置文件(如.bashrc或.zshrc),并添加类似下面的行:替换/path/to/jdk/bin为你的JDK安装路径下的bin目录。
bash复制代码export PATH=/path/to/jdk/bin:$PATH
登录后复制
- 在你添加或修改了环境变量后,重新打开一个新的命令行窗口,以确保新设置生效。然后输入echo %PATH%(Windows)或echo $PATH(Linux/macOS)来检查PATH变量是否包含了JDK的bin目录。
- 在命令行中输入javac -version来检查javac是否可以被识别,以及它的版本是否与你期望的JDK版本一致。
- 如果上述步骤都不能解决问题,考虑卸载JDK并重新安装。确保在安装过程中选择了正确的安装路径,并在安装完成后检查是否勾选了“将JDK添加到PATH”的选项(如果有这个选项的话)。
- 作为临时解决方案,你可以直接使用JDK的bin目录下的javac的绝对路径来编译Java程序。例如:/path/to/jdk/bin/javac YourFile.java。
确保在进行这些更改后重新启动命令行窗口或终端,以便新的环境变量设置生效。如果按照这些步骤操作后仍然遇到问题,可能需要进一步检查系统配置或寻求专业的技术支持。
以上就是java成功javac不成功怎么解决的详细内容,更多请关注php中文网其它相关文章!
java成员内部类
在阅读JAVA一些数据结构源代码的时候,经常会发现成员内部类的使用,如Hashmap中的Node和TreeNode,所以我们就来把它归纳总结一下
1.成员内部类
(1)内外部的访问权限
构造一个外部类和内部类
public class Out {
private String str="outStr";
private void print()
{
System.out.println(str);
}
private class In{
public String inStr="inStr";
public void test()
{
System.out.println(str);
print();
}
}
}
测试
public static void main(String[] args) {
Out out=new Out();
Out.In in=out.new In();
in.test();
}
结果说明内部类可以任意访问外部类的成员函数成员变量,而外部类不能随意访问内部类的成员等
另外内部类是可以有修饰符的 比如这里是private,但也竟然成功了
(2)内部类会在外部类初始化的时候被初始化吗?
public class Out {
public Out()
{
System.out.println("out");
}
public class In{
public In()
{
System.out.println("in");
}
}
public static void main(String[] args) {
Out out=new Out();
}
}
很显然不会
(3)内外部方法名冲突时的处理方法
public class Out {
public void test()
{
System.out.println("out");
}
public class In{
public void test()
{
System.out.println("in");
}
}
public static void main(String[] args) {
Out out=new Out();
Out.In in=out.new In();
in.test();
}
}
显然调用内部类自己的,外部类的方法被隐藏掉了
2.静态成员内部类
(1)内外部的访问权限
public class Out {
private String str="outStr";
private static String static_str="static_str";
public static class In{
public void test()
{
System.out.println(static_str);
}
}
public static void main(String[] args) {
//Out out=new Out();
In in=new In();
in.test();
}
}
结果表明现在内部类只能调用外部类的静态成员了,而且此时内部类的初始化也不再完全依赖于外部类了
(2)内部类会在外部类初始化的时候被初始化吗?
public class Out {
public Out()
{
System.out.println("out");
}
public static class In{
public In()
{
System.out.println("in");
}
}
public static void main(String[] args) {
Out out=new Out();
}
}
还是不会
今天关于Java成语接龙和java实现成语接龙的讲解已经结束,谢谢您的阅读,如果想了解更多关于java -英语单词接龙、Java实验--关于英文短语词语接龙、java成功javac不成功怎么解决、java成员内部类的相关知识,请在本站搜索。
本文标签: