昨天王老师进行了一次课堂,做了那么多次的Java web,久违得做了一次纯Java的题目
实施证明我对Java的理解和掌握也不深入
题目——
我的代码——
package demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
//没有Pair
//存放每个单词的首尾
class Pair{
private String sword;
private char left;
private char right;
private int count;//记录此单词所在位数
public String getSword() {
return sword;
}
public void setSword(String sword) {
this.sword = sword;
}
public char getLeft() {
return left;
}
public void setLeft(char left) {
this.left = left;
}
public char getRight() {
return right;
}
public void setRight(char right) {
this.right = right;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Pair assignment(Pair pair2) {
//赋值,把pair2的值赋给另一对象
Pair pair = new Pair();
pair.setLeft(pair2.getLeft());
pair.setRight(pair2.getRight());
pair.setCount(pair2.getCount());
pair.setSword(pair2.getSword());
return pair;
}
};
class Tool{
@SuppressWarnings("null")
public Pair getlandr(String word) {
//记录首尾
char left, right;
left = word.charAt(0);
right = word.charAt(word.length()-1);
//System.out.println(left);System.out.println(right);
Pair pair = new Pair();
pair.setLeft(left);
pair.setRight(right);
return pair;
}
public boolean compare(char l, char r) {
//比较首尾是否相同
if(l == r) {
//System.out.println(l+" "+r);
return true;
}
//System.out.println(l+" "+r);
return false;
}
};
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] max = new int[3];//max,起始,终止
max[0]=max[1]=max[2]=1;//初始化
int[] maxpro = new int[3];//max,起始,终止
maxpro[0]=maxpro[1]=maxpro[2]=1;//初始化
try {
// 防止文件建立或读取失败
// 读入TXT文件
String pathname = "D:\\eclipse\\input1.txt";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(
new FileInputStream(filename)); // 建立一个输入流对象reader
BufferedReader br = new BufferedReader(reader);
String line = "";
String str = "";
line = br.readLine();
while (line != null) {
str += line;
line = br.readLine(); // 一次读入一行数据
}
String[] lin = new String[300];
int num = 0;//记录单词个数
String[] split = str.split(" ");
/*
for(String st:split){
System.out.print(st + " ");
}
*/
//System.out.println(str);//////////////////////////
Tool tool = new Tool();
Pair pair1 = new Pair();
pair1.setCount(1);
Pair pair2 = new Pair();
pair2.setCount(1);
String word_chain_pro = "";
String word_chain = "";
for(String st:split){
num++;
//i为单词个数
//分首尾
pair2 = tool.getlandr(st);
pair2.setCount(num);
pair2.setSword(st);
if(num == 1) {
pair1 = pair1.assignment(pair2);
}
if(num > 1) {
if(tool.compare(pair1.getRight(), pair2.getLeft() )){
//相等
//目前最长长度加一,最后以为单位数变更为此单词位数
maxpro[0]++;
if(maxpro[0]==2) {
//记录起始单词位置
maxpro[1] = pair1.getCount();
word_chain_pro = pair1.getSword();
}
//记录结束单词位置
maxpro[2] = pair2.getCount();
word_chain_pro += " " + pair2.getSword();
} else {
//不相等
//目前最长长度(小)赋值给最长长度,变更起始位置
if(max[0] < maxpro[0]) {
//更改最大的记录以及起始位置和结束位置
max[0] = maxpro[0];
max[1]= maxpro[1];
max[2]= maxpro[2];
//目前起始位置和终止位置相同,后移至下一个单词,目前最大长度置1
maxpro[1]= maxpro[2]= pair2.getCount()+1;
maxpro[0] = 1;
word_chain = word_chain_pro;
}
}
pair1 = pair1.assignment(pair2);
}
}
//写入txt文件
File writename = new File("D:\\eclipse\\output1.txt");
writename.createNewFile(); // 创建新文件
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write(word_chain);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("单词链最长长度:" + max[0]);
System.out.println("起始单词位数:" + max[1]);
System.out.println("末尾单词位数:" + max[2]);
}
}
在下一篇博客中我会讲一讲我的思路
标签:maxpro,pair2,String,max,测试,new,课堂,public From: https://www.cnblogs.com/yansans/p/17165793.html