调用两个方法并将调用第一个方法的结果传递给第二个方法的 Main 方法



>我的任务是创建一个调用两个方法的主方法。第一种方法返回字符串数组,而第二种方法获取字符串数组并在单独的行上打印出元素。然后,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); 
} 
}

首先,纠正你的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中,不应该调用它,因为数据应该作为参数传递。

你的代码可以工作,但在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

如果您希望数据"流"到主管道,您应该这样做:

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); 
} 
}

执行上述操作的正确方法如下所示。您必须获取方法 1 的返回值,并将其作为参数插入方法 2。

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); 
} 
}

你快到了:你的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); 
} 

最新更新