if else语句的大小是split

时间:2013-09-20 16:14:30

标签: java

我正在尝试编写一个基本程序,输出用户输入的名称的缩写和变体。我有基本的编码,但我仍然坚持如何编写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));
}

7 个答案:

答案 0 :(得分:3)

计算输入的字数,

if (partOfName.length == 2){
// only First name and last name
}else if (partOfName.length == 3){
// with middle name
}

答案 1 :(得分: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 );

答案 2 :(得分:0)

if (partOfName.length == 3) {

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

} else {
   //deal with exceptions
}

答案 3 :(得分:0)

使用数组partOfName的大小。

if (partOfName.length>=3)
{
 //work with first name, middle name and last name
} else {
 //work with first name and last name
}

答案 4 :(得分:0)

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

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

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

}

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

答案 5 :(得分:0)

if (partOfName.length == 3)

你可能想要增强你的else语句,以确保分裂实际上至少有两部分

答案 6 :(得分: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
    }