如何创建名称生成器?
我正在创建一个程序,提示输入名字和姓氏,然后打印一个由用户名字的第一个字母组成的字符串,后跟用户姓氏的前五个字符,然后是 10 到 99 范围内的随机数我
知道如何提示输入姓名并找到随机数,但我不确定如何
“打印由名字的第一个字母和姓氏的前五个字母组成的字符串”。
谁能帮助我吗?我是一个非常初级的Java程序员。
所以我真的很接近完成这个,但它一直说第 55 行“非法的表达式开始”,我不明白。这是我的代码,抱歉,我知道这很混乱:
Random generator = new Random();
int num1;
num1 = generator.nextInt(10-99);
line 55: public String substring; <<<
String result;
System.out.println("Result:" + (beginIndex) + (firstname.substring(0,1) + lastname. substring (0,5)) + (num1) );
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.
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.
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:
Random generator = new Random();
int num1;
num1 = generator.nextInt(10-99);
line 55: public String substring; <<<
String result;
System.out.println("Result:" + (beginIndex) + (firstname.substring(0,1) + lastname. substring (0,5)) + (num1) );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说这似乎是家庭作业,所以我会给出提示。查找方法 substring() 和 charAt()第一部分,以及 第二个随机类。
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 class for the second.
我是一名 .NET 开发人员,所以我无法帮助您了解语法,但您需要获取名字的第一个字符(通常可通过索引器访问)-firstName.charAt(0),以及第二个名字的子字符串范围从第一个字符(序数 0)到第五个字符(序数 4),可能类似于 lastName.substring(0, 4);并连接这两个字符串 -
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 -
像这样的事情会做
Something like this will do
您缺少 println 的右括号。
我建议删除字符串连接周围的所有括号,它们只会使其难以阅读。
另外,如果用户输入的姓氏只有 4 个字符,会发生什么情况?
You're missing a closing parentheses for println.
I would recommend removing all the parentheses around the string concats they just make it hard to read.
Also what happens if the user enters a last name with only 4 characters?
看一下 String.substring()
Take a look at String.substring()
看看这有多容易吗?我给出了4个简单的名字,可以用单词等代替。代码中的“4”代表字符串中名称的数量。这就是最简单的事情了。对于那些想要更短的人(我所做的只是减小间距):
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):