如何操作返回的值数组

时间:2015-03-11 06:29:07

标签: java arrays

所以,我有点麻烦。我终于决定了我想做什么,但是一旦我完成,我就像......嗯......这就是我想要对这些信息做些什么,但是如何从阵列中获取它?

因此,我返回我的String数组'results',其中包含两个fullName和pinCode值。我打电话给方法使用'greeting(scan)'来创建这个信息,但现在我不知道如何单独取出fullName和pinCode,或者将它们放入main方法的本地数组中。有什么建议吗?

public static void main(String[] args) throws InterruptedException {
    Scanner scan = new Scanner(System.in);
    greeting( scan );
}
public static String[] greeting( Scanner scan ) throws InterruptedException {
    System.out.println( "Hello, User..." );

    //Timeout for 0.5 seconds
    Thread.sleep(0500);

    //Ask for the users first name
    System.out.println( "Please enter your first name: " );
    String firstName = scan.nextLine();

    //Ask for the users last name
    System.out.println( "Please enter your last name: " );
    String lastName = scan.nextLine();

    //Generates a random number between 1000-10000 for the PIN code
    String pinCode = Integer.toString((int) ((Math.random()*9000)+1000));

    //Erase everything, but letters by calling my stringValid method
    String fullName = stringValid( firstName, lastName );

    String[] results = {fullName, pinCode};
    return results;
}

3 个答案:

答案 0 :(得分:0)

变化:

public static void main(String[] args) throws InterruptedException {
    Scanner scan = new Scanner(System.in);
    greeting( scan );
}

public static void main(String[] args) throws InterruptedException {
    Scanner scan = new Scanner(System.in);
    String[] result = greeting( scan );
    String fullname = result[0];
    String pinCode = result[1];
}

您必须将返回值分配给对象,然后您可以从中获取值。

答案 1 :(得分:0)

String [] localResults = greeting(scan);
String fullname = results[0];
String pinCode = results[1];

答案 2 :(得分:0)

在我看来,如果:

String[] result = greeting(scan);

然后:

String fullName = result[0];
String pinCode = result[1];