如何在java中将数字组合在一起?

发布于 2024-12-08 03:29:49 字数 679 浏览 0 评论 0原文

由于某种原因,当我运行以下代码时,程序打印出“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情释 2024-12-15 03:29:49

您的问题是 randomNumbers 被声明为 int,因此当您在其他整数上使用 + 时,它将对整数求和而不是连接他们。

因此,如果我们要使用您的方法,我们需要一个字符串:

String userName = initials + randomNumber1 + randomNumber2 + randomNumber3;

但是生成一个 0-999 之间的数字并用零填充会容易得多:

int randomNumber = generator.nextInt(1000);
String usernName = initials + String.format("%03d", randomNumber);

还有一点值得注意的是 Random.nextInt(n) 生成一个介于 0(含)和 n(不包括)之间的随机数,所以你可能应该这样做 .nextInt(10) 如果您想要 0 到 9 之间的数字。

Your problem is randomNumbers is declared as an int, 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:

String userName = initials + randomNumber1 + randomNumber2 + randomNumber3;

But it would be a lot easier just to generate a number from 0-999 and pad it with zeroes:

int randomNumber = generator.nextInt(1000);
String usernName = initials + String.format("%03d", randomNumber);

Something else worth noting is that Random.nextInt(n) generates a random number between 0 (inclusive) and n (exclusive), so you probably should be doing .nextInt(10) if you want a number between 0 and 9.

感悟人生的甜 2024-12-15 03:29:49

假设您有三个数字:

int randomNumber1 = 3;
int randomNumber2 = 4;
int randomNumber3 = 5;
int randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;

您希望 randomNumbers 为 345 吗?或者你预计它是12?

现在试试这个:

String randomNumbers = "" + randomNumber1 + randomNumber2 + randomNumber3;

这次你得到了什么?

Suppose you have three numbers:

int randomNumber1 = 3;
int randomNumber2 = 4;
int randomNumber3 = 5;
int randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;

Do you expect randomNumbers to be 345? Or do you expect it to be 12?

Now try this:

String randomNumbers = "" + randomNumber1 + randomNumber2 + randomNumber3;

What do you get this time?

望她远 2024-12-15 03:29:49

尝试,

String userName=String.format("%s%d%d%d",initials,randomNumber1,randomNumber2,randomNumber3);

Try,

String userName=String.format("%s%d%d%d",initials,randomNumber1,randomNumber2,randomNumber3);
Oo萌小芽oO 2024-12-15 03:29:49
randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;

您正在添加数字,而不是连接它们。

建议的解决方案:仅使用 1 个随机数

generator.nextInt(1000);

,或者,您可以像这样:

String userName = initials + randomNumer1 + randomNumber2 + randomNumber3;

但我几乎确定您会得到 2 甚至 3 倍相同的数字,因此您应该使用第一种方法。

randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;

You're adding the numbers, not concatenating them.

Suggested solution: use only 1 random number with

generator.nextInt(1000);

Or, you can go like:

String userName = initials + randomNumer1 + randomNumber2 + randomNumber3;

But I'm nearly sure you'll get 2 or even 3 times the same number, so you should use the first approach instead.

甜味拾荒者 2024-12-15 03:29:49

您正在此处添加数字。

randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;

如果您想使用相同的方法来实现您正在寻找的目标,您可以这样做。

randomNumbers = randomNumber1 + (randomNumber2 * 10) + (randomNumber3 * 100);

You are adding the numbers here.

randomNumbers = randomNumber1 + randomNumber2 + randomNumber3;

If you want to achieve what you are looking for using this same method you could do.

randomNumbers = randomNumber1 + (randomNumber2 * 10) + (randomNumber3 * 100);
池木 2024-12-15 03:29:49

错误在于,+ 运算符在用于 int 变量时执行的是加法,而不是连接。

用于

String randomNumbers = Integer.toString(randomNumber1) + Integer.toString(randomNumber2) + Integer.toString(randomNumber3);

将数字转换为字符串并将它们连接起来。

What's wrong is that the + operator does an addition, and not a concatenation when used on int variables.

Use

String randomNumbers = Integer.toString(randomNumber1) + Integer.toString(randomNumber2) + Integer.toString(randomNumber3);

to convert the numbers into strings and concatenate them.

昇り龍 2024-12-15 03:29:49

您添加 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文