将IF仪链更改为开关表达
我正在尝试将以下转换为
语句转换为switch> switch
expression 。
public static int parseDays(String durationString) {
String[] parts = durationString.split(" ");
if (parts.length == 2) {
return unitValueCalculation(parts[1], parts[0]);
}
else if (parts.length == 1)
{
if (parts[0].equalsIgnoreCase("once")) return 1;
}
return 0;
}
这就是我所拥有的:
public static int parseDays(String durationString) {
switch (durationString) {
case durationString.split(" ").length == 2 -> unitValueCalculation(parts[1], parts[2]);
}
不过,我不确定该如何处理案件陈述。
任何想法都将不胜感激。
I'm trying to convert the below if
statement into a switch
expression.
public static int parseDays(String durationString) {
String[] parts = durationString.split(" ");
if (parts.length == 2) {
return unitValueCalculation(parts[1], parts[0]);
}
else if (parts.length == 1)
{
if (parts[0].equalsIgnoreCase("once")) return 1;
}
return 0;
}
This is what I've got:
public static int parseDays(String durationString) {
switch (durationString) {
case durationString.split(" ").length == 2 -> unitValueCalculation(parts[1], parts[2]);
}
I'm not sure what to do about the case statement, though.
Any ideas would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的代码可以制成
switch-expression
喜欢这样:parts
数组的长度应在括号内使用()
开关
。并且每个特定的长度(1
和2
)应用作 case-contantant 。默认
案例涵盖了所有未指定的数组大小。The code you've provided can be made into a
switch-expression
like that:The length of the
parts
array should be used inside the parentheses()
of theswitch
. And each particular length (1
and2
) should be used as a case-constant.The
default
case covers all unspecified array sizes.