Java 检查字符是否为';t等于字母表的前6个字母
您好,我正在尝试想出一个解决方案,以确定我的char变量是否是字母表的前六个字母之一。如果不是,那就是真的 “t”是我的char变量,它包含某个未知值 我所拥有的:Java 检查字符是否为';t等于字母表的前6个字母,java,if-statement,char,Java,If Statement,Char,您好,我正在尝试想出一个解决方案,以确定我的char变量是否是字母表的前六个字母之一。如果不是,那就是真的 “t”是我的char变量,它包含某个未知值 我所拥有的: (t < 'a' || t > 'f' && t < 'A' || t > 'F') (t'f'&t'f') 你的情况有点不对劲,你是说 ((t >= 'g' && t <= 'z') || (t >= 'G' && t <= 'Z')
(t < 'a' || t > 'f' && t < 'A' || t > 'F')
(t<'a'| t>'f'&t<'a'| t>'f')
你的情况有点不对劲,你是说
((t >= 'g' && t <= 'z') || (t >= 'G' && t <= 'Z'))
((t>='g'&&t='g'&&t你的情况有点不对劲,你是说
((t >= 'g' && t <= 'z') || (t >= 'G' && t <= 'Z'))
((t>='g'&&t='g'&&tint数组[]={'a','b','c','d','e','f'};
字符串toCheck='b';
对于(int i=0;iint数组[]={'a','b','c','d','e','f'};
字符串toCheck='b';
对于(int i=0;i,正如您在这里看到的&&
运算符的优先级高于|
,因此您的条件
(t < 'a' || t > 'f' && t < 'A' || t > 'F')
(t<'a'| t>'f'&t<'a'| t>'f')
与
(t < 'a' || (t > 'f' && t < 'A') || t > 'F')
^^^^^^^^^^^^^^^^^^^^
(t<'a'| |(t>'f'&t<'a')| t>'f')
^^^^^^^^^^^^^^^^^^^^
你可能想分成这些部分
((t < 'a' || t > 'f') && (t < 'A' || t > 'F'))
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
((t<'a'| t>'f')&(t<'a'| t>'f'))
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
您还可以使用char-tLower=character.toLowerCase(t)
将字符大小写为小写,只需检查tLower<'a'| | tLower>'f'
,正如您在这里看到的&
运算符的优先级高于|
,因此您的条件
(t < 'a' || t > 'f' && t < 'A' || t > 'F')
(t<'a'| t>'f'&t<'a'| t>'f')
与
(t < 'a' || (t > 'f' && t < 'A') || t > 'F')
^^^^^^^^^^^^^^^^^^^^
(t<'a'| |(t>'f'&t<'a')| t>'f')
^^^^^^^^^^^^^^^^^^^^
你可能想分成这些部分
((t < 'a' || t > 'f') && (t < 'A' || t > 'F'))
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
((t<'a'| t>'f')&(t<'a'| t>'f'))
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
您还可以使用char tLower=character.toLowerCase(t)
将字符大小写为小写,只需检查表达式是否为tLower
((t >= 'a' && t <= 'f') || (t >= 'A' && t <= 'F'))
将起作用。表达式
((t >= 'a' && t <= 'f') || (t >= 'A' && t <= 'F'))
将起作用。如果您想要一些冗长的实现,为了理解,您可以在JDK1.7中尝试
char t='t';
boolean status=true;
switch (String.valueOf(t).toLowerCase())
{
case "a": status=false;
break;
case "b": status=false;
break;
case "c": status=false;
break;
case "d": status=false;
break;
case "e": status=false;
break;
case "f": status=false;
break;
}
if(status){
System.out.println("condition satisfied");
}else{
System.out.println("letter is "+String.valueOf(t)+" and it is between a and f");
}
如果您想要一些冗长的实现,为了便于理解,可以在JDK1.7中尝试
char t='t';
boolean status=true;
switch (String.valueOf(t).toLowerCase())
{
case "a": status=false;
break;
case "b": status=false;
break;
case "c": status=false;
break;
case "d": status=false;
break;
case "e": status=false;
break;
case "f": status=false;
break;
}
if(status){
System.out.println("condition satisfied");
}else{
System.out.println("letter is "+String.valueOf(t)+" and it is between a and f");
}
那么你现在想要什么呢?小心操作顺序。&&binds比| | |更紧。那么你现在想要什么呢?小心操作顺序。&&binds比| | |更紧。对不起,如果我不清楚的话。我不应该是alphabet@DaViDa哈哈,让我编辑一下。等等,它还需要一封信吗?给一个字符变量t已经给定了一个值,写一个表达式,其值为真,当且仅当t不是字母表中的前六个字母之一。(我需要使用a和f)@DaViDa不清楚,但相信他们是在字母表上讨论的chaacters@DaViDa然后接受他的回答:)无论如何,他的回答也会让符号进入,所以要确保像“@”这样的符号是可接受的。如果我不清楚的话,对不起。我不应该是这个单词的前六个字母之一alphabet@DaViDa哈哈,让我来编辑它。等等,它还需要是一个字母吗?给定一个已经给定值的char变量t,如果只有当t不是字母表中的前六个字母之一时(我需要使用a和f)@DaViDa这不清楚,但相信他们是在字母表上说话chaacters@DaViDa然后接受他的回答:)无论如何,他的回答也会让符号进入,所以一定要确保像“@”这样的符号是可接受的哈哈,我喜欢“哦,废话”部分,但不幸的是条件不好哈哈,我喜欢“哦,废话”部分,但不幸的是条件不好