[英]How to return multiple arrays from a method
I've changed the question, the problem I'm facing is with return, I can't seem to return name and phone from addcontact method to main properly.我已经改变了问题,我面临的问题是返回,我似乎无法将姓名和电话从 addcontact 方法正确返回到 main。
I need to return the name and phone to main method and update if necessary.我需要将姓名和电话返回给主要方法并在必要时进行更新。 The object[] seemed like a good option but I can't seem to make it work properly.
object[] 似乎是一个不错的选择,但我似乎无法使其正常工作。
package asstask3;
import java.util.*;
public class AssignmentTask3
{
static Scanner input = new Scanner( System.in );
public Object[] addcontact(String[] name, String[] phone, int count)
{
if ( count == 30 )
{
System.out.println( "No more contacts can be added" );
return null;
}
else
{
System.out.println( "Enter the name:" );
name[ count ] = input.next();
System.out.println( "Enter the phone:" );
phone[ count ] = input.next();
count++;
}
return new Object[]{name, phone};
}
public boolean updatecontact(String[] name, String[] phone, String upname, int count)
{
for ( int i = 0; i < count; i++ )
{
if ( name[ i ].equals( upname ) )
{
System.out.println( "Enter to update phone:" );
phone[ i ] = input.next();
return true;
}
}
return false;
}
public void displaymenu()
{
System.out.println( "1. Add New Contact" );
System.out.println( "2. Update Existing Contact" );
System.out.println( "0. Exit" );
}
public static void main(String []args)throws Exception
{
AssignmentTask3 Astask = new AssignmentTask3();
String name[] = new String[ 30 ];
String phone[] = new String[ 30 ];
int count = 1;
do
{
Astask.displaymenu();
int x = input.nextInt();
if ( x == 1 )
{
Object[] add = Astask.addcontact( name, phone, count );
String ss = add[1].toString();
System.out.println( "Name" +name + "Phone"+ phone + "Con" + ss );
}
if ( x == 2 )
{
System.out.println( "Enter name to update:" );
String upname = input.next();
Astask.updatecontact( name, phone, upname, count );
}
if ( x == 0 )
{
System.exit( 0 );
}
} while ( true );
}
}
Name[Ljava.lang.String;@5010be6Phone[Ljava.lang.String;@685f4c2eCon[Ljava.lang.String;@5010be6姓名[Ljava.lang.String;@5010be6电话[Ljava.lang.String;@685f4c2eCon[Ljava.lang.String;@5010be6
this is what I get from print statement after addcontact method is executed.这是我在执行 addcontact 方法后从 print 语句中得到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.