【问题标题】:Main method that calls two method and passes result of calling first method over to second method调用两个方法并将调用第一个方法的结果传递给第二个方法的主方法
【发布时间】:2019-02-05 05:49:06
【问题描述】:

我的任务是创建一个调用两个方法的主方法。第一种方法返回字符串数组,而第二种方法获取字符串数组并在单独的行上打印出元素。然后 main 方法将调用第一个方法的结果传递给第二个方法,然后停止。我是否正确理解了这个问题?当我编译并执行时,我得到了

sunshine
road
73
11

public class Hihihi
{

public static void main(String args[]) 
{
  method1();
  method2();//Will print the strings in the array that
            //was returned from the method1()
   System.exit(0); 
}                                 



public static String[] method1() 
{
     String[] xs = new String[] {"sunshine","road","73","11"};
     String[] test = new String[4];
     test[0]=xs[0];
     test[1]=xs[1];
     test[2]=xs[2];
     test[3]=xs[3];
     return test;
 }

   public static void  method2()
  { 
    String[] test = method1();
    for(String str : test)
        System.out.println(str); 
  } 
}

【问题讨论】:

    标签: java function methods


    【解决方案1】:

    如果您希望数据通过 main “流动”,您应该这样做:

    public static void main(String args[]){
          String[] arr = method1();
          method2(arr);
          System.exit(0); 
    }   
    
    public static String[] method1(){
         String[] xs = new String[] {"sunshine","road","73","11"};
         String[] test = new String[4];
         test[0]=xs[0];
         test[1]=xs[1];
         test[2]=xs[2];
         test[3]=xs[3];
         return test;
    }
    
    public static void  method2(String[] arr){ 
        for(String str : arr){
            System.out.println(str); 
        } 
    }
    

    【讨论】:

      【解决方案2】:

      首先,纠正你的method2

      它必须能够接受 String 元素的数组作为参数:

      public static void  method2(String[] test)
        { 
          // this line is not needed -> String[] test = method1();
          for(String str : test)
              System.out.println(str); 
        } 
      

      这样,您实际上将数据传递给方法,正如您所描述的那样。一个好处是:它也可以用于其他 String 数组。

      您的method1 有很多冗余代码。把它过滤掉

      public static String[] method1() 
      {
           return new String[] {"sunshine","road","73","11"};
      }
      

      现在,您的main 方法,只是为了链接它们。改变

      public static void main(String args[]) 
      {
        method1();
        method2(); // this will now cause a compilation error, because it expects a parameter
         System.exit(0); 
      } 
      

      到:

      public static void main(String args[]) 
      {      
        method2(method1());
         System.exit(0); 
      } 
      

      您的代码最初构建的方式,method1 被调用了两次,第一次在 main 方法中,这是完全多余的,因为没有使用结果,第二次在 method2 中,它不应调用,因为数据应作为参数传递。

      【讨论】:

        【解决方案3】:

        你快到了:你的method1() 的返回应该在第一次调用中使用(你实际上忽略了它),你必须使用那个返回值(我也在代码中做了一些注释) :

        public static void main(String args[]) {
          String[] result = method1();  //TAKE THE VALUE HERE
          method2(result);//pass the result here as parameter
          System.exit(0); //there's no need of this, the program will exit when method2 is finished
        }  
        

        所以编辑你的method2 以获得method1 的结果作为参数

        public static void  method2(String[] arrayToPrint){ 
            for(String str : arrayToPrint)
                System.out.println(str); 
        } 
        

        【讨论】:

          【解决方案4】:

          上面的正确方法如下。你必须把方法一的返回值作为参数插入到方法二中。

           public class Hihihi
              {
          
              public static void main(String args[]) 
              {
                String[] test=method1();
                method2(test);
              }                                 
          
          
          
              public static String[] method1() 
              {
                   String[] xs = new String[] {"sunshine","road","73","11"};
                   String[] test = new String[4];
                   test[0]=xs[0];
                   test[1]=xs[1];
                   test[2]=xs[2];
                   test[3]=xs[3];
                   return test;
               }
          
                 public static void  method2(String[] newtest)
                { 
          
                  for(String str : newtest)
                      System.out.println(str); 
                } 
              }
          

          【讨论】:

            【解决方案5】:

            您的代码有效,但在main 中,method1 的返回值被忽略。并且在method2 中调用method1 是不必要的。

            可以让method2接受参数String[] strings

            public static void method2(String[] strings) {
            
                for (String str : strings)
                    System.out.println(str);
            }
            

            并将method1 的结果传递给method2

            String[] result = method1();
            method2(result);//Will print the strings in the array that
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-03-23
              • 2018-02-12
              • 1970-01-01
              • 1970-01-01
              • 2013-10-18
              • 1970-01-01
              • 2010-10-17
              • 1970-01-01
              相关资源
              最近更新 更多