gpt4 book ai didi

java - 使用 Java 的 ChickenCounter 程序和 ChickenCounter 测试器

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:19 26 4
gpt4 key购买 nike

// The countChicken() method should count the number of occurrences of the word chicken (or some other word) in the string passed to it.
// Use recursion to accomplish this (countChicken() should call itself whenever "chicken" is found).
// Make this class flexible by passing the word you want to search for as a parameter to the constructor. If
// nothing is passed to the constructor (there is no parameter), then the search word should be "chicken".
import static java.lang.System.*;

public class ChickenCounter
{
//Create two constructor methods here, one with no parameter and one with one parameter
private String word;
private int length;
private int x;
public ChickenCounter()
{
word = "chicken";
length = 7;
x = 0;
}
public ChickenCounter(String z)
{
word = z;
length = word.length();
x = 0;
}
// The parameter bigString below should contain the long string that you want to find the word chicken in
public int countChickens(String bigString)
{
if(bigString.length() <= length)
{
if(bigString.equals(word))
{
return 1;
}

else
{
String temp = bigString.substring(x, length-1);//line that is highlighted when given the error
if(temp.equals(word))
{
bigString = bigString.substring(x, bigString.indexOf(word)) + bigString.substring(bigString.indexOf(word)); //dont know if this is the correct syntax of the method indexOf()
return countChickens(bigString) + 1;
}
else
{
x++;
return countChickens(bigString); //this line is also part of the problem
}
}
}
return 0;
}
}


import static java.lang.System.*;


public class ChickenCounterTester
{
public static void main(String args[])
{
ChickenCounter counter = new ChickenCounter();

System.out.println(counter.countChickens("itatfun")); //0 <--what it supposed to output

System.out.println(counter.countChickens("itatchickenfun")); //1

System.out.println(counter.countChickens("chchickchickenenicken")); //3

System.out.println(counter.countChickens("chickchickfun")); //0

System.out.println(counter.countChickens("chickenbouncetheballchicken")); //2


//pass the word to search for to the object

counter = new ChickenCounter("java");

System.out.println("");

System.out.println(counter.countChickens("jjajavavaavaisfun")); //3

System.out.println(counter.countChickens("I want some jajavava")); //2

}

}

输出:01207

12

我不断收到索引越界错误,提示其超出范围 -1。该程序的目的是尝试在给我的字符串中找到某个单词。我的老师使用这个词作为此代码的示例。他想要遍历每一行代码并找到单词“chicken”(如果找到),则将其添加到计数器变量中(如果没有则将其删除),然后再次遍历字符串并尝试再次找到该单词。

最佳答案

你的教授希望你使用递归来计算字符串中单词出现的次数。您当前的 countChickens 方法不包含正确的逻辑。它应该类似于以下内容:

public int countChickens(String bigString){
if(bigString.contains(word)){
return 1 + countChickens(
bigString.substring(
bigString.indexOf(word)+word.length()));

}else{
return 0;
}
}

关于java - 使用 Java 的 ChickenCounter 程序和 ChickenCounter 测试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42477245/

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