想不通Javas字符串?

时间:2013-04-17 23:23:35

标签: java

我现在是学生,所以我还在学习。我很快就拿起了VB,而另一方面它很简单,我很困惑。

我这次给出的作业让我感到困惑“编写一个方法来确定两个字符串的位置数量不同。例如,”Peace“和”Piece“在两个位置上有所不同。该方法被声明为int compare(String word1,String word2);如果字符串相同,则返回0.如果两个字符串的长度不同,则返回-1。

附加“编写一个主要方法来测试方法。主要方法应该告诉字符串有多少,位置不同,或者它们是否相同,或者如果长度不同,请说明长度。从控制台获取字符串。 到目前为止,这就是我所处的地方,我正在寻找有人帮我在DUMDUM术语中解决这个问题,如果他们能够我不需要解决方案,只有帮助理解它。

package arraysandstrings;
import java.util.Scanner;

public class differStrings {
    public static void main (String agrs[]){
        Scanner scanner = new Scanner (System.in);
        System.out.print("Enter a word");
        String word1;
        String word2;
        word1 = scanner.next();
        System.out.print("Enter another word");
        word2 = scanner.next();
        int count = 0;
        int length = word1.length();

        for(int x = 0; x >= length; x = x+1) {
            if (word1.charAt(x) == word2.charAt(x)) {
                count = count + 1;
                System.out.print (count);
            }
        }
    }
}

附加问题

   package arraysandstrings;
   import java.util.Scanner;

 public class differStrings {
 public static void main (String agrs[]){
 Scanner scanner = new Scanner (System.in);
 System.out.println("Enter a word");
 String word1 = scanner.next();

System.out.println("Enter another word");
String word2 = scanner.next();
int count = 0;
int word1Length = word1.length();
int word2Length = word2.length();

if (word1Length != word2Length) {
      System.out.println ("Words are a diffrent length");
      System.out.println (word1 + "Has" + word1.length() + " chars");
      System.out.println (word2 + "Has" + word2.length() + " chars");
}

for(int x = 0; x < word1Length; x = x+1) {

       if (word1.charAt(x) != word2.charAt(x)) {
        count = count + 1;
       }}}

 System.out.println (count+" different chars");

}

在实施了从您的回复中获得的知识后,我遇到了最后一行的问题:

 System.out.println (count+" different chars");

它表示错误预期然而它在我添加我的任务的下一部分之前有效:

   if (word1Length != word2Length) {
      System.out.println ("Words are a diffrent length");
      System.out.println (word1 + "Has" + word1.length() + " chars");
      System.out.println (word2 + "Has" + word2.length() + " chars");
}

3 个答案:

答案 0 :(得分:1)

for(int x = 0; x >= length; x = x+1) {

你可能意味着

for(int x = 0; x < length; x = x+1) {

答案 1 :(得分:0)

你需要的是一个循环,它比较两个字符串并计算它们不相等的地方。

您的逻辑计算两个字符相同的位置数。每当两个字符相等时,您也会打印计数。

你需要的是一个循环,它迭代两个字符串中的字符,比较每个字符并增加不匹配或不同字符的数量。然后通过比较所有字符计算不同的字符后,您将打印出不同字符的计数。

所以基础知识将是:(1)读取每个字符串,(2)检查长度是否相同,(3)如果长度相同则在字符串上循环比较每个字符并增加错误计数每次出现差异时匹配的字符,(4)打印出计数。如果字符串长度不同,那么只需将计数设置为负1(-1),不要费心去比较两个字符串。

要做的事情就是创建一个下划线和星号的字符串,其中每个匹配的字符位置由下划线表示,每个不匹配的字符位置由星号表示,或者字符串可能包含所有匹配的字符和不匹配的字符都将被星号替换。

编辑:添加示例程序

以下示例是对您的程序进行带注释的重写。我做的一个改变是使用一个函数来执行不匹配字符的计数。函数countNonMatchChars ()是一个静态函数,以解决Java的面向对象特性。此函数是实用程序类型函数,而不是类的一部分。任何想要使用它的人都应该可以使用它。

而不是使用var = var + 1;的语法递增变量而不是像var++;中那样使用++的postincrement运算符。

package arraysandstrings;
import java.util.Scanner;

public class so_strings_main {

    // function to compare two strings and count the number
    // of characters that do not match.
    //
    // this function returns an integer indicating the number
    // of characters that did not match or a negative one if the
    // strings are not equal in length.
    //
    // "john"  "john"   returns 0
    // "john1" "john2"  returns 1
    // "mary1" "john1"  returns 4
    // "john"  "john1"  returns -1 (lengths are not equal)
    public static int countNonMatchChars (String s1, String s2)
    {
        // initialize the count to negative one indicating strings unequal in length
        // get the lengths of the two strings to see if any comparison is needed
        int count = -1;
        int word1Length = s1.length();
        int word2Length = s2.length();

        if (word1Length == word2Length) {
            // the lengths of the two strings are equal so we now do our comparison
            // we start count off at zero.  as we find unmatched characters, we
            // will increment our count.  if no unmatched characters found then
            // we will return a count of zero.
            count = 0;
            for(int iLoop = 0; iLoop < word1Length; iLoop++) {
                if (s1.charAt(iLoop) != s2.charAt(iLoop)) {
                    // the characters at this position in the string do not match
                    // increment our count of non-matching characters
                    count++;
                }
            }
        }
        // return the count of non-matching characters we have found.
        return count;
    }

    public static void main (String agrs[]){
        Scanner scanner = new Scanner (System.in);
        System.out.println("Count non-matching characters in two strings.");
        System.out.println("Enter first word");
        String word1 = scanner.next();

        System.out.println("Enter second word");
        String word2 = scanner.next();
        int count = countNonMatchChars (word1, word2);

        if (count < 0) {
            System.out.println ("Words are a diffrent length");
            System.out.println ("   " + word1 + " Has " + word1.length() + " chars");
            System.out.println ("   " + word2 + " Has " + word2.length() + " chars");
        } else {
            System.out.println (count + " different chars");
        }
    }
}

答案 2 :(得分:0)

转换一些代码,添加一些换行符并对逻辑进行2次小调整会产生一个更接近你想要构建的程序。

package arraysandstrings;
import java.util.Scanner;

public class differStrings {
  public static void main (String agrs[]){
    Scanner scanner = new Scanner (System.in);
    System.out.println("Enter a word");
    String word1 = scanner.next();
    System.out.println("Enter another word");
    String word2 = scanner.next();
    int count = 0;
    int length = word1.length();

    for(int x = 0; x < length; x = x+1) {
        if (word1.charAt(x) != word2.charAt(x)) {
            count = count + 1;
        }
    }
    System.out.println (count+" different chars");
  }
}

除了@LouisWasserman指出的for循环外,你看起来还有一些试图找到相同字符的代码。