java 2个字符串中找相同字符

来源:百度知道 编辑:UC知道 时间:2024/06/14 16:47:35
import javax.swing.JOptionPane;

public class zf{
public static void main(String args[])
{
String str1 = JOptionPane.showInputDialog(null,
"第1个字符串", "输入2个字符串,判断共同的字符", JOptionPane.QUESTION_MESSAGE);
String str2 = JOptionPane.showInputDialog(null,
"第2个字符串", "输入2个字符串,判断共同的字符", JOptionPane.QUESTION_MESSAGE);
int a=str1.length();
int b=str2.length();
String D=null;
for(int i=0;i<a;i++)
{
nn:
for(int j=0;j<b;j++)
{
if(str1.charAt(i)==str2.charAt(j))
{
if(D==null)
{
D=str1.charAt(i)+" \n";
}
else
D+=str1.charAt(i)+" \n";
for(int m=i;m<a;m++)
{
if(str1.charAt(i)==str1.charAt(m))
{

String str3=str1;
str1=null;
str1+=str3.substring(0,m)+s

兄弟我帮你写了一个实现这种功能的程序,很简单的,你看看,对你有不有帮助
Sorry,你的那个程序写的我实在看不清。所以帮你写了一个:

import javax.swing.JOptionPane;

public class Print {
public static void main(String args[]) {

String s1 = JOptionPane.showInputDialog(null,
"第1个字符串", "输入2个字符串,判断共同的字符", JOptionPane.QUESTION_MESSAGE);
String s2 = JOptionPane.showInputDialog(null,
"第2个字符串", "输入2个字符串,判断共同的字符", JOptionPane.QUESTION_MESSAGE);

String all = "";

//把两个字符串中的相同的字符提出来(这里可能有重复的字符)
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
if (s1.charAt(i) == s2.charAt(j))
all += s1.charAt(i);
}
}

//因为上面的all中可能有重复的字符,所以下面把它里面重复的字符去除。
//这里用了一个boolean型的数组来标记那些是重复的字符
boolean[] boo = new boolean[all.length()];

for (int i = 0; i < all.length(); i++) {
for (int j = i; j &l