简体   繁体   English

在方法中返回数组

[英]Returning an Array in a Method

I have a method that will return a String[], depending on previous user input that will determine if the parameter is "INTERNATIONAL" or "DOMESTIC". 我有一个方法将返回String [],具体取决于先前的用户输入,该输入将确定参数是“ INTERNATIONAL”还是“ DOMESTIC”。 Regardless, both inputs should lead to the creation of two different String[]. 无论如何,两个输入都应导致创建两个不同的String []。 When I try to compile though, I get an "missing return statement" error message. 但是,当我尝试编译时,出现“缺少返回语句”错误消息。 How can I fix this? 我怎样才能解决这个问题?

Here is my method: 这是我的方法:

   public String[] typeflight(String type)
     { 
        String type2= type.toUpperCase();
        if (type2.equals("INTERNATIONAL"))
        {
            String[] flights = {"B738 to Melbourne, Australia ", "A380 to Beijing, China ", "F348 to London, England ", "M225 to Ontario, Canada",
                    "E987 to Tokyo, Japan ", "T451 to Copenhagen, Denmark ", "S501 to Seoul, South Korea ", "N778 to Venice, Italy ",
                    "B621 to Mexico City, Mexico ", "L454 to Rabat, Morocco ", "C998 to San Jose, Costa Rica", "H859 to Amsterdam, Netherlands "};
            return flights;

        }
        else
        if(type2.equals("DOMESTIC"))
        {   
            String[] flights = {"459 to Seattle, Washington ", "662 to Los Angeles, California ", "712 to New Orleans, Louisiana ", "285 to Chicago, Illinois ",
                    "896 to Honolulu, Hawaii ", "476 to Boston, Massachusetts ", "823 to Newark, New Jersey ", "902 to Miami, Florida ",
                    "353 to Fort Wayne, Indiana ", "112 to Des Moines, Iowa ", "", "294 to Las Vegas, Nevada"};
            return flights;

        }
    }

Thanks in advance! 提前致谢!

What happens if neither of those if-statements is true? 如果这些if陈述都不成立,会发生什么? Then your method has no return value. 则您的方法没有返回值。

eg type2 = "ALIEN" 例如, type2 = "ALIEN"

Perhaps there is logic in your program that prevents this from happening. 程序中可能存在阻止这种情况发生的逻辑。 But the Java compiler does not know this. 但是Java编译器不知道这一点。

A quick and dirty fix for this is to just put a return null; 一个快速而肮脏的解决方法是只将return null; at the end of the method. 在方法的末尾。 A "better" way is to use exceptions for illegal arguments. 一种“更好”的方法是对非法参数使用例外。

If both the if conditions fails the function is not returning anything. 如果两个if条件都失败,则该函数不返回任何内容。 So return null if both the conditions don't match. 因此,如果两个条件都不匹配,则return null

public String[] typeflight(String type)
 { 
    String type2= type.toUpperCase();
    if (type2.equals("INTERNATIONAL"))
    {
        // Code
        return flights;

    }
    else
    if(type2.equals("DOMESTIC"))
    {   
        // Code 
        return flights;

    }

    return null;
}

Your issue is that you have an if and and else if , but no else . 您的问题是您有一个ifelse if ,但没有else Java is complaining that the case where type2 does not equal either of the two given values is not addressed. Java抱怨type2与两个给定值都不相等的情况没有得到解决。

好的编译器需要外部的“ return语句”来确定返回结果。

You should have the return statement for all the cases . 对于所有情况,您都应该具有return语句。 So declare the String flights out side the if else and initailize dynmically, and finally return as 因此,如果不是,则声明String跳出,然后动态地进行初始化,最后返回为

 public String[] typeflight(String type)
    {
    String[] flights;
         if(condition1){
           flights  = ....
           }
         else{
            flights = .....   
      }
          return flights;
    }

Also you should avoid the declaration of data members in the same name , 另外,您应该避免声明相同名称的数据成员,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM