1

我现在是学生,所以我还在学习。我很快就学会了 VB,但另一方面,我很困惑它是简单的 Java。

这次给我的作业让我很困惑“写一个方法来确定两个字符串不同的位置数。例如,”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");
}
4

3 回答 3

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

你大概是说

for(int x = 0; x < length; x = x+1) {
于 2013-04-17T23:24:54.507 回答
0

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

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

听起来您需要的是一个循环,该循环遍历两个字符串中的字符,比较每个字符并增加不匹配或不同字符的计数。然后通过比较所有字符获得不同字符的计数后,您将打印出不同字符的计数。

所以基础是:(1)读取每个字符串,(2)检查长度是否相同,(3)如果长度相同,则遍历字符串比较每个字符并增加每个不匹配字符的计数时间有差异,(4)打印出计数。如果字符串长度不同,则只需将计数设置为负一 (-1),无需费心比较两个字符串。

最好的做法是创建一个下划线和星号组成的字符串,其中每个匹配的字符位置用下划线表示,每个不匹配的字符位置用星号表示,或者字符串可能包含所有匹配的字符和不匹配的字符将被星号替换。

编辑:添加示例程序

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

此外,不是使用 I 的语法递增变量,var = var + 1;而是使用 ++ 的后递增运算符,如 in var++;

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");
        }
    }
}
于 2013-04-17T23:39:20.750 回答
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 循环之外,您还有试图查找相同字符的代码。

于 2013-04-17T23:39:38.630 回答