如何在Java中的Switch-Case语句中返回数组?
问题的本质:您需要实施一种公共静态方法,该方法返回两个要素的数组 - 以英语为单位的日子名称。该方法接受参数为输入 - 返回格式作为字符串。总共有两个可能的值:
“ long”(默认) - 数组包含字符串“星期六”和“星期日”
“ short” - 数组包含字符串“ sat”和“ sun”,
需要使用该问题解决问题 。开关案例语句。我的解决方案如下:
class App {
public static void main(String[] args){
App.getWeekends("long");
}
public static void getWeekends(){
String[] day =new String[4];
day[0]="saturday";
day[1]="sunday";
day[2]="sat";
day[3]="sun";
switch(day){
case "long":
return {"saturday","sunday"};
case "short":
return {"sat", "sun"};
default:
return null;
}
}
}
The essence of the problem: you need to implement a public static method that returns an array of two elements - the names of the days off in English. The method accepts a parameter as input - the return format as a string. There are two possible values in total:
"long" (default) – array contains the strings "saturday" and "sunday"
"short" - the array contains the strings "sat" and "sun"
The problem needs to be solved using the switch-case statement. My solution is as follows:
class App {
public static void main(String[] args){
App.getWeekends("long");
}
public static void getWeekends(){
String[] day =new String[4];
day[0]="saturday";
day[1]="sunday";
day[2]="sat";
day[3]="sun";
switch(day){
case "long":
return {"saturday","sunday"};
case "short":
return {"sat", "sun"};
default:
return null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码包含多个无关的错误。
阵列初始化器不允许
不是合法的Java,除了在非常具体的地方。
具体而言,在初始化局部变量的初始化表达或字段声明中,其类型为数组是合法的,并且在其他任何地方都不合法。换句话说,好的:
这是您可以合法写的唯一地方。如果您想要一个解决带有2个“值”的字符串阵列的表达式,第一个是“星期六”,第二个是“星期日”,您写道:
这在可变声明中也是合法的:
但是您可以'缩短'并省略
new String []
部分。主应用程序中的参数混乱
您将
“长”
作为getweekends
方法的参数传递,但是您的getweekends()
方法>方法clangaration clangaration condaration接受零参数。您需要解决此问题:返回类型混乱
您在getweekends方法中编写
返回
,即您想返回某些东西。具体来说,您要返回字符串数组。但是,您已声明它返回什么都没有返回(void
表示:我什么也没返回)。您必须修复此操作:毫无意义的创建
天
看起来整个
day
是您尝试随机的东西,挥舞着,不知道该怎么做。让我们摆脱它吗?从String [] Day
到天[3] =
;您可以简单地打开type
,这是您从所谓的任何内容中获得的参数。您的应用程序无能为力,
返回值只是...返回。致呼叫者。呼叫者需要对此做一些事情。也许在这里,我们将打印它。让我们使用
system.out.println(arrays.tostring(the-array-here))
将其添加到您的主要方法中。null
混乱<代码> null 不应使用,除非您真的知道自己在做什么并且对事物有明确的定义。当然,它不应用作错误条件的“站立”。如果发生代码路径,您不知道该如何处理,只需扔东西即可。
默认情况
情况应抛出一些东西,而不是返回null
。因此将所有这些放在一起
:
Your code contains multiple unrelated errors.
Array initializers not allowed
That's not legal java, except in very specific places.
Specifically, in the initializing expression of either a local variable, or field declaration whose type is an array, that is legal, and it isn't legal anywhere else. In other words, okay:
And that's about the only place you can legally write that. If you want an expression that resolves to a string array with 2 'values', the first one being "saturday" and the second being "sunday", you write:
This is also legit in variable declarations:
But you can 'shorten' it and omit the
new String[]
part there.Parameter confusion
In your main app you pass
"long"
as parameter to yourgetWeekends
method, but yourgetWeekends()
method declaration accepts zero parameters. You need to fix that:Return type confusion
You write
return
in your getWeekends method, i.e. you want to return something. Specifically, you want to return a string array. However, you've declared it to return nothing (void
means: I return nothing). You must fix this:Pointless creation of
day
It looks like that whole
day
thing is you trying random stuff, flailing about and having no idea what to do. Let's just get rid of it? Remove the stuff fromString[] day
today[3]=
; you can simply switch ontype
, which is the parameter you got from whatever called you.Your app does nothing
A return value is just... Returned. To the caller. The caller needs to do something with it. Perhaps, here, as an example, we shall print it. Let's add that to your main method using
System.out.println(Arrays.toString(the-array-here))
.null
confusionnull
should not be used unless you really know what you are doing and you have a clear definition of things. It most certainly should not be used as 'stand in' for an error condition. Just throw something if a code path occurs that you don't know how to deal with. Thedefault
case should throw something instead of returningnull
.Putting it all together
Thus:
尝试一下,我还添加了一个扫描仪,以便如果您想要短版本或
具有void方法的长版本,但您尝试返回数组... void方法无法返回值,
您仍然可以使用您可以使用
返回null;
如果输入不是其中一种情况Try this , i also added a scanner so you can type in the console if you want the short version or the long one
You had a void method but you tried to return arrays... void methods can't return values
You can still use the
return null;
if the input isn't one of the cases