如何使用java从函数返回数组

Kee*_*ana -3 java

在此输入图像描述

我写了一个程序来获取用户的地址详细信息并打印相同而不使用2 for循环,所以我使用函数add_show().但我收到错误,因为string []无法转换为字符串.返回添加;

public String add_show()
{ 

    Scanner h=new Scanner(System.in);
    System.out.println("Enter the number of address to deliver");
    int n=h.nextInt();
    String[] add=new String[n];
    System.out.println("Enter the"+n+" of address to deliver");
    for(int i=0;i<n;i++)
    {
        add[i]=h.next();
    }
    return add;
}

public void show()
{
     System.out.println("Book name is "+B_name());
     System.out.println("count "+department());
     System.out.println("address is "+add_show());
}
Run Code Online (Sandbox Code Playgroud)

请更新,如何从函数中获取数组的返回值.

小智 5

public String[] add_show()
{
    Scanner h=new Scanner(System.in);
    System.out.println("Enter the number of address to deliver");
    int n=h.nextInt(); 
    String[] add=new String[n];
    System.out.println("Enter the"+n+" of address to deliver");
    for(int i=0;i<n;i++)
    {
      add[i]=h.next();
    }
    return add;
}
Run Code Online (Sandbox Code Playgroud)

将方法的返回类型更改为字符串数组,它将适合您.

  • 请正确回答代码缩进 (4认同)