Java I';我的代码出错:不兼容的类型:int无法转换为布尔行:6 public int countHi(字符串str) { int hiCount=0; 对于(int i=0;i-1:-)或str.contains(“hi”)@shmosel true,如果您只关心字符串是否在那里而不关心它在哪里,那么这可能是一种更好的编写方法。contains的实现很熟悉:returnindexof(s.toString())>-1:-)
我一直在尝试完成一项作业的代码,但我不断地得到错误(标题)。我不是最聪明的,有人能帮我吗?我试图让代码返回代码中“hi”的次数。Java I';我的代码出错:不兼容的类型:int无法转换为布尔行:6 public int countHi(字符串str) { int hiCount=0; 对于(int i=0;i-1:-)或str.contains(“hi”)@shmosel true,如果您只关心字符串是否在那里而不关心它在哪里,那么这可能是一种更好的编写方法。contains的实现很熟悉:returnindexof(s.toString())>-1:-),java,Java,我一直在尝试完成一项作业的代码,但我不断地得到错误(标题)。我不是最聪明的,有人能帮我吗?我试图让代码返回代码中“hi”的次数。字符串。indexOf返回int:找到参数的索引,如果找不到,返回-1。它不会像您预期的那样返回布尔值 要检查是否找到了“hi”,而不是写入: public int countHi(String str) { int hiCount = 0; for (int i=0; i < str.length()-1; i++) { if (str.ind
字符串。indexOf
返回int
:找到参数的索引,如果找不到,返回-1
。它不会像您预期的那样返回布尔值
要检查是否找到了“hi”
,而不是写入:
public int countHi(String str)
{
int hiCount = 0;
for (int i=0; i < str.length()-1; i++)
{
if (str.indexOf("hi"))
{
hiCount++;
i++;
}
}
return hi;
}
你应该写:
if (str.indexOf("hi"))
String.indexOf
在int
中返回:找到参数的索引,如果找不到,则返回-1
。它不会像您预期的那样返回布尔值
要检查是否找到了“hi”
,而不是写入:
public int countHi(String str)
{
int hiCount = 0;
for (int i=0; i < str.length()-1; i++)
{
if (str.indexOf("hi"))
{
hiCount++;
i++;
}
}
return hi;
}
你应该写:
if (str.indexOf("hi"))
首先,对于for循环,为了循环字符串的整个长度,它应该是:
if (str.indexOf("hi") != -1)
首先,对于for循环,为了循环字符串的整个长度,它应该是:
if (str.indexOf("hi") != -1)
什么是str.indexOf(“hi”)
返回的?什么是str.indexOf(“hi”)
返回的?或者str.contains(“hi”)
@shmosel true,如果您只关心字符串是否在那里而不关心它在哪里,那么这可能是一种更好的编写方法。contains
的实现很熟悉:returnindexof(s.toString())>-1
:-)或str.contains(“hi”)
@shmosel true,如果您只关心字符串是否在那里而不关心它在哪里,那么这可能是一种更好的编写方法。contains
的实现很熟悉:returnindexof(s.toString())>-1代码>:-)