JAVA题目
编程序实现把英文句子 ”Like many other large earthquakes, the 1960 quake increased stresses on adjacent parts of the fault zone, including the area where the quake occurred Saturday. Although there had been smaller quakes in the area in the ensuing 50 years, Mr. Lin said, none of them had been large enough to relieve the strain, which kept building up as the two plates converged. “This one should have released most of the stresses,” he said.” 中的单词in找出来,打印所有单词in在字符串中的位置。注意: 只处理独立单词in,而如increase中的in不算。
追问:找到位置什么意思?最后两个199和211是什么意思啊?
答案:public static void main(String[] args) {
String text=
"Like many other large earthquakes, the 1960 quake increased stresses on adjacent parts of the fault zone, including the area where the quake occurred Saturday. Although there had been smaller quakes in the area in the ensuing 50 years, Mr. Lin said, none of them had been large enough to relieve the strain, which kept building up as the two plates converged. “This one should have released most of the stresses,” he said.";
printOccurrences(text,"in");
}
static void printOccurrences(String text,String match_word){
String word="";
int len=text.length(), i=0;
for(; i<len; i++){
char ch=text.charAt(i);
if(Character.isLetterOrDigit(ch)){
word+=ch;
}else{
if(word.compareTo(match_word)==0)
System.out.println("找到位置:"+(i-match_word.length()));
word="";
}
}
if(word.compareTo(match_word)==0)
System.out.println("找到位置:"+(i-match_word.length()));
}
=============
找到位置:199
找到位置:211
其他:private void replaceStr(String str){ //str就是这英文语句
int thisIndex = 0;
while(thisIndex < str.length()){
char ch1 = sql.charAt(thisIndex);
if(ch1 == 'i'){
if(thisIndex < str.length() -1){
char ch2 = str.charAt(thisIndex+1);
if(ch2 == 'n'){
if(thisIndex < str.length() -2){
char ch3 = str.charAt(thisIndex+2);
if(ch3 == ' '){
System.out.println("开始位置"+thisIndex + " 结束位置"+thisIndex+2 )
//thisIndex = thisIndex+2;
}
}
}
}
}
thisIndex ++;
}
}
能看懂就行,手写的没调试,基本没问题,。。。上班鸟~ public static void main(String[] args)
{
List<Integer> list = new ArrayList<Integer>();
String s = "Like many other large earthquakes, the 1960 quake increased stresses on adjacent parts of the fault zone, including the area where the quake occurred Saturday. Although there had been smaller quakes in the area in the ensuing 50 years, Mr. Lin said, none of them had been large enough to relieve the strain, which kept building up as the two plates converged. ‘This one should have released most of the stresses,’ he said.‘";
count(list, s);
for (int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
}
public static void count(List list, String s)
{
int index = s.indexOf(" in ");
if(index != -1)
{
list.add(index + 1);
count(list, s.substring(index + 3));
}
}
上一个:今天不小心把JAVA文件夹里所有的文件都给删了,现在下了JAVA程序可以安装,但是安装后打开就显示空白
下一个:各位,我刚自学完清华的一本java简明教程,快被我翻烂了,有了基础知识,该继续看点什么书好呢?