gpt4 book ai didi

java 计算字符串参数存储在对象中的次数

转载 作者:行者123 更新时间:2023-12-01 22:32:34 30 4
gpt4 key购买 nike

对于方法public int count(String str)
如何返回字符串参数str存储在对象中的次数

我已经编写了以下代码,但我认为它不应该这样做。

public int count(String str)
{
int count = 0;
// if the Object contains str
if(contains(str)) {
count++;
else {
add(str); //add str to Object
count++;
}
return count;
}

当我在命令行中输入“test”时,count1。但当我再次输入“测试”时,计数 仍为1。 (假设应该是2)。

提前谢谢您。

最佳答案

然后将 count 变量声明为全局变量。

关于java 计算字符串参数存储在对象中的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27415247/

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