java - 尝试使用带有方法的子字符串作为参数,但不断出现错误
问题描述
我一直在尝试使用子字符串返回一行代码,但每次我尝试编译时都会收到错误“找不到符号 - 方法 findFirstVowel()”它出现在最后一行代码中。为什么这不起作用?findFirstVowel 应该返回一个整数,对吗?而且这个方法也不需要输入 - 所以参数应该是(0,findFirstVowel的值)。有谁知道如何解决这一问题?谢谢!
public class words
{
private String w;
/**
* Default Constructor for objects of class words
*/
public words()
{
// initialise instance variables
w="";
}
/**
* Assignment constructor
*/
public words(String assignment)
{
w=assignment;
}
/**
* Copy constructor
*/
public words(words two)
{
w=two.w;
}
/**
* Pre: 0<=i<length( )
* returns true if the character at location i is a vowel (‘a’, ‘e’, ‘i', ‘o’, ‘u’ only), false if not
*/
public boolean isVowel(int i)
{
char vowel = w.charAt(i);
return vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u';
}
/**
* determines whether the first vowel in the String is at location 0, 1, 2, or 3 (don’t worry about exceptions)
*/
public int findFirstVowel()
{
if (isVowel(0))
return 0;
else if (isVowel(1))
return 1;
else if (isVowel(2))
return 2;
else if (isVowel(3))
return 3;
else
return -1;
}
/**
* returns the Pig-Latin version of the String
*/
public String pigify()
{
return w.substring(w.findFirstVowel()) + w.substring(0,w.findFirstVowel()) + "ay";
}
解决方案
findFirstVowel
不是 String 类的一部分,它是你的类的一部分。所以不要调用它w
是 String 类的一个实例
这是固定代码
public String pigify() {
return w.substring(findFirstVowel()) + w.substring(0, findFirstVowel()) + "ay";
}
推荐阅读
- postgresql - 复杂 postgres 查询的分页
- c# - 调试时 Visual Studio 2019 未启动控制台应用程序窗口
- angular - 角度反应形式,如何知道 valueChanges 是否由用户触发?
- android - 键盘上推显示
- python - 将所有关键字仅参数传递给其他函数
- android - 如何在 RecyclerView 适配器列表 Kotlin 中的适配器内实现警报对话框?
- java - BoxCars Java 程序 - 初学者。奇怪的问题:程序不在控制台中运行,但它在 Eclipse Java 中没有列出任何错误
- html - 添加html时div向下移动
- java - Java Swing - ScheduledExecutor 服务停止在用户输入上工作
- content-security-policy - 内容安全策略:页面的设置阻止了资源的加载 data:image/svg+xml;base64,PHN2ZyB3aWR0aD... (“default-src”)