2017-02-17 105 views
-2

以下是问题陈述:编写一个比较2个字符串的函数,根据两个字符串是否包含相同的字母,返回true或false。顺序无关紧要。Java初学者,比较嵌套循环中的字符串

我不知道如何正确比较我的嵌套循环中的字符数组。我希望我可以更具体地了解我的问题,但我是一个非常新的学习者,不知道为什么这不起作用。我相信它没有在嵌套for循环中做我想要的。提前致谢!

import java.util.Scanner; 

public class PracticeProblems { 

public static boolean stringCompare(String word1, String word2) { 
    char[] word1b = new char[word1.length()]; 
    char[] word2b = new char[word2.length()]; 
    boolean compareBool = false; 

    for(int i = 0; i < word1.length(); i++) { 
     word1b[i] = word1.charAt(i); 
     word2b[i] = word2.charAt(i); 
    } 

    for(int i = 0; i < word1.length(); i++) { 
     for(int j = 0; j < word2.length(); j++) { 
      if(word1b[i] == word2b[j]) { 
       compareBool = true; 
       break; 
      } else { 
       compareBool = false; 
       break; 
      } 
     } 
    } 
    return compareBool; 
} 

public static void main(String []args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Word 1?"); 
    String word1 = scan.nextLine(); 
    System.out.println("Word 2?"); 
    String word2 = scan.nextLine(); 

    if(PracticeProblems.stringCompare(word1, word2) == true) { 
     System.out.println("Same Letters!"); 
    } else { 
     System.out.println("Different Letters..."); 
    } 

} 
+1

你总是在第一次通过循环“休息”。删除'if'中的'break'并将其保留在'else'中。 – resueman

+0

为什么不只是'char [] word1b = word1.toCharArray();'? (或者,比较'word1.charAt(i)'和'word2.charAt(j)',避免创建新的数组)。 –

+0

将每个“字符串”的字符放入“集合”中。然后比较这些集合。 –

回答

1

下面的代码将完成这项工作。这实质上是弗兰克上面评论的扩展。我们将两个字符串转换为两个集合,然后进行比较。

import java.util.*; 

public class SameChars { 

    // Logic to convert the string to a set 
    public static Set<Character> stringToCharSet(String str) { 
     Set<Character> charSet = new HashSet<Character>(); 
     char arrayChar[] = str.toCharArray(); 
     for (char aChar : arrayChar) { 
      charSet.add(aChar); 
     } 

     return charSet; 
    } 

    // Compares the two sets 
    public static boolean hasSameChars(String str1, String str2) { 
     return stringToCharSet(str1).equals(stringToCharSet(str2)); 
    } 

    public static void main(String args[]){ 
     // Should return true 
     System.out.println(hasSameChars("hello", "olleh")); 
     // Should returns false 
     System.out.println(hasSameChars("hellox", "olleh")); 
    } 

} 
+0

你可以想一下如何去做而不使用一套? :) –

+2

首先检查字符串的长度是否相同,如果它们是从字符串中创建两个数组,则进行排序,然后比较索引。其余的作业:) –

+0

难道这不会有更好的答案吗? –

0

请允许我向您boolean变量重命名为letterFound(或者甚至letterFoundInWord2),因为这是你在你的双循环检查。解释性命名使得思路清晰易于理解。

既然你正在检查从word1一个字母的时间,你可以移动的letterFound声明外for循环内,并且把它初始化为false在这里,因为每次拍摄新的信word1时候,你还没有找到它在word2还没有。在for循环中的if声明中,如果字母相同并且您将letterFound设置为true,则break正确。在相反的情况下,不要中断,继续检查下一封信。实际上,您可以完全删除else部分。

for循环后,如果letterFound仍不true,我们知道,从word1信不word2。所以stringCompare()应该返回false:

if (! letterFound) { 
    return false; 
} 

随着这一变化,外循环for后,我们知道,从word1所有字母都在word2发现,所以你可以在这里输入return true;

除外:

  • 您好像假设字符串的长度相同。如果他们没有,你的程序将无法正常工作。
  • 正如Andy Turner所说,您还应该检查word2中的所有字母是否在word1;它并不遵循来自word1的信件在word2中。
  • 应该只考虑字母吗?你应该忽略空格,数字,标点符号......?

希望你能弄清楚。欢迎随时留言或提出新问题。

1

我比较之前对数组进行排序。

//import statement for Arrays class 
    import java.util.Arrays; 
    import java.util.Scanner; 

    public class PracticeProblems { 

    public static boolean stringCompare(String word1, String word2) { 
     char[] word1b = new char[word1.length()]; 
     char[] word2b = new char[word2.length()]; 

     boolean compareBool = true; 

     for(int i = 0; i < word1.length(); i++) { 
      word1b[i] = word1.charAt(i); 
     } 
     //sort the new char array 
     Arrays.sort(word1b); 


     // added a second loop to for the second world 
     for(int i = 0; i < word2.length(); i++) { 
      word2b[i] = word2.charAt(i); 
     } 

     Arrays.sort(word2b); 

     for(int i = 0; i < word1.length(); i++) { 

      //removed second for loop. 
    //  for(int j = 0; j < word2.length(); j++) { 

      // if the two strings have different length, then they are different 
      if((word1.length()!=word2.length())){ 
       compareBool = false; 
       break; 

      } 
      //changed to not equal 
      if((word1b[i] != word2b[i])) { 
       compareBool = false; 
       break; 

      } 
      //removed else statment 
    //  else { 
    //    compareBool = false; 
    //    break; 
    // 
    //  } 
     } 
     return compareBool; 
    } 

    public static void main(String []args) { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Word 1?"); 
     String word1 = scan.nextLine(); 
     System.out.println("Word 2?"); 
     String word2 = scan.nextLine(); 

     if(PracticeProblems.stringCompare(word1, word2) == true) { 
      System.out.println("Same Letters!"); 
     } else { 
      System.out.println("Different Letters..."); 
     } 
     //resource leak. use close() method. 
     scan.close(); 
    } 
    } 
+0

很好的解释。我有点怀疑你的版本是否正确,如果用户输入'ease'和'seas'。它们包含相同的字母,即a,e和s,但每个字母并不相同。如果这是一个学校任务,可能是任何结果都可以接受,只要它指定了程序的功能。 –