gpt4 book ai didi

Java无法实现if和else

转载 作者:行者123 更新时间:2023-12-01 18:22:36 27 4
gpt4 key购买 nike

当我输入辅音时,它没有意识到有一个 if 语句。而且当我输入 e 时,它​​意识到它是一个辅音。

此外,当我输入“a”时,它会生成字符串元音的 if 语句和 else 语句。与大写字母“A”相同,但这次它产生两次 else。

import javax.swing.JOptionPane;

public class R
{
public static void main(String args[])
{
String[] vowels = {"a","e","i","o","u"};
String[] vowel = {"A","E","I","O","U"};
String InputVowel = JOptionPane.showInputDialog(null,"Enter a Character: ");
for (int x=0;x<vowels.length;x++)
{
if(InputVowel.equals (vowels[x]))
JOptionPane.showMessageDialog(null,InputVowel+" is a lowercase");
if(InputVowel.equals(vowel[x]))
JOptionPane.showMessageDialog(null,InputVowel+" is an uppercase");
else
x = 5;
JOptionPane.showMessageDialog(null,InputVowel+" is a consaunant");
}
}
}

最佳答案

它无法识别您的 if-statement 的原因是,如果您有多个 if-statement 检查,则需要大括号。如果您有 if-else,则需要大括号。

String[] vowels = {"a","e","i","o","u"};
String[] vowel = {"A","E","I","O","U"};
String InputVowel = JOptionPane.showInputDialog(null,"Enter a Character: ");

for (int x=0;x<vowels.length;x++) {
if(InputVowel.equals (vowels[x])) {
JOptionPane.showMessageDialog(null,InputVowel+" is a lowercase");
} else if(InputVowel.equals(vowel[x])) {
JOptionPane.showMessageDialog(null,InputVowel+" is an uppercase");
} else {
x = 5;
JOptionPane.showMessageDialog(null,InputVowel+" is an consaunant");
}
}

上面的代码应该可以工作。

关于Java无法实现if和else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27233008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com