【发布时间】:2017-04-09 14:34:51
【问题描述】:
(对不起,提前很长的 q - 只是想详细一点!) 大家好, 长期以来,我一直在努力编译这段代码,并且我到处都进行了研究(包括在这里!),但似乎没有任何效果,而且我不断收到错误。我不断调整我定义静态方法的方式以及我一直在 main 方法中添加的内容,但现在我不知道出了什么问题,而且这些错误对我来说没有意义。 正如标题中所说,我需要编写一个包含静态方法的程序,当调用该方法时,将返回一个整数数组,该数组描述输入字符串中元音的数量,无论大小写如何。 a 在 0 索引处一直到 u 在 4 索引处。所以 vowelCount ("EeioOoua") 将返回 {1, 2, 1, 3, 1} 的数组。
这个问题不仅与我本人有关,因为您通常希望能够计算对象内部元素的数量,然后将其组织成数组。我个人还没有看到堆栈溢出问题以这种方式/使用这些参数来解决这个问题。任何可以提供的帮助或指导将不胜感激。
请查看我的代码和下面的错误:
这是我的完整代码:
import java.util.*;
import java.io.*;
import java.util.Arrays;
class Vowels
{
public static void main (String [] args)
{
vowelCount(String [] letters);
}
private static String [] vowelCount (String [] letters)
{
String [] letters = new String[5];
for (int i = 0; i < letters.length; i++)
{
if (letters.charAt(i) == 'A' || letters.charAt(i) == 'a')
{
letters[0] = letters[0]++;
}
if (letters.charAt(i) == 'E' || letters.charAt(i) == 'e')
{
letters[1] = letters[1]++;
}
if (letters.charAt(i) == 'I' || letters.charAt(i) == 'i')
{
letters[2] = letters[2]++;
}
if (letters.charAt(i) == 'O' || letters.charAt(i) == 'o')
{
letters[3] = letters[3]++;
}
if (letters.charAt(i) == 'U' || letters.charAt(i) == 'u')
{
letters[4] = letters[4]++;
}
}
return letters;
}
}
使用上面的代码,我只有两个错误:
Vowels.java:9: error: '.class' expected
vowelCount(String [] letters);
^
Vowels.java:9: error: ';' expected
vowelCount(String [] letters);
但是,根据我所看到的(我认为是堆栈溢出)
我刚刚将 'vowelCount (String [] letters)' 更改为
String [] userInput = {"I hate this problem set"};
String [] counting = vowelCount (userInput);
vowelCount (userInput);
现在这些都是我的错误(现在我有 16 个错误):
Vowels.java:16: error: variable letters is already defined in method vowelCount(String[])
String [] letters = new String[5];
^
Vowels.java:19: error: cannot find symbol
if (letters.charAt(i) == 'A' || letters.charAt(i) == 'a')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:19: error: cannot find symbol
if (letters.charAt(i) == 'A' || letters.charAt(i) == 'a')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:21: error: bad operand type String for unary operator '++'
letters[0] = letters[0]++;
^
Vowels.java:23: error: cannot find symbol
if (letters.charAt(i) == 'E' || letters.charAt(i) == 'e')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:23: error: cannot find symbol
if (letters.charAt(i) == 'E' || letters.charAt(i) == 'e')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:25: error: bad operand type String for unary operator '++'
letters[1] = letters[1]++;
^
Vowels.java:27: error: cannot find symbol
if (letters.charAt(i) == 'I' || letters.charAt(i) == 'i')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:27: error: cannot find symbol
if (letters.charAt(i) == 'I' || letters.charAt(i) == 'i')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:29: error: bad operand type String for unary operator '++'
letters[2] = letters[2]++;
^
Vowels.java:31: error: cannot find symbol
if (letters.charAt(i) == 'O' || letters.charAt(i) == 'o')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:31: error: cannot find symbol
if (letters.charAt(i) == 'O' || letters.charAt(i) == 'o')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:33: error: bad operand type String for unary operator '++'
letters[3] = letters[3]++;
^
Vowels.java:35: error: cannot find symbol
if (letters.charAt(i) == 'U' || letters.charAt(i) == 'u')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:35: error: cannot find symbol
if (letters.charAt(i) == 'U' || letters.charAt(i) == 'u')
^
symbol: method charAt(int)
location: variable letters of type String[]
Vowels.java:37: error: bad operand type String for unary operator '++'
letters[4] = letters[4]++;
【问题讨论】:
-
将您的参数名称更改为 letters1 或任何其他但不是字母,您还需要了解更多的数组,因为在您的第 19 行错误中它应该包含一个数组元素 letters[i]..charAt(0 )
-
你有没有一一经历过错误?首先,您正在重新定义论点,因此没有意义。其次,
charAt是String的方法!letters不是字符串。你不能在字符串上使用++! -
为此使用数组会使事情变得复杂。一个名为“VowelCounter”的类会更容易。您是否坚持使用数组?
标签: java arrays for-loop methods