【发布时间】:2016-05-30 15:07:44
【问题描述】:
各位程序员,大家好,我需要一些关于学校作业文本的帮助。
使用此文本我收到错误消息:无法编译的源代码 - 不兼容的类型:void 无法转换为 java.lang.String
我的意图是程序将获取一个句子,然后计算元音和辅音的数量并在对话窗口中显示它们。
我对编程很陌生,所以答案对你们所有人来说可能都很明显,但非常感谢您的帮助:D
所以要清楚,我的问题如下。如何修复 void to string 错误?
public static void main (String args[]) {
String string1= JOptionPane.showInputDialog("Write a sentence.");
int count = 0;
int vowels = 0;
int consonants = 0;
for (int i = 0; i <string1.charAt(0); i++) {
char ch = string1.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u')
{
vowels++;
}
else
{
consonants++;
}
String finished= JOptionPane.showMessageDialog(null,"You have written " +vowels+ "vowels and you have written " +consonants+ "consonants");
}
}
【问题讨论】:
-
错误告诉你什么?
-
我相信你的意思是
i <string1.length() -
错误应该告诉一个行号它发生在哪里。转到那条线,看看each method returns 是什么,以及你试图用它做什么。您很快就会发现错误。
标签: java string swing error-handling void