【发布时间】:2015-04-13 14:55:03
【问题描述】:
请原谅我,我是编程新手,但完全享受我迄今为止所学到的东西。所以我的问题是;我正在尝试编写一个程序,提示用户输入一个字母,然后返回该字母是元音还是辅音。问题是我无法让它接受字母输入。如果强制如“user_input = 'a'”那样,它将接受它。但不是作为一种选择。我知道缺少一些简单的东西。
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int user_input;
System.out.print("Enter a letter: ");
user_input = in.nextInt();//<--line of error
if (user_input == 'a')
{
System.out.println("Vowel");
}
else if (user_input =='e')
{
System.out.println("Vowel");
}
else if (user_input =='i')
{
System.out.println("Vowel");
}
else if (user_input =='o')
{
System.out.println("Vowel");
}
else if (user_input =='u')
{
System.out.println("Vowel");
}
else
{
System.out.println("Consonant");
}
}
提前感谢您的宝贵时间。
【问题讨论】:
-
见 Scanner.next() - docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
标签: java