如何在java中将数字组合在一起?
由于某种原因,当我运行以下代码时,程序打印出“JDN4”或“JDN16”,而我始终希望它打印“JDN”,后跟 0-9 之间的三个数字;例如,“JDN123”或“JDN045”或“JDN206”。代码有什么问题吗?
import java.util.*;
public class Document2 {
public static void main(String[] args) {
String initials = "JDN";
int randomNumbers;
Random generator = new Random();
int randomNumber1 = generator.nextInt(9);
int randomNumber2 = generator.nextInt(9);
int randomNumber3 = generator.nextInt(9);
randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;
String userName = initials + randomNumbers;
System.out.println("Your username is " + userName + ".");
}
}
For some reason when I run the following code the program prints out "JDN4" or "JDN16" when at all times I want it to be printing "JDN" followed by three numbers ranging from 0-9; for example, "JDN123" or "JDN045" or "JDN206". What is wrong with the code?
import java.util.*;
public class Document2 {
public static void main(String[] args) {
String initials = "JDN";
int randomNumbers;
Random generator = new Random();
int randomNumber1 = generator.nextInt(9);
int randomNumber2 = generator.nextInt(9);
int randomNumber3 = generator.nextInt(9);
randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;
String userName = initials + randomNumbers;
System.out.println("Your username is " + userName + ".");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的问题是
randomNumbers
被声明为int
,因此当您在其他整数上使用+
时,它将对整数求和而不是连接他们。因此,如果我们要使用您的方法,我们需要一个字符串:
但是生成一个 0-999 之间的数字并用零填充会容易得多:
还有一点值得注意的是
Random.nextInt(n)
生成一个介于 0(含)和n
(不包括)之间的随机数,所以你可能应该这样做.nextInt(10)
如果您想要 0 到 9 之间的数字。Your problem is
randomNumbers
is declared as anint
, so when you use the+
on the other ints, it will sum the integers rather than concatenate them.So, if we were to use your approach, we'd need a string:
But it would be a lot easier just to generate a number from 0-999 and pad it with zeroes:
Something else worth noting is that
Random.nextInt(n)
generates a random number between 0 (inclusive) andn
(exclusive), so you probably should be doing.nextInt(10)
if you want a number between 0 and 9.假设您有三个数字:
您希望
randomNumbers
为 345 吗?或者你预计它是12?现在试试这个:
这次你得到了什么?
Suppose you have three numbers:
Do you expect
randomNumbers
to be 345? Or do you expect it to be 12?Now try this:
What do you get this time?
尝试,
Try,
您正在添加数字,而不是连接它们。
建议的解决方案:仅使用 1 个随机数
,或者,您可以像这样:
但我几乎确定您会得到 2 甚至 3 倍相同的数字,因此您应该使用第一种方法。
You're adding the numbers, not concatenating them.
Suggested solution: use only 1 random number with
Or, you can go like:
But I'm nearly sure you'll get 2 or even 3 times the same number, so you should use the first approach instead.
您正在此处添加数字。
如果您想使用相同的方法来实现您正在寻找的目标,您可以这样做。
You are adding the numbers here.
If you want to achieve what you are looking for using this same method you could do.
错误在于,
+
运算符在用于int
变量时执行的是加法,而不是连接。用于
将数字转换为字符串并将它们连接起来。
What's wrong is that the
+
operator does an addition, and not a concatenation when used onint
variables.Use
to convert the numbers into strings and concatenate them.
您添加 3 位数字,最多会得到 27 个数字。您想要连接这些数字的字符串表示形式。
也就是说,为什么不简单地生成一个 0 到 999 之间的随机数呢?
You are adding three digits, that would at most give you 27. You want to concatenate the string representation of those digits.
That said, why don't you simply generate a random number between 0 and 999?