如我之前所说,如何测试输入的字符是否为参数之一?我已经编写了这段代码,但它似乎并没有很好地运行(或者根本没有运行),没有错误。此外,我需要使用我在这里使用的基本代码。这是为了学校而编写的,如果我们使用他们没有教过的东西,我们就会失分(该死的学校)。
class doody
{
public static void main(String[] args)
{ char i;
char input='D';
for(i='A';i<='Z';i++)//check if uppercase
{
if(input==i){
System.out.println("Uppercase");
switch(input){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println("Vowel"); break;
default: System.out.println("Not a vowel"); break;}
}
for(i='a';i<='z';i++)//check if lowercase
{
if(input==i){
System.out.println("Lowercase");
switch(input){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("Vowel"); break;
default: System.out.println("Not a vowel"); break;
}}
for(i='0';i<='9';i++)//check if number
{
if(input==i)
System.out.println("Number");
}
}
}}}
编辑:这是我今天拼凑出来的一些代码。更为简单。我不知道为什么之前没有想到,可能是因为当时还有点儿迷糊,而且已经很晚了。
class doody
{
public static void main(String[] args)
{
char input='$';//input here.
boolean lorn=false;
if(input>='a'&&input<='z')
{System.out.println("Lowercase");
lorn=true;
if(input=='a')System.out.println("Vowel.");
if(input=='e')System.out.println("Vowel.");
if(input=='i')System.out.println("Vowel.");
if(input=='o')System.out.println("Vowel.");
if(input=='u')System.out.println("Vowel.");
}
if(input>='A'&&input<='Z')
{System.out.println("Uppercase");
lorn=true;
if(input=='A')System.out.println("Vowel.");
if(input=='E')System.out.println("Vowel.");
if(input=='I')System.out.println("Vowel.");
if(input=='O')System.out.println("Vowel.");
if(input=='U')System.out.println("Vowel.");
}
if(input>='0'&&input<='9')
{
lorn=true;
System.out.println("Number");
}
if(lorn==false)System.out.println("It is a special character");
}
}
}
。 - JB Nizet