0

以下を印刷するプログラミングを行っています

ユーザーは次のように名前を入力します --> 最初の中間 最後に印刷:
FML
バリエーション 1: LAST、最初の M。
バリエーション 2: 最後、最初のミドル

ここで、名だけが入力された場合に「エラー、入力が正しくありません」と表示されるように、if ステートメントが必要です。

私はこれを恐ろしく、非常に型破りにコーディングしましたが、これは私がこれまでにプログラミングした最初のものなので、私たちは皆どこかから始めていると思います.

import java.util.Scanner;

public class name {
    public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    String fullName = input.nextLine();
    String firstName;
    String middleName;
    String lastName;

    //Declares length of entire name
    int nameLength = fullName.length();

    //Declares int where first space is
    int a = fullName.indexOf(" ");

    //Declares int where second space is
    int b = fullName.lastIndexOf(" ");

    //If they equal each other, then there is only one space
    if ( a == b )
    {
        firstName = fullName.substring(0,a);

        lastName = fullName.substring(a+1,nameLength);

        String firstNameInitial = firstName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        firstName = fullName.substring(0,a);

        lastName = fullName.substring(b+1,nameLength);

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a));
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a));

    }

    //If a < b then it will notice a middle name exists due to multiple spaces
    else if ( a < b )
    {
        firstName = fullName.substring(0,a);

        middleName = fullName.substring(a+1,b);

        lastName = fullName.substring(b+1,nameLength);

        String firstNameInitial = firstName.substring(0,1);
        String middleNameInitial = middleName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + middleNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        //MNIC = Middle Name Initial Capitalized
        String MNIC = middleNameInitial.toUpperCase();

        //MNIMFC = Middle Name Initial Minus First Character
        String MNIMFC = middleName.substring(1, middleName.length());

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) + " " + middleNameInitial.toUpperCase() + "." );
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) +  " " + MNIC  + MNIMFC);


    }







}

}

4

5 に答える 5

1

この関数を使用してString.split()、文字列を区切り記号に沿って部分に分割できます。

あなたの場合、それはスペースになります ( " ")

試す:

Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
String firstName;
String middleName;
String lastName;

String[] parts = fullName.split(" ");

if(parts.length() == 3){
    // 3 words were entered, so there is a middle name
}
// ...
于 2013-09-19T07:52:47.213 に答える
0

このチェックを追加するだけです

if(fullName.indexOf(" ")==-1 || (fullName.indexOf(" ") == fullName.lastIndexOf(" "))){
    // The first check is to check if only firstname was given and the second check is to check if only first and middle names were given.
    // If first + middle is a valid scenario, you can remove the second half of the if condition
    System.out.println("error, incorrect input");
    System.exit(0);
}

コード内の以下のステートメントの前。

int nameLength = fullName.length();
于 2013-09-19T07:52:54.243 に答える
0

を簡単に確認できますa == -1indexOf見つからない場合は -1 を返します ( docsに従って)。

if (a == -1)
   System.out.println("Error, invalid input!");
else if (a == b)
...
于 2013-09-19T07:53:06.973 に答える