数字太长,无法传递给 Random() ?

发布于 2024-11-17 06:16:03 字数 273 浏览 4 评论 0原文

我正在尝试做

Random generator = new Random(1309233053284);

Randomjava.util.Random

它说数字太长,但为什么可以 System.currentTimeMillis()传递给构造函数?它返回更大的数字。

如果您想知道的话,1309233053284 是毫秒。

I'm trying to do

Random generator = new Random(1309233053284);

Random being java.util.Random

It says the number is too long, but why can System.currentTimeMillis() be passed to the constructor? It returns even bigger numbers.

1309233053284 are milliseconds, if you're wondering.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

回眸一笑 2024-11-24 06:16:03

您可能会有更好的运气:

Random generator = new Random(1309233053284L);

在 Java 中,除非另有说明,所有文字数字都是 int 类型。要将您的数字解释为 long,您需要在其后面添加“L”(或“l”,但这很难与“1”区分开来,因此不太清晰) 。

You may have better luck with:

Random generator = new Random(1309233053284L);

In Java, all literal numbers are of type int unless otherwise specified. To get your number interpreted as a long, you need to suffix it with 'L' (or alternately 'l', but that is difficult to distinguish from a '1', and therefore somewhat less clear).

会发光的星星闪亮亮i 2024-11-24 06:16:03

试试这个

Random generator = new Random(1309233053284l);

你应该将它指定为 long。

如果您调用 new Random(1309233053284),它将使用带有 int 参数的构造函数。当您调用 new Random(System.currentTimeMillis()) 时,它使用带有 long 参数的构造函数,因为 System.currentTimeMillis() 返回一个长的。为了使其正常工作,您还应该通过添加 l1309233053284 指定为 long。

Try this

Random generator = new Random(1309233053284l);

You should specify it as a long.

If you call new Random(1309233053284), it will use the constructor taking an int argument. When you call new Random(System.currentTimeMillis()), it's using the constructur taking a long argument since System.currentTimeMillis() returns a long. To make it work, you should also specify 1309233053284 to be a long by adding the l.

撑一把青伞 2024-11-24 06:16:03

整数文字默认为 int 类型。您需要为 float 添加 f,为 double 添加 d,为 long 添加 LL 优于 l,因为后者看起来像 1

例如

31 <= 31 as an int
3l <= looks like 31 but is 3 as a long.
31L <= 31 as a long.
311 <= is 311 as an int.

integer literals are int type by default. You need to add f for float, d for double and L for long. L is preferred to l as the later can look like 1

e.g.

31 <= 31 as an int
3l <= looks like 31 but is 3 as a long.
31L <= 31 as a long.
311 <= is 311 as an int.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文