可以说我有这个程序下面。现在我想将用户的名字和姓氏都传递给方法,而不用再次调用它并更改参数并使用户重新键入那里的名字和姓氏。我有一个方法,我想返回三个不同的值,我的主要方法
我知道我可以做两种不同的方法来找到名字和姓氏,但我想知道是否有办法做到这一点只是一种方法。
import java.util.Scanner;
public class Document3 {
public static void main(String[] args) {
String name;
name = getName("lastName"); //But how would I get my lastName WITHOUT recalling the method with "lastName" in the parameters?
System.out.println(name); //I want to also get the other name into in my main method somehow
}
public static String getName(String nameOption) {
Scanner x = new Scanner(System.in);
String firstName = "";
String lastName = "";
String nameChoice = "";
System.out.print("Please enter your first name: ");
firstName = x.nextLine();
System.out.print("\nPlease enter your last name: ");
lastName = x.nextLine();
if (nameOption.equals("firstName")) {
nameChoice = firstName;
}
if (nameOption.equals("lastName")) {
nameChoice = lastName;
}
return nameChoice; //how do I return both variables (firtName and lastName) and how would I access them
}
}
我结束了创建一个数组,现在我可以从main方法使用nameArray [0]或nameArray [1]来访问我需要的不同事物。 – JDN