在2月27日进行了课堂练习,计算超长英语单词链
package letteron;
import java.io.*;
import java.util.*;
public class Letteron {
public static void main(String[] args)throws FileNotFoundException, IOException {
// TODO 自动生成的方法存根
File file=new File("D:/input.txt");
File file2=new File("D:/output.txt");
if(!file.exists())
{
System.out.println("文件不存在!");
}
else if(file.exists() && file.length() == 0) {
System.out.println("文件为空,无单词信息,无法接龙!");
}
else
{
BufferedReader br=new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/output.txt")) ;
String s;
int i,j;
ArrayList<ArrayList<String>> listall=new ArrayList();
String key;
s=br.readLine();
//将文本文件中的所有信息变为字符串
String []s1=s.split(" ");//以空格为分割划分每个单词
if(s1.length==1)
System.out.println("单词数为1,无法进行单词接龙");
else
{
for(i=0;i<s1.length;i++)
{
System.out.println(s1[i]);
}
for(j=0;j<s1.length;j++)
{
ArrayList<String> list=new ArrayList<String>();
key=s1[j].substring(s1[j].length()-1);
list.add(s1[j]);
System.out.println("key:"+key);
for(i=1;i<s1.length;i++)
{
if(key.equals(s1[i].substring(0, 1)))
{
list.add(s1[i]);
key=s1[i].substring(s1[i].length()-1);
}
else
continue;
}
listall.add(list);
}
int max=0;
int count=0;
for(i=0;i<listall.size();i++)
{
if(max<listall.get(i).size())
{
max=listall.get(i).size();
count=i;
}
}
System.out.println("排位"+count);
System.out.println("最长单词链为:");
for(i=0;i<listall.get(count).size();i++)
{
System.out.println(listall.get(count).get(i));
bw.write(listall.get(count).get(i));
bw.write(" ");
}
br.close();
bw.close();
}
}
}
}
标签:27,英语单词,课堂练习,s1,System,key,println,new,out From: https://www.cnblogs.com/jm56/p/17177208.html