简体   繁体   English

比较两个字符串与布尔值?

[英]Comparing two strings with boolean?

I was trying this code writing exercise and I am so lost! 我正在尝试进行此代码编写练习,但我很迷茫!

The exercise is: 练习是:

Complete the method which takes two Strings and one boolean as input. 完成将两个String和一个布尔作为输入的方法。 If the boolean is true, this method compares the first two Strings, ignoring case considerations (uppercase/lowercase). 如果布尔值为true,则此方法将比较前两个String,而忽略大小写考虑(大写/小写)。 Two Strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two Strings are equal ignoring case. 如果两个字符串的长度相同,则认为它们相等而忽略大小写,并且两个字符串中的相应字符忽略大小写相等。

If the boolean is false, this method should compare two Strings and return true if the first String represents the same sequence of characters as the second String, otherwise false. 如果布尔值为false,则此方法应比较两个String,如果第一个String表示与第二个String相同的字符序列,则返回true。

Note: compareTwoStrings("HELLO", "", false) should return false. 注意: compareTwoStrings("HELLO", "", false)应该返回false。

And here is my attempt: 这是我的尝试:

public boolean compareTwoStrings (String a, String b, boolean isIgnoreCase) 
{ 
    if (a.equalsIgnoreCase(b)) {
        return (isIgnoreCase==true);
    }
    else if (a.equals(b)) {
       return (isIgnoreCase==false);
    }
}

It doesn't even compile, but even if it did, I'm sure it wouldn't work. 它甚至不编译,但是即使可以编译,我也确信它不会起作用。

You're doing it backwards. 您正在倒退。 The subject says: if the boolean is true, then do this, else, then do that. 主题说:如果布尔值是true,则执行此操作,否则执行该操作。 So you should program it the same way: 因此,您应该以相同的方式对其进行编程:

if (isIgnoreCase) {
    return ...
}
else {
    return ...
}

The rest is left as an exercise. 剩下的作为练习。 You should be able to figure it out by yourself. 您应该能够自己弄清楚。

I think the requested solution is the following method: 我认为要求的解决方案是以下方法:

public boolean compareTwoStrings(String a, String b, boolean isIgnoreCase)
{
    if (isIgnoreCase)
    {
        return a.equalsIgnoreCase(b);
    }

    return a.equals(b);
}

Your method das not compile because the Java compiler supposes that unter some circumstances both if conditions evalutes to false . 您的方法无法编译,因为Java编译器假定在某些情况下(如果条件评估为false都不会。 Because of that the compiler can not ensure that at least one return statement is reachable. 因此,编译器无法确保至少有一个return语句是可到达的。

I think this is what you are looking for. 我认为这就是您想要的。

public static void myMethod(String str, String str2, boolean bool) {

    if (bool) {
        str = str.toLowerCase();
        str2 = str2.toLowerCase();
        if (str.equals(str2))
            System.out.println("strings equal");
    }
    else {
        System.out.println("false condition");
    }
}

This could be the solution for your problem: 这可能是您的问题的解决方案:

public boolean compareTwoStrings (String a, String b, boolean isIgnoreCase){ 
  if (isIgnoreCase) 
    return (a.toLowerCase()).equals(b.toLowerCase());        
  else 
   return a.equals(b);      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM