如题所述
import java.util.Scanner;
public class Test {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("输入第一个字符串:");
String s1 = sc.nextLine();
System.out.println("输入第二个字符串:");
String s2 = sc.nextLine();
String max = null, min = null;
max = s1.length() > s2.length() ? s1 : s2;
min = max.equals(s1) ? s2 : s1;// 找出长短!
for (int i = 0; i < min.length(); i++) {//遍历短
for (int a = 0, b = min.length() - i; b != min.length() + 1; a++, b++) {
String sub = min.substring(a, b);//截取
if (max.contains(sub)) {//找到!
System.out.println("共同字符串为:" + sub);
return;
}
}
}
}
}
public class Test {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("输入第一个字符串:");
String s1 = sc.nextLine();
System.out.println("输入第二个字符串:");
String s2 = sc.nextLine();
String max = null, min = null;
max = s1.length() > s2.length() ? s1 : s2;
min = max.equals(s1) ? s2 : s1;// 找出长短!
for (int i = 0; i < min.length(); i++) {//遍历短
for (int a = 0, b = min.length() - i; b != min.length() + 1; a++, b++) {
String sub = min.substring(a, b);//截取
if (max.contains(sub)) {//找到!
System.out.println("共同字符串为:" + sub);
return;
}
}
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2018-03-20
你留个邮箱 回头发给你代码