一:题目描述:

二:代码实现
public class test {
public static void main(String[] args) throws IOException {
String Str="";
File file = new File("src/main/resources/Test.txt");
//文件路径src/main/resources/Test.txt
String book="";
Scanner input = new Scanner(file);
while (input.hasNext()) {
String firstName = input.next();
Str = Str + firstName.charAt(0);
Str = Str + firstName.charAt(firstName.length()-1);
System.out.println(firstName);
}
// 没必要关闭输入文件,但这样做是一种释放资源的好方法
System.out.println("Str="+Str);
//System.out.println(Str.length());
Str+='.';
int temp=1;
int count=1;
int k=0;
int[]A = new int[1000];
for (int i = 1; i < Str.length() ; i = i + 2) {
System.out.println(Str.charAt(i));
System.out.println(Str.charAt(i+1));
if (Str.charAt(i) == Str.charAt(i + 1)||Str.charAt(i) == Str.charAt(i + 1)+'a'-'A') {
//System.out.println(Str.charAt(i));
// System.out.println(Str.charAt(i+1));
//System.out.println((i+1)/2);
temp++;
}else
{
if(count<=temp)
{
count=temp;
}
temp=1;
continue;
}
}
System.out.println(count);
input.close();
}
}
三:思路描述
将每个单词首尾的字母取下来传到一个新的String字符串,然后循环验证最大相同的字母数量,最后打印出来数字。
浙公网安备 33010602011771号