[英]Why does this Java Switch-Case not work?
So, all variables in the conditions are static strings. 因此,条件中的所有变量都是静态字符串。
type
itself is a string in fact. type
本身实际上是一个字符串。
switch(type) {
case (INT || TINYINT):
preparedStatement = setInteger(preparedStatement, value, index);
break;
case (BIGINT || LONG):
preparedStatement = setLong(preparedStatement, value, index);
break;
case (DATETIME || TIMESTAMP):
preparedStatement = setTimestamp(preparedStatement, value, index);
break;
case (MEDIUMTEXT || ENUM || TEXT || LONGTEXT || VARCHAR):
preparedStatement = setString(preparedStatement, value, index);
break;
}
First, switch
statements on strings are supported in Java 7+, but not in Java 6 and before. 首先,在Java 7+中支持在字符串上的
switch
语句,但在Java 6及更高版本中不支持。
Next, the ||
接下来,
||
operator (the logical-OR operator) only works on boolean
values, not String
values. 运算符(逻辑或运算符)仅适用于
boolean
值,而不适用于String
值。 But you can get the same code to be run on multiple cases by listing the cases and not breaking until past the relevant code: 但是,您可以通过列出案例并在不超过相关代码的情况下中断代码,来在多个案例上运行相同的代码:
switch(type) {
case INT:
case TINYINT:
// This code will run for INT and TINYINT only.
preparedStatement = setInteger(preparedStatement, value, index);
break;
case BIGINT:
case LONG:
// This code will run for BIGINT and LONG only.
preparedStatement = setLong(preparedStatement, value, index);
break;
// etc.
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
return typeOfDay;
}
Further I have never seen an OR
statement inside of a switch
like that. 此外,我从未见过像这样的
switch
内部的OR
语句。 I would highly recommend not doing that. 我强烈建议您不要这样做。
假设您使用的是Java SE 7(或更高版本),并且常量是static final String
,则语法不是Java。
case INT: case TINYINT:
What does this expression evaluate to? 此表达式的计算结果是什么?
INT || TINYINT
What are the datatypes for INT
and TINYINT
INT
和TINYINT
的数据类型是什么
I've only ever seen switch
used with some primitives (and new in Java 7, string) literals or variables declared as final
. 我只见过将
switch
与某些原语(Java 7中的新功能)和声明为final
变量一起使用。
If this isn't throwing a compile error, then the ||
如果这没有引发编译错误,则
||
operator must be defined for whatever datatype those are. 必须为这些数据类型定义运算符。 But unless that's somehow being resolved at compile time, that operator is not going to be allowed.
但是除非以某种方式在编译时解决该问题,否则将不允许该运算符。 (Again, this might be something new in Java 7 I'm not aware of.)
(同样,这可能是我不知道的Java 7中的新功能。)
If you are trying to do " or " logic, the normative pattern (in pre-7 versions of Java at least), is: 如果您尝试执行“ 或 ”逻辑,则规范模式(至少在Java的7之前版本中)为:
switch(type) {
case INT:
case TINYINT:
preparedStatement = setInteger(preparedStatement, value, index);
break;
case BIGINT:
case LONG:
preparedStatement =
break;
在Java 7及更高版本中受支持
You cannot use logical operators in switch statements, even with Strings. 即使在使用String的情况下,也不能在switch语句中使用逻辑运算符。 You can only test one case at a time.
您一次只能测试一个案例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.