java如何比较两个字符串是否有单个相同字符?
对了,我们没学过很难的,就while if什么的,for没学过 就一些很简单的 展开
public class Test
{
public static void main(String[] args)
{
String str1="abcde";
String str2="efghijk";
char[] charArray = str1.toCharArray();
boolean flag = false;
for(int i=0;i<charArray.length;i++)
{
char tmp = charArray[i];
String str = String.valueOf(tmp);
if(str2.contains(str))
{
System.out.println("有相同字符");
flag = true;
break;
}
}
if(!flag)
{
System.out.println("没有相同字符");
}
}
}
public static int get(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
int i1 = 0, i2 = 0;
int count = 0;
while (i1 != c1.length) {
while (i2 != c2.length) {
if (c1[i1] == c2[i2]) {
count++;
}
i2++;
}
i2 = 0;
i1++;
}
return count;
}
package zhidao;
public class TT
{
private static int compare(String f, String s)
{
int count = 0;
for ( int i = 0; i < f.length (); i++ )
{
for ( int j = 0; j < s.length (); j++ )
{
if (f.charAt (i) == s.charAt (j))
{
count++;
}
}
}
return count;
}
public static void main ( String[] args )
{
System.out.println (compare ("67821", "12345"));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String a="12393";
String b = "15679";
for(char c:b.toCharArray()){
if(a.indexOf(c)!=-1){
System.out.println(a.charAt(a.indexOf(c)));
}
}
}
2014-10-15
public class TestDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sl=cal();
System.out.println("一共有"+sl+"个相同的");
}
public static int cal()
{
int [] a=new int []{1,2,3,4,5};
int [] b=new int []{6,7,8,2,1};
int mount=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
mount++;
}
}
}
return mount;
}
}
for(初始值;循环条件;循环一次后的操作)
{
}
比如
for(int i=0;i<a.length;i++)
{
//初始化i等于0,接下来就判断是否满足循环条件,也就是i小雨a.length吗?满足条件进//入循环,否则不进入,进入后,执行到末尾,本次完成,进行i++;再次判断是否满足循环条//件
}