我目前是学生,所以我还在学习。 我很快就选择了VB,但另一方面,它却是简单的Java。

这次分配给我的作业让我感到困惑:“编写一种方法来确定两个字符串所不同的位置数。例如,“ Peace”和“ Piece”在两个位置上有所不同。该方法声明为int compare(String word1,字符串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");

}

在运用了Iv从您的回答中获得的知识之后,我遇到了最后一行的问题:

 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");
}

===============>>#1 票数:1

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

你可能是说

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

===============>>#2 票数:0

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

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

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

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

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

编辑:添加示例程序

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

也不要使用var = var + 1;的语法来递增变量var = var + 1; 我改为像var++;一样使用++的postincrement运算符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");
        }
    }
}

===============>>#3 票数: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循环外,您还有试图查找相同字符的代码。

  ask by Ozzyfan2323 translate from so

未解决问题?本站智能推荐: