这个问题在这里已经有了答案:
What does "Incompatible types: void cannot be converted to ..." mean?
(1 个回答)
5年前关闭。
各位程序员,大家好,我需要一些关于学校作业文本的帮助。
使用此文本,我收到错误消息:无法编译的源代码 - 不兼容的类型: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");
}
}
最佳答案
如果你使用任何 IDE,这条线会向你提示
String finished= JOptionPane.showMessageDialog
因为
showMessageDialog
不返回任何东西,它是一个 void 方法,所以没有完成的 String 可以从中获取,它只是显示一个弹出窗口
关于java - 不兼容的类型 : void cannot be converted to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37528860/