【发布时间】:2017-01-04 22:22:47
【问题描述】:
我需要帮助调试这个;问题在第 10-13 行。它说我无法从 String 转换为 Int,但我不知道如何修复它。代码在给定的字符串中查找单词“bug”。
public class BugHunter extends ConsoleProgram
{
public void run()
{
String test1 = "Debug";
String test2 = "bugs bunny";
String test3 = "boogie";
String test4 = "baby buggie";
int index1 = findBug(test1);
int index2 = findBug(test2);
int index3 = findBug(test3);
int index4 = findBug(test4);
printBug(test1, index1);
printBug(test2, index2);
printBug(test3, index3);
printBug(test4, index4);
}
// Returns the index of the String "bug" inside the String str
// If str does not contain the String "bug", returns -1
public String findBug(String str)
{
str.indexOf("bug");
}
public void printBug(String test, int index)
{
if(index != -1)
{
System.out.println(test + " has a bug at index " + index);
}
else
{
System.out.println(test + " has no bugs");
}
}
}
【问题讨论】: