我正在尝试编写一个基本程序来输出用户输入的名称的首字母和变体。我已经完成了基本的编码,但我一直坚持如何编写 if-else 语句,该语句将允许用户只输入名字和姓氏,而无需中间。这是代码的开头:
Scanner scan=new Scanner (System.in);
System.out.println("What are your first, middle, and last names? ");
String name = scan.nextLine();
String[] partOfName = name.split(" ");
**if ()**
{
char firstInitial = Character.toUpperCase(partOfName[0].charAt(0));
char middleInitial= Character.toUpperCase(partOfName[1].charAt(0));
char lastInitial = Character.toUpperCase(partOfName[2].charAt(0));
String firstName = partOfName[0].substring(0,1).toUpperCase() + partOfName[0].substring(1);
String lastName = partOfName[2].substring(0,1).toUpperCase() + partOfName[2].substring(1);
String middleName = partOfName[1].substring(0,1).toUpperCase() + partOfName[1].substring(1);
String c_lastName = lastName.toUpperCase();
System.out.println ("Your Initials Are " + firstInitial + middleInitial + lastInitial);
System.out.println("Variation one: " + c_lastName + ", " + firstName + " " + middleInitial + ".");
System.out.println("Variation two: " + lastName+ ", " + firstName + " " + middleName);
}
else
{
char firstInitial = Character.toUpperCase(partOfName[0].charAt(0));
char lastInitial = Character.toUpperCase(partOfName[2].charAt(0));
}