![]() |
#2
grmmylbs2016-05-12 10:56
如果仅仅是找单词中包含的the,可以这样写,如果是要包含独立的单词the,那么需要小改一下。
![]() import *; import java.util.*; public class End { private int line, charCount, countThe; private StringBuffer sb; private String str; private Vector<String> v; private Vector<StringBuffer> vsb; public End() { sb = new StringBuffer(); v = new Vector<String>(); vsb = new Vector<StringBuffer>(); str = new String(); } public void input() throws IOException { String s = ""; System.out.println("请输入若干行文本,以end作为结束行:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); s = br.readLine(); str = ""; while (!s.equals("end")) { v.add(s); str = str + s; StringBuffer ssb = new StringBuffer(s); vsb.add(ssb); s += "\n"; line++; sb.append(s); s = br.readLine(); } } public void count() { charCount = str.length(); str = str.replace("the", ""); countThe = (charCount - str.length())/"the".length(); /* for (int i = 0; i < v.size(); i++) { String[] s = v.get(i).split(""); String ss = ""; for (int j = 0; j < s.length; j++) { if (s[j].contains("the")) countThe++; ss+=s[j]; } charCount += ss.length(); }*/ }//问题 public void translate() { for (int i = 0; i < vsb.size(); i++) { for (int j = 0; j < vsb.get(i).length(); j++) if (j == 0 || vsb.get(i).charAt(j - 1) == ' ') vsb.get(i).setCharAt(j, Character.toUpperCase(vsb.get(i).charAt(j))); } } public void output() { System.out.println("该文本由" + line + "行组成,字符总数为:" + charCount + ",有 " +countThe+ "个“the”"); System.out.println("将整个文本中所有单词首字母为小写的改为大写输出:"); System.out.println(vsb.toString()); } public static void main(String[] args) throws IOException { End e = new End(); e.input(); e.count(); e.translate(); e.output(); } } 请输入若干行文本,以end作为结束行: the other another end 该文本由3行组成,字符总数为:15,有 3个“the” 将整个文本中所有单词首字母为小写的改为大写输出: [The, Other, Another] |
只有本站会员才能查看附件,请 登录

import *;
import java.util.*;
public class End {
private int line, charCount, countThe;
private StringBuffer sb;
private Vector<String> v;
private Vector<StringBuffer> vsb;
public End() {
sb = new StringBuffer();
v = new Vector<String>();
vsb = new Vector<StringBuffer>();
}
public void input() throws IOException {
String s = "";
System.out.println("请输入若干行文本,以end作为结束行:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
while (!s.equals("end")) {
v.add(s);
StringBuffer ssb = new StringBuffer(s);
vsb.add(ssb);
s += "\n";
line++;
sb.append(s);
s = br.readLine();
}
}
public void count() {
for (int i = 0; i < v.size(); i++) {
String[]s = v.get(i).split("");
String ss = "";
for (int j = 0; j < s.length; j++) {
if (s[j].contains("the"))
countThe++;
ss+=s[j];
}
charCount += ss.length();
}
}//问题
public void translate() {
for (int i = 0; i < vsb.size(); i++) {
for (int j = 0; j < vsb.get(i).length(); j++)
if (j == 0 || vsb.get(i).charAt(j - 1) == ' ')
vsb.get(i).setCharAt(j, Character.toUpperCase(vsb.get(i).charAt(j)));
}
}
public void output() {
System.out.println("该文本由" + line + "行组成,字符总数为:" + charCount + ",有 " +countThe+ "个“the”");
System.out.println("将整个文本中所有单词首字母为小写的改为大写输出:");
System.out.println(vsb.toString());
}
public static void main(String[] args) throws IOException {
End e = new End();
e.input();
e.count();
e.translate();
e.output();
}
}
import java.util.*;
public class End {
private int line, charCount, countThe;
private StringBuffer sb;
private Vector<String> v;
private Vector<StringBuffer> vsb;
public End() {
sb = new StringBuffer();
v = new Vector<String>();
vsb = new Vector<StringBuffer>();
}
public void input() throws IOException {
String s = "";
System.out.println("请输入若干行文本,以end作为结束行:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
while (!s.equals("end")) {
v.add(s);
StringBuffer ssb = new StringBuffer(s);
vsb.add(ssb);
s += "\n";
line++;
sb.append(s);
s = br.readLine();
}
}
public void count() {
for (int i = 0; i < v.size(); i++) {
String[]s = v.get(i).split("");
String ss = "";
for (int j = 0; j < s.length; j++) {
if (s[j].contains("the"))
countThe++;
ss+=s[j];
}
charCount += ss.length();
}
}//问题
public void translate() {
for (int i = 0; i < vsb.size(); i++) {
for (int j = 0; j < vsb.get(i).length(); j++)
if (j == 0 || vsb.get(i).charAt(j - 1) == ' ')
vsb.get(i).setCharAt(j, Character.toUpperCase(vsb.get(i).charAt(j)));
}
}
public void output() {
System.out.println("该文本由" + line + "行组成,字符总数为:" + charCount + ",有 " +countThe+ "个“the”");
System.out.println("将整个文本中所有单词首字母为小写的改为大写输出:");
System.out.println(vsb.toString());
}
public static void main(String[] args) throws IOException {
End e = new End();
e.input();
e.count();
e.translate();
e.output();
}
}