Java 制作自定义名称生成器?
为了做节目作业,我被要求为电视节目《巴比伦5》制作一个名字生成器。这个程序应该包括你的名字、姓氏、最喜欢的城市和一个好朋友的名字 名字:最喜欢城市的最后3个字母+第一个的前3个字母 名字 姓氏:朋友姓名的最后3个字母+最后4个字母 名字 然后,程序应该在每个名称的第一个辅音之前插入撇号(除了名称开头的辅音,在这种情况下,使用第二个辅音) 除了插入撇号外,我什么都做了Java 制作自定义名称生成器?,java,Java,为了做节目作业,我被要求为电视节目《巴比伦5》制作一个名字生成器。这个程序应该包括你的名字、姓氏、最喜欢的城市和一个好朋友的名字 名字:最喜欢城市的最后3个字母+第一个的前3个字母 名字 姓氏:朋友姓名的最后3个字母+最后4个字母 名字 然后,程序应该在每个名称的第一个辅音之前插入撇号(除了名称开头的辅音,在这种情况下,使用第二个辅音) 除了插入撇号外,我什么都做了 public class MMStarWarsNG { /** * @param c * @retu
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;
}
}
公共类MMStarWarsNG{
/**
*@param c
*@返回
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
Scanner sc=new Scanner(System.in);//创建允许用户输入的扫描仪
System.out.print(“您的名字是什么?”);
String firstName=sc.nextLine();//在用户点击enter键后创建新行
firstName=firstName.substring(0,3);//查找用户键入的前三个字符。在本例中,字符为0,1,2。
System.out.print(“您姓什么?”);
字符串lastName=sc.nextLine();
lastName=lastName.substring(0,4);//取用户键入的前4个字符,字符:0,1,2,3
System.out.print(“你最喜欢的城市是什么?”);
字符串favCity=sc.nextLine();
favCity=favCity.substring(favCity.length()-3);//获取用户键入的最后3个字符
favCity=favCity.substring(0,1).toUpperCase()+favCity.substring(1);//获取键入的第一个字符并对其进行capatilize
System.out.print(“好朋友的名字是什么?”);
字符串friend=sc.nextLine();
friend=friend.substring(friend.length()-3);
friend=friend.substring(0,1).toUpperCase()+friend.substring(1);
字符串SWName=favCity+firstName+“”+friend+lastName;//将所有子字符串相加。firstName后面的空格是名字和姓氏之间的空格
System.out.println(SWName);//打印上面的行
}
公共静态布尔辅音查找器(字符c){
字符串元音=“euioa”;
for(int i=0;i<元音.length();i++){
if(c==元音字符(i)){
返回false;
}
}
返回true;
}
公共静态整型撇号标题(StringBuilders){
//加撇号
内部位置;
对于(position=0;position
首先,当找到辅音时,consonantFinder
返回false
,这让人感到困惑,因此交换返回值
然后,可以使用spaceOccursed
标志来修复撇号标题
:
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;
}
public static int撇号标题(StringBuilders){
内部位置;
布尔空格Occursed=true;
int lastSpacePosition=0;
对于(位置=0;位置
main没有调用撇号header()方法。这是一个很好的开始。添加该方法。。。或者看看我提供了什么
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;
}
}
import java.util.*;
公共类MMStarWarsNG{
/**
*@param c
*@返回
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
Scanner sc=new Scanner(System.in);//创建允许用户输入的扫描仪
System.out.print(“您的名字是什么?”);
String firstName=sc.nextLine();//在用户点击enter键后创建新行
firstName=firstName.substring(0,3);//查找用户键入的前三个字符。在本例中,字符为0,1,2。
System.out.print(“您姓什么?”);
字符串lastName=sc.nextLine();
lastName=lastName.substring(0,4);//取用户键入的前4个字符,字符:0,1,2,3
System.out.print(“你最喜欢的城市是什么?”);
字符串favCity=sc.nextLine();
favCity=favCity.substring(favCity.length()-3);//获取用户键入的最后3个字符
favCity=favCity.substring(0,1).toUpperCase()+favCity.substring(1);//获取键入的第一个字符并对其进行capatilize
System.out.print(“好朋友的名字是什么?”);
字符串friend=sc.nextLine();
friend=friend.substring(friend.length()-3);
friend=friend.substring(0,1).toUpperCase()+friend.substring(1);
字符串SWName=favCity+firstName+“”+friend+lastName;//将所有子字符串相加。firstName后面的空格是名字和姓氏之间的空格
StringBuilder SWNameWapos=撇号标题(SWName);
系统输出