2016-09-24 1 views
1

宿題のプログラミングのために、私はテレビ番組Babylon 5のための名前ジェネレータを作るように頼まれました。プログラムは、あなたの名字、お気に入りの都市、そして良い友達のファーストネームを取ることになっています。カスタム名生成プログラムを作成しますか?

まず名前:好きな都市の最後の3文字+最初 名前の最初の3つの文字

姓:友人の名前の最後の3文字+ラスト 名の最初の4つの文字

次に、プログラムは、各名前の最初の子音の前にアポストロフィを挿入することになっています(名前の頭の子音を除いて、その場合は2番目の子音を使用します)。

私は、アポストロフィ挿入を除いてすべて機能しています。

public class MMStarWarsNG { 

    /** 
    * @param c 
    * @return 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Scanner sc = new Scanner(System.in); //creates the scanner that allows user input 

     System.out.print("What is your first name? "); 
     String firstName = sc.nextLine(); //creates new line after user hits enter 
     firstName = firstName.substring(0, 3); //locates the first three characters the the user typed. In this case the character are 0,1,2. 

     System.out.print("What is your last name? "); 
     String lastName = sc.nextLine(); 
     lastName = lastName.substring(0, 4); //takes the first 4 characters the the user typed, characters: 0,1,2,3 

     System.out.print("What is your favorite city? "); 
     String favCity = sc.nextLine(); 
     favCity = favCity.substring(favCity.length() - 3); //takes the final 3 characters that the user typed 
     favCity = favCity.substring(0, 1).toUpperCase() + favCity.substring(1); //takes the first character typed and capatilizes it 

     System.out.print("What is the first name of a good friend? "); 
     String friend = sc.nextLine(); 
     friend = friend.substring(friend.length() - 3); 
     friend = friend.substring(0, 1).toUpperCase() + friend.substring(1); 

     String SWName = favCity + firstName + " " + friend + lastName;//adds all of the substrings together. The space after firstName is the space between the first and last name 

     System.out.println(SWName); //prints the line above 
    } 

    public static boolean consonantFinder(char c) { 

     String vowels = "euioa"; 

     for (int i = 0; i < vowels.length(); i++) { 

      if (c == vowels.charAt(i)) { 
       return false; 
      } 
     } 
     return true; 
    } 

    public static int apostropheAdder(StringBuilder s) { 
     //adds apostrophe 
     int position; 
     for (position = 0; position < s.length(); position++) { //linear search for the length of the string 
      if (consonantFinder(s.charAt(position))) { //finds position 
       if (position != 0) { //checks if position is the first letter 
        consonantFinder((char) position); //does consonantFinder on the position 
        if (consonantFinder((char) position) == true) { //adds apostrophe 
         s.insert(position, "'"); 
         position++; //because of the randomness in my code, I've made it so that 
         //it can have more than one apostrophe 
        } 

       } 

      } 
     } 
     return position; 
    } 

} 

答えて

1

あなたの主なメソッドapostropheAdder()を呼び出していません。それは始めるには良い場所です。そのメソッドを追加...または私が提供したものを見てください。

import java.util.*; 
public class MMStarWarsNG { 

    /** 
    * @param c 
    * @return 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Scanner sc = new Scanner(System.in); //creates the scanner that allows user input 

     System.out.print("What is your first name? "); 
     String firstName = sc.nextLine(); //creates new line after user hits enter 
     firstName = firstName.substring(0, 3); //locates the first three characters the the user typed. In this case the character are 0,1,2. 

     System.out.print("What is your last name? "); 
     String lastName = sc.nextLine(); 
     lastName = lastName.substring(0, 4); //takes the first 4 characters the the user typed, characters: 0,1,2,3 

     System.out.print("What is your favorite city? "); 
     String favCity = sc.nextLine(); 
     favCity = favCity.substring(favCity.length() - 3); //takes the final 3 characters that the user typed 
     favCity = favCity.substring(0, 1).toUpperCase() + favCity.substring(1); //takes the first character typed and capatilizes it 

     System.out.print("What is the first name of a good friend? "); 
     String friend = sc.nextLine(); 
     friend = friend.substring(friend.length() - 3); 
     friend = friend.substring(0, 1).toUpperCase() + friend.substring(1); 

     String SWName = favCity + firstName + " " + friend + lastName;//adds all of the substrings together. The space after firstName is the space between the first and last name 

     StringBuilder SWNameWapos = apostropheAdder(SWName); 

     System.out.println(SWNameWapos); //prints the line above 
    } 

    public static boolean consonantFinder(char c) { 

     String vowels = "euioa "; 

     for (int i = 0; i < vowels.length(); i++) { 

      if (c == vowels.charAt(i)) { 
       return false; 
      } 
     } 
     return true; 
    } 

    public static StringBuilder apostropheAdder(String a) { 
     StringBuilder s = new StringBuilder(a); 
     //adds apostrophe 
     int position; 
     for (position = 1; position < s.length(); position++) { //linear search for the length of the string 
      if (consonantFinder(s.charAt(position))) { //finds position 
       s.insert(position, "'"); 
       position++; 
      } 
     } 
     return s; 
    } 
} 
2

まず、それはconsonantFinder戻っfalse子音が見つかったため、戻り値を交換することを混乱です。

はその後、apostropheAdderspaceOccuredフラグを使用して固定することができます

public static int apostropheAdder(StringBuilder s) { 
    int position; 
    boolean spaceOccured = true; 
    int lastSpacePosition = 0; 
    for (position = 0; position < s.length(); position++) { 
     if (s.charAt(position) == ' ') { 
      spaceOccured = true; 
      lastSpacePosition = position; 
     } 
     if (!spaceOccured) { 
      continue; 
     } 
     if (consonantFinder(s.charAt(position))) { 
      if (position == lastSpacePosition) { 
       continue; 
      } 
      spaceOccured = false; 
      s.insert(position, "'"); 
     } 
    } 
    return position; 
} 
関連する問題