1

我正在尝试编写一个基本程序来输出用户输入的名称的首字母和变体。我已经完成了基本的编码,但我一直坚持如何编写 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));
}
4

7 回答 7

3

计算输入的单词数,

if (partOfName.length == 2){
// only First name and last name
}else if (partOfName.length == 3){
// with middle name
}
于 2013-09-20T16:18:14.653 回答
1
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(" ");
int len = partOfName.length;


if( len > 2 )
{
   //If user inputs 4 or more words only first three will be evaluated.

  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 if(len == 2)
{
  char firstInitial = Character.toUpperCase(partOfName[0].charAt(0));
  char lastInitial = Character.toUpperCase(partOfName[1].charAt(0));
}
else if ( len < 2)
{
   System.out.println("Bad input");
}

或者你可以尝试类似的东西

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(" ");
int len = partOfName.length;
int i=0;
string initials = "";

if(len >= 2) 
{

    for (i=0;i<len;i++)
    {
       initials += Character.toUpperCase(partOfName[i].charAt(0));
    }

    System.out.println ("Your Initials Are " + initials );

}

另一种方法是使用 StringTokenizer 类。不要忘记导入 StringTokenizer

import java.util.StringTokenizer; 


Scanner scan=new Scanner (System.in);
System.out.println("What are your first, middle, and last names? ");
String name = scan.nextLine();

StringTokenizer st = new StringTokenizer(name);

String initials = "Your Initials Are: ";


while (st.hasMoreElements()) 
{
    initials += Character.toUpperCase(st.nextElement().charAt(0)));
}

System.out.println( initials );
于 2013-09-20T16:20:23.980 回答
0
if (partOfName.length == 3) {

} else if (partOfName.length == 2) {

} else {
   //deal with exceptions
}
于 2013-09-20T16:19:42.443 回答
0

使用数组的大小partOfName

if (partOfName.length>=3)
{
 //work with first name, middle name and last name
} else {
 //work with first name and last name
}
于 2013-09-20T16:19:51.857 回答
0

我可能会检查length数组的属性。

if (partOfName.length > 2) // At least 3 names entered
{

}
else if (partOfName.length == 2) // Two names entered
{

}

您可能还想检查是否只输入了一个名称。也许用户是麦当娜。

于 2013-09-20T16:20:01.460 回答
0
if (partOfName.length == 3)

您可能希望增强您的 else 语句以确保 split 实际上至少有 2 个部分

于 2013-09-20T16:20:45.187 回答
0

首先,在设置之前,

    char firstInitial = Character.toUpperCase(partOfName[0].charAt(0));
    char middleInitial= Character.toUpperCase(partOfName[1].charAt(0));
    char lastInitial = Character.toUpperCase(partOfName[2].charAt(0));

确保拆分后的数组包含三个元素。否则你的程序会失败。那么你可以假设如果数组有两个元素,用户输入了他的名字和姓氏,那么你可以设置firstInitial和lastInitial。如果数组长度为3,则可以设置firstInitial、middleInitial和lastInitial

    int len=partOfName.length;

    // If you need to get more than 3, you should need a more generic solution.
    if (len<2 || len>3 )
    System.out.println("Bad input."); // or throw an Exception

    if (len==3){
    //set initials of name, middle and lastname
    }
    else if (len==2) {
    //set initials of name, and lastname
    }
于 2013-09-20T16:22:40.193 回答