gpt4 book ai didi

string - 定位字符的下一次出现 ===>(发现 Unit 需要 boolean 值,错误)

转载 作者:行者123 更新时间:2023-12-04 17:38:13 25 4
gpt4 key购买 nike

我知道这个问题已被问过很多次,但我不断收到:found Unit required Boolean 错误。我尝试查看各种解决方案并实现它们(在 SO 中),但它们都不起作用。

感谢帮助!! xx

P.S 我的函数是一个更大程序的一部分,该程序的总体思路是我们有一个缓冲区(其中包含给定的字符串)并且我们在缓冲区中找到字符的出现。如果我们发现函数必须返回事件出现的数字(在本例中为游标)并且还返回 boolean 语句(true ==found an occurrence)else(false == did not find an occurrence),还需要什么).

class buffer(s:String){

private var buffer: StringBuilder = new StringBuilder(s)
private var cursor: Int = 0

//function which checks the occurrence of a given string inside the "buffer"
def ff(c: Char): Boolean = {

var getWord = ""

var Solution = 0

//get String method is used to see if buffer is not empty
if(getString != ""){

for(x <- cursor until buffer.length by 1){
if(buffer.charAt(x)==c)
{
Solution = x

return true;
}

else
return false }
}
}
}

最佳答案

看起来你想要的比你真正要求的要多一点。但是如果你只是想检查一个字符是否存在于一个字符串中,你需要做的就是:

s.exists(c == _)

这里是你的整个例子:

val s = "asdfasdfERerfasdfasfasdfdsfdfa-asdfasdfafg"

def ff(c: Char): Boolean = s.exists(c == _)

println(ff('e')) // -> true
println(ff('x')) // -> false

如果这已经解决了您的问题,或者您是否有其他要求,请告诉我。

关于string - 定位字符的下一次出现 ===>(发现 Unit 需要 boolean 值,错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55667764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com