如何在Java中的Switch-Case语句中返回数组?

发布于 2025-02-05 18:41:34 字数 678 浏览 2 评论 0原文

问题的本质:您需要实施一种公共静态方法,该方法返回两个要素的数组 - 以英语为单位的日子名称。该方法接受参数为输入 - 返回格式作为字符串。总共有两个可能的值:

“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2025-02-12 18:41:34

您的代码包含多个无关的错误。

阵列初始化器不允许

{“星期六”,“星期日”}

不是合法的Java,除了在非常具体的地方。

具体而言,在初始化局部变量的初始化表达或字段声明中,其类型为数组是合法的,并且在其他任何地方都不合法。换句话说,好的:

String[] x = {"saturday", "sunday"};

这是您可以合法写的唯一地方。如果您想要一个解决带有2个“值”的字符串阵列的表达式,第一个是“星期六”,第二个是“星期日”,您写道:

new String[] {"saturday", "sunday"};

这在可变声明中也是合法的:

String[] x = new String[] {"saturday", "sunday"};

但是您可以'缩短'并省略new String []部分。

主应用程序中的参数混乱

您将“长”作为getweekends方法的参数传递,但是您的getweekends()方法>方法clangaration clangaration condaration接受零参数。您需要解决此问题:

public static void getWeekends(String type) {

返回类型混乱

您在getweekends方法中编写返回,即您想返回某些东西。具体来说,您要返回字符串数组。但是,您已声明它返回什么都没有返回(void表示:我什么也没返回)。您必须修复此操作:

public static String[] getWeekends(String type) {

毫​​无意义的创建

看起来整个day是您尝试随机的东西,挥舞着,不知道该怎么做。让我们摆脱它吗?从String [] Day天[3] =;您可以简单地打开type,这是您从所谓的任何内容中获得的参数。

您的应用程序无能为力,

返回值只是...返回。致呼叫者。呼叫者需要对此做一些事情。也许在这里,我们将打印它。让我们使用system.out.println(arrays.tostring(the-array-here))将其添加到您的主要方法中。

null混乱

<代码> null 不应使用,除非您真的知道自己在做什么并且对事物有明确的定义。当然,它不应用作错误条件的“站立”。如果发生代码路径,您不知道该如何处理,只需扔东西即可。 默认情况情况应抛出一些东西,而不是返回null

因此将所有这些放在一起

class App {
   
  public static void main(String[] args){
      String[] dayNames = App.getWeekends("long");
      System.out.println(Arrays.toString(dayNames));
  }

    public static String[] getWeekends(String type) {
     switch(type) {
        case "long":
          return new String[] {"saturday","sunday"};

        case "short":
          return new String[] {"sat", "sun"};

      default:
          throw new IllegalArgumentException("Pass either 'short' or 'long'");
     }
    }
}

Your code contains multiple unrelated errors.

Array initializers not allowed

{"saturday", "sunday"}

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:

String[] x = {"saturday", "sunday"};

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:

new String[] {"saturday", "sunday"};

This is also legit in variable declarations:

String[] x = new String[] {"saturday", "sunday"};

But you can 'shorten' it and omit the new String[] part there.

Parameter confusion

In your main app you pass "long" as parameter to your getWeekends method, but your getWeekends() method declaration accepts zero parameters. You need to fix that:

public static void getWeekends(String type) {

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:

public static String[] getWeekends(String type) {

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 from String[] day to day[3]=; you can simply switch on type, 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 confusion

null 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. The default case should throw something instead of returning null.

Putting it all together

Thus:

class App {
   
  public static void main(String[] args){
      String[] dayNames = App.getWeekends("long");
      System.out.println(Arrays.toString(dayNames));
  }

    public static String[] getWeekends(String type) {
     switch(type) {
        case "long":
          return new String[] {"saturday","sunday"};

        case "short":
          return new String[] {"sat", "sun"};

      default:
          throw new IllegalArgumentException("Pass either 'short' or 'long'");
     }
    }
}
作妖 2025-02-12 18:41:34

尝试一下,我还添加了一个扫描仪,以便如果您想要短版本或

具有void方法的长版本,但您尝试返回数组... void方法无法返回值,

您仍然可以使用您可以使用返回null;如果输入不是其中一种情况

import java.util.Arrays;
import java.util.Scanner;

public class App {
    

        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("Write \"long\" for the long format, or \"short\" for the short format");
            String length= sc.next();
            System.out.println(Arrays.toString(getWeekends(length)));
        }

        public static String[] getWeekends(String input){


            switch(input){
                case "long":
                    return new String[] {"saturday","sunday"};

                case "short":
                    return new String[] {"sat", "sun"};

            }
            return new String[0];
        }


}

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

import java.util.Arrays;
import java.util.Scanner;

public class App {
    

        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("Write \"long\" for the long format, or \"short\" for the short format");
            String length= sc.next();
            System.out.println(Arrays.toString(getWeekends(length)));
        }

        public static String[] getWeekends(String input){


            switch(input){
                case "long":
                    return new String[] {"saturday","sunday"};

                case "short":
                    return new String[] {"sat", "sun"};

            }
            return new String[0];
        }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文