为了编写家庭作业,我被要求为电视节目Babylon 5制作一个名称生成器。该节目应该包含你的名字,姓氏,最喜欢的城市和一个好朋友的名字。
名字:最喜欢的城市的最后3个字母+第一个的前3个字母 名称
姓氏:朋友姓名的最后3个字母+最后4个字母 名称
然后程序应该在每个名字的第一个辅音之前插入一个撇号(名称开头的辅音除外,在这种情况下,使用第二个辅音)。
除了撇号插入外,我已经完成了所有工作。
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;
}
}
答案 0 :(得分:2)
首先,当辅音找到时,consonantFinder
返回false
会让人感到困惑,所以交换返回值。
然后,apostropheAdder
可以使用spaceOccured
标志修复:
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;
}
答案 1 :(得分:1)
您的main不会调用方法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;
}
}