用户输入一个字符串。
如果猫和狗出现的时间相同,则程序返回 true,否则返回 false。
示例:
catcatdoghotdog
=返回 TRUE
,因为cat = 2
和dog = 2
//我的代码
public String three (String str)
{
int cat = 0;
int dog = 0;
for(int a = 0; a < str.length() - 2; a++)
{
if (str.substring(a, a+3).equals("cat"))
{
cat++;
}
if (str.substring(a, a+3).equals("dog"))
{
dog++;
}
}
if(dog == cat)
{
return "TRUE";
}
else
{
return "FALSE";
}
}
System.out.println("Number of times cat and dogs appear in your word: " , + response.three(word)); // doesn't work...
ERROR
: + operator is undefined for String argument.
任何建议,非常欢迎
最佳答案
要连接字符串和变量,使用+
就足够了。删除,
。
System.out.println("Number of times cat and dogs appear in your word: " + response.three(word));
关于Java返回值取决于字符串中猫和狗的出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47916775/