gpt4 book ai didi

java - 制作自定义名称生成器?

转载 作者:行者123 更新时间:2023-11-29 04:39:49 25 4
gpt4 key购买 nike

为了编程作业,我被要求为电视节目 Babylon 5 制作一个名字生成器。该程序应该包含您的名字、姓氏、最喜欢的城市和好 friend 的名字。

First name: Last 3 letters of favorite city + first 3 letters of first name

Last name: Last 3 letter of friends name + first 4 letters of last name

然后程序应该在每个名字的第一个辅音之前插入一个撇号(名字开头的辅音除外,在这种情况下,使用第二个)。

除了插入撇号外,我的一切都正常工作。

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;
}

}

最佳答案

首先,令人困惑的是 consonantFinder 在找到辅音时返回 false,因此交换返回值。

然后,可以使用 spaceOccured 标志修复 apostropheAdder:

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;
}

关于java - 制作自定义名称生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com