java 如何创建名称生成器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6155067/
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
How do I create a name generator?
提问by Chickadee
I am creating a program that prompts a first and last name then prints a string composed of the first letter of the user's first name, followed by the first five characters of the user's last name, followed by a random number in the range 10 to 99.
我正在创建一个提示名字和姓氏的程序,然后打印一个由用户名字的第一个字母组成的字符串,后跟用户姓氏的前五个字符,后跟一个 10 到 99 范围内的随机数.
I know how to prompt for the name and find the random number but I'm not sure how to
我知道如何提示输入名称并找到随机数,但我不知道如何
"print a string composed of the first letter of the first name, followed by the first five letters of the last name."
“打印由名字的第一个字母组成的字符串,后跟姓氏的前五个字母。”
Can anyone help me? I am a very elementary Java programmer.
谁能帮我?我是一个非常初级的Java程序员。
So I am really close to finishing this but it keeps saying "illegal start of expression" for line 55 and I can't figure it out. Here is my code, sorry, I know it's a mess:
所以我真的很接近完成这个,但它一直在说第 55 行的“非法的表达开始”,我无法弄清楚。这是我的代码,抱歉,我知道这是一团糟:
Random generator = new Random();
int num1;
num1 = generator.nextInt(10-99);
line 55: public String substring;<<<
第 55 行:公共字符串子字符串;<<<
String result;
System.out.println("Result:" + (beginIndex) + (firstname.substring(0,1) + lastname. substring (0,5)) + (num1) );
回答by MByD
Seems like homework to me, so I will give a hint. look for the method substring()and charAt()for the first part, and the Random classfor the second.
对我来说似乎是家庭作业,所以我会给出提示。第一部分查找方法substring()和charAt(),第二部分查找Random 类。
回答by cchamberlain
I am a .NET developer so I can't help you with the syntax but you would need to grab the first char of the first name, usually accessible via an indexer - firstName.charAt(0), and a substring of the second one that ranges from the first character (ordinal 0) to the 5th character (ordinal 4), likely something like lastName.substring(0, 4); and concatenate these two strings -
我是 .NET 开发人员,所以我无法帮助您处理语法,但您需要获取名字的第一个字符,通常可通过索引器访问 - firstName.charAt(0),以及第二个字符的子字符串范围从第一个字符(序数 0)到第 5 个字符(序数 4),可能类似于 lastName.substring(0, 4); 并连接这两个字符串 -
concatenatedName = firstName.charAt(0) + lastName.substring(0, 4);
回答by Ragnar123
Something like this will do
像这样的事情会做
import java.lang.String;
import java.io.*;
import java.util.Random;
class Name {
public static void main(String args[]) {
Random rnd = new Random(); // Initialize number generator
String firstname = "Jessica"; // Initialize the strings
String lastname = "Craig";
String result; // We'll be building on this string
// We'll take the first character in the first name
result = Character.toString(firstname.charAt(0)); // First char
if (lastname.length() > 5)
result += lastname.substring(0,5);
else
result += lastname; // You did not specify what to do, if the name is shorter than 5 chars
result += Integer.toString(rnd.nextInt(99));
System.out.println(result);
}
}
回答by Michael Krussel
You're missing a closing parentheses for println.
您缺少 println 的右括号。
I would recommend removing all the parentheses around the string concats they just make it hard to read.
我建议删除字符串 concats 周围的所有括号,它们只会使其难以阅读。
System.out.println("Result:" + beginIndex + firstname.substring(0,1) + lastname.substring(0,5) + num1 );
Also what happens if the user enters a last name with only 4 characters?
如果用户输入只有 4 个字符的姓氏,会发生什么情况?
回答by R1D1CUL3
See how easy it is? I gave 4 simple names which can be replaced with words and such. The "4" in the code represents the number of names in the String. This is about as simple as it gets. And for those who want it even shorter(all I did was decrease the spacing):
看看它有多容易?我给了 4 个简单的名字,可以用单词等代替。代码中的“4”代表字符串中名称的数量。这很简单。对于那些想要更短的人(我所做的只是减少间距):
import java.util.*;
public class characters{
public static void main(String[] args){
Random generate = new Random();
String[] name = {"John", "Marcus", "Susan", "Henry"};
System.out.println("Customer: " + name[generate.nextInt(4)]); }}
回答by keuleJ
Take a look at String.substring()