java 输出姓名首字母的算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7621034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Algorithm to output the initials of a name
提问by Aleksandrs Prokopjevs
I have just started the java programming and at the moment I am doing the basic things. I came across a problem that I can't solve and didn't found any answers around so I thought you might give me a hand. I want to write a program to prompt the user to enter their full name (first name, second name and surname) and output their initials.
我刚刚开始 Java 编程,目前我正在做基本的事情。我遇到了一个我无法解决的问题,也没有找到任何答案,所以我想你可以帮我一把。我想编写一个程序来提示用户输入他们的全名(名字、第二名和姓氏)并输出他们的姓名首字母。
Assuming that the user always types three names and does not include any unnecessary spaces. So the input data will always look like this : Name Middlename Surname
假设用户总是键入三个名称并且不包含任何不必要的空格。因此输入数据将始终如下所示: Name Middlename Surname
Some of my code that I have done and stuck in there as I get number of the letter that is in the code instead of letter itself.
我已经完成的一些代码并停留在那里,因为我得到了代码中的字母编号而不是字母本身。
import java.util.*;
public class Initials
{
public static void main (String[] args)
{
//create Scanner to read in data
Scanner myKeyboard = new Scanner(System.in);
//prompt user for input – use print to leave cursor on line
System.out.print("Please enter Your full Name , Middle name And Surname: ");
String name = myKeyboard.nextLine();
String initials1 = name.substring(0, 1);
int initials2 = name.
//output Initials
System.out.println ("Initials Are " + initials1 + initials2 + initials3);
}
}
回答by hvgotcodes
Users will enter a string like
用户将输入一个字符串
"first middle last"
“先中后”
so therefore you need to get eachword from the string.
因此,您需要从字符串中获取每个单词。
Loot at split.
掠夺分裂。
After you get each word of the user-entered data, you need to use a loopto get the first letter of each part of the name.
获取用户输入数据的每个单词后,需要使用循环获取名称各部分的首字母。
回答by DavidAndroidDev
First, the nextLine Function will return the full name. First, you need to .split()
the string name
on a space, perhaps. This requires a correctly formatted string from the user, but I wouldn't worry about that yet.
首先,nextLine 函数将返回全名。首先,您可能需要.split()
将字符串name
放在一个空格上。这需要来自用户的正确格式的字符串,但我不会担心。
Once you split the string, it returns an array of strings. If the user put them in correectly, you can do a for loop on the array.
拆分字符串后,它会返回一个字符串数组。如果用户正确放置它们,则可以在数组上执行 for 循环。
StringBuilder builder = new StringBuilder(3);
for(int i = 0; i < splitStringArray.length; i++)
{
builder.append(splitStringArray[i].substring(0,1));
}
System.out.println("Initials Are " + builder.toString());
回答by Snowy Coder Girl
Use the String split()
method. This allows you to split a String using a certain regex (for example, spliting a String by the space character). The returned value is an array holding each of the split values. See the documentationfor the method.
使用字符串split()
方法。这允许您使用某个正则表达式拆分字符串(例如,通过空格字符拆分字符串)。返回值是一个包含每个拆分值的数组。请参阅该方法的文档。
Scanner myKeyboard = new Scanner(System.in);
System.out.print("Please enter Your full Name , Middle name And Surname: ");
String name = myKeyboard.nextLine();
String[] nameParts = name.split(" ");
char firstInitial = nameParts[0].charAt(0);
char middleInitial = nameParts[1].charAt(0);
char lastInitial = nameParts[2].charAt(0);
System.out.println ("Initials Are " + firstInitial + middleInitial + lastInitial);
Note that the above assumes the user has entered the right number of names. You'll need to do some catching or checking if you need to safeguard against the users doing "weird" things.
请注意,以上假设用户输入了正确数量的姓名。如果您需要防止用户做“奇怪”的事情,您需要做一些捕获或检查。