初学者在这里 -给出两个字符串的奇数字符
我需要在一组两个字符串中找到奇数字符。一切都在编译,但是当它打印出奇数字符时,它会打印出较长字符串的字符。有谁知道我该如何修复它?
感谢
public class odd
{
public static void main(String[] args)
{
String str1;
String str2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 2 words:");
str1 = keyboard.nextLine();
str2 = keyboard.nextLine();
int n1 = str1.length();
int n2 = str2.length();
int x1 = 0, x2 = 0;
if (Math.abs(n1-n2)==1)
{
if (n1 > n2)
{
x1 = n1;
x2 = n2;
}
if(n1 < n2)
{
x1 = n2;
x2 = n1;
String temp = str1;
str1 = str2;
str2 = str1;
}
}
else
{
System.out.print("Invalid input.");
}
for (int i=0; i < x1; i++)
{
for (int j = 0; j < x2; j++)
{
if(str1[i]==str2[j])
{
System.out.println("Extra letter is: " + str1[i]);
break;
}
}
}
}
}
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – David