在for循环中不使用Java。

时间:2022-04-26 18:14:23

I've got an assignment to write a a method that accepts two strings as an argument and returns a value of 1, 0, -1 (respectively) if the first string is lexicographicaly before, equal or after the second string, using only the charAt() and length() methods from the String class.

我有一个赋值来编写一个方法,该方法接受两个字符串作为参数,并返回一个值为1、0、-1(分别),如果第一个字符串是lexicicaly之前,等于或在第二个字符串之后,只使用字符串类中的charAt()和length()方法。

The problem is that even though I initialized an int variable in my for loop, it won't recognize them later in the loop when using the charAt() method. The compiler keeps saying "unexpected type. required: variable; found: value" (using BlueJ).

问题是,即使我在for循环中初始化了一个int变量,但在使用charAt()方法时,它在循环中不会识别它们。编译器总是说“意想不到的类型”。要求:变量;发现:价值”(使用BlueJ)。

public class Word {

    private String _st;

    /**
     * range small letters: a = 97, z = 122.
     * 
     * range capital letters: A = 65, Z = 90.
     */
    public int myCompare(String s1, String s2) {
        char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

        //1st loop i;
        //2nd loop j;
        //3rd loop k;
        for (int i = 0; i < s1.length(); i++) {
            _st = new String (s1);
            if (_st.charAt(i) < 97) {
               _st.charAt(i) += 32;
            }

            for (int j = 0; alphabet[j] == _st.charAt(i); j++) {
                int x = alphabet[j];
                _st.charAt(i) = x;
            }
        }
        return 1; // temporaray.
        /*
        if (s1.charAt(0) < s2.charAt(0)) {
            return 1;
        }
        else if (s1.charAt(0) > s2.charAt(0)) {
            return -1;
        }
        else {
            return 0;
        }*/
    }
}

So what exactly am I doing wrong? Thanks for any help.

我到底做错了什么?感谢任何帮助。

4 个解决方案

#1


3  

_st.charAt(i) += 32;

_st.charAt(我)+ = 32;

this is meaningless.

这是毫无意义的。

_st.charAt(i) returns a char. Adding 32 to that char has no meaning if you don't store the result in some variable.

_st.charAt(i)返回一个字符。如果不将结果存储在某个变量中,那么将32添加到这个char中就没有意义了。

You can replace it with :

你可以用:

 char c = _st.charAt(i);
 c += 32;

#2


1  

Besides, that _st.charAt(i) += 32; does nothing with the result, your algorithm is a little bit complicated. You should do the comparison with the two strings in place and not via an alphabet:

除此之外,那就是圣查拉特(i) += 32;没有结果,你的算法有点复杂。你应该用两个字符串来做比较,而不是通过一个字母表:

public static int myCompare(String string1, String string2) {
    int result = 0;
    int differenceCases='a'-'A';
    int end = (string1.length() > string2.length())
            ? string2.length()
            : string1.length();
    for (int index = 0; index < end; index += 1) {
        int char1=(string1.charAt(index)>'Z')?(string1.charAt(index)-differenceCases):(string1.charAt(index));
        int char2=(string2.charAt(index)>'Z')?(string2.charAt(index)-differenceCases):(string2.charAt(index));
        int difference = char1-char2;
        if (difference == 0 || abs(difference) == differenceCases) continue;
        return (difference < 0) ? 1 : -1;
    }
    return result;
}

1) You should take care which one of the two strings is the longer one, to prevent Out of bounds problems.

1)你应该注意两个字符串中的哪一个较长,以防止出现边界问题。

int end = (string1.length() > string2.length())
            ? string2.length()
            : string1.length();

2) You could make use of the ASCII-Values of the two strings.

2)可以使用两个字符串的ascii值。

2aa) Calculate different positions

2 aa)计算不同位置

2ab) Make sure, you have the same case for comparison

2ab)确保,你有同样的理由进行比较。

int char1=(string1.charAt(index)>'Z')?(string1.charAt(index)-differenceCases):(string1.charAt(index));

If it is lowercase substract the difference between cases ('a'-'A').

如果它是小写的,那么在case ('a'-' a')之间的区别。

2ba) If the difference is 0, you have two chars of the same kind.

如果差额是0,你就有两张相同的。

2bb) If the absolute difference is 'a'-'A', you have the same letter, but mixed cases

如果绝对的差别是“a”-“a”,你有相同的字母,但是混合的情况。

2bc) In each case, it is no difference, so skip to the next letter

在每种情况下,都没有区别,所以跳到下一个字母。

2c) If you have a difference, which is greater than 'a'-'A', check if it is negative, so string2.charAt(i) is greater than string1.charAt(i), which means it comes after.

如果你有区别,比“a”更大,检查它是否为负数,那么string2.charAt(i)大于string1.charAt(i),这意味着它的出现。

#3


0  

enjoy this working methode

享受这工作方法

public static int compareWords(String mot1,String mot2){

    String toLowerCase = mot1.toLowerCase();
    String toLowerCase1 = mot2.toLowerCase();
    int answer=0,lenght=(mot1.length()<mot2.length())?mot1.length():mot2.length();

    for (int i = 0; i < lenght; i++) {

        if (toLowerCase.charAt(i)<toLowerCase1.charAt(i)) {
            answer=1;
          //  System.out.println("loop1:"+answer);
        }else{
            if (toLowerCase.charAt(i)>toLowerCase1.charAt(i)) {

            answer=-1;
            break;
            }
        }
    }
    if (answer==0) {
        if (mot1.length()<mot2.length()) {
            answer=1;
         //   System.out.println("if1:"+answer);
        }else {
            if (mot1.length()==mot2.length()) {
                answer=0;
            }else answer=-1;
        }
    }
   // System.out.println("answer:"+answer);
    return answer;
}

#4


0  

You can have a look at java.lang.String.compareTo() and java.lang.String.compareToIgnoreCase()? If you are ok to use standard Java library functions, you can use those directly, if not, you can refer to implementations of the same.

您可以查看java.lang. compareto()和java.lang. comparetoignorecase ()?如果您可以使用标准的Java库函数,您可以直接使用这些函数,如果不是,您可以参考相同的实现。

I know it's a shortcut, but it'll provide you a better understanding.

我知道这是捷径,但它能让你更好地理解。

#1


3  

_st.charAt(i) += 32;

_st.charAt(我)+ = 32;

this is meaningless.

这是毫无意义的。

_st.charAt(i) returns a char. Adding 32 to that char has no meaning if you don't store the result in some variable.

_st.charAt(i)返回一个字符。如果不将结果存储在某个变量中,那么将32添加到这个char中就没有意义了。

You can replace it with :

你可以用:

 char c = _st.charAt(i);
 c += 32;

#2


1  

Besides, that _st.charAt(i) += 32; does nothing with the result, your algorithm is a little bit complicated. You should do the comparison with the two strings in place and not via an alphabet:

除此之外,那就是圣查拉特(i) += 32;没有结果,你的算法有点复杂。你应该用两个字符串来做比较,而不是通过一个字母表:

public static int myCompare(String string1, String string2) {
    int result = 0;
    int differenceCases='a'-'A';
    int end = (string1.length() > string2.length())
            ? string2.length()
            : string1.length();
    for (int index = 0; index < end; index += 1) {
        int char1=(string1.charAt(index)>'Z')?(string1.charAt(index)-differenceCases):(string1.charAt(index));
        int char2=(string2.charAt(index)>'Z')?(string2.charAt(index)-differenceCases):(string2.charAt(index));
        int difference = char1-char2;
        if (difference == 0 || abs(difference) == differenceCases) continue;
        return (difference < 0) ? 1 : -1;
    }
    return result;
}

1) You should take care which one of the two strings is the longer one, to prevent Out of bounds problems.

1)你应该注意两个字符串中的哪一个较长,以防止出现边界问题。

int end = (string1.length() > string2.length())
            ? string2.length()
            : string1.length();

2) You could make use of the ASCII-Values of the two strings.

2)可以使用两个字符串的ascii值。

2aa) Calculate different positions

2 aa)计算不同位置

2ab) Make sure, you have the same case for comparison

2ab)确保,你有同样的理由进行比较。

int char1=(string1.charAt(index)>'Z')?(string1.charAt(index)-differenceCases):(string1.charAt(index));

If it is lowercase substract the difference between cases ('a'-'A').

如果它是小写的,那么在case ('a'-' a')之间的区别。

2ba) If the difference is 0, you have two chars of the same kind.

如果差额是0,你就有两张相同的。

2bb) If the absolute difference is 'a'-'A', you have the same letter, but mixed cases

如果绝对的差别是“a”-“a”,你有相同的字母,但是混合的情况。

2bc) In each case, it is no difference, so skip to the next letter

在每种情况下,都没有区别,所以跳到下一个字母。

2c) If you have a difference, which is greater than 'a'-'A', check if it is negative, so string2.charAt(i) is greater than string1.charAt(i), which means it comes after.

如果你有区别,比“a”更大,检查它是否为负数,那么string2.charAt(i)大于string1.charAt(i),这意味着它的出现。

#3


0  

enjoy this working methode

享受这工作方法

public static int compareWords(String mot1,String mot2){

    String toLowerCase = mot1.toLowerCase();
    String toLowerCase1 = mot2.toLowerCase();
    int answer=0,lenght=(mot1.length()<mot2.length())?mot1.length():mot2.length();

    for (int i = 0; i < lenght; i++) {

        if (toLowerCase.charAt(i)<toLowerCase1.charAt(i)) {
            answer=1;
          //  System.out.println("loop1:"+answer);
        }else{
            if (toLowerCase.charAt(i)>toLowerCase1.charAt(i)) {

            answer=-1;
            break;
            }
        }
    }
    if (answer==0) {
        if (mot1.length()<mot2.length()) {
            answer=1;
         //   System.out.println("if1:"+answer);
        }else {
            if (mot1.length()==mot2.length()) {
                answer=0;
            }else answer=-1;
        }
    }
   // System.out.println("answer:"+answer);
    return answer;
}

#4


0  

You can have a look at java.lang.String.compareTo() and java.lang.String.compareToIgnoreCase()? If you are ok to use standard Java library functions, you can use those directly, if not, you can refer to implementations of the same.

您可以查看java.lang. compareto()和java.lang. comparetoignorecase ()?如果您可以使用标准的Java库函数,您可以直接使用这些函数,如果不是,您可以参考相同的实现。

I know it's a shortcut, but it'll provide you a better understanding.

我知道这是捷径,但它能让你更好地理解。