比较两个字符串与布尔? [英] Comparing two strings with boolean?
问题描述
我尝试这个code的写作练习,我这样就失去了!
I was trying this code writing exercise and I am so lost!
这次演习是:
完成这需要两个字符串和一个布尔值,输入方法。如果布尔是真实的,这种方法的第一个两个字符串比较,不考虑大小写(大写/小写)。两个字符串被认为是相等忽略大小写,如果他们是相同的长度,并在两个字符串对应的字符等于忽略大小写。
Complete the method which takes two Strings and one boolean as input. If the boolean is true, this method compares the first two Strings, ignoring case considerations (uppercase/lowercase). 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.
如果布尔是假的,这个方法应该比较两个字符串,返回true,如果第一个字符串再presents字符作为第二个字符串的相同序列,否则为false。
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.
请注意: compareTwoStrings(HELLO,,FALSE)
应该返回false
Note: compareTwoStrings("HELLO", "", false)
should return false.
和这里是我的尝试:
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. 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.
这篇关于比较两个字符串与布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!