java:for循环在BigInteger的情况下如何工作

发布于 2024-09-05 20:16:15 字数 181 浏览 7 评论 0原文

我想将用户的输入作为大整数并将其操纵到 For 循环中,

BigInteger i;
for(BigInteger i=0; i<=100000; i++) {
    System.out.println(i);
}

但它不会起作用,

任何人都可以帮助我。

I want to take Input from the user as Big-Integer and manipulate it into a For loop

BigInteger i;
for(BigInteger i=0; i<=100000; i++) {
    System.out.println(i);
}

But it won't work

can any body help me.

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

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

发布评论

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

评论(5

半边脸i 2024-09-12 20:16:15

您可以改用这些语法:

BigInteger i = BigInteger.valueOf(100000L);  // long i = 100000L;
i.compareTo(BigInteger.ONE) > 0              // i > 1
i = i.subtract(BigInteger.ONE)               // i = i - 1

下面是一个将它们放在一起的示例:

    for (BigInteger bi = BigInteger.valueOf(5);
            bi.compareTo(BigInteger.ZERO) > 0;
            bi = bi.subtract(BigInteger.ONE)) {

        System.out.println(bi);
    }
    // prints "5", "4", "3", "2", "1"

请注意,使用 BigInteger 作为循环索引是非常不典型的。 long 通常足以满足此目的。

API 链接


compareTo 习惯用法

来自文档:

此方法优先于六个布尔比较运算符(<==> 中每一个的单独方法提供、>=!=<=)。执行这些比较的建议习惯用法是:(x.compareTo(y)0),其中 是六个比较运算符之一。

换句话说,给定 BigInteger x, y,这些是比较习惯用法:

x.compareTo(y) <  0     // x <  y
x.compareTo(y) <= 0     // x <= y
x.compareTo(y) != 0     // x != y
x.compareTo(y) == 0     // x == y
x.compareTo(y) >  0     // x >  y
x.compareTo(y) >= 0     // x >= y

这不是特定于 BigInteger;这适用于任何 Comparable< /code>一般来说。


关于不可变性的注意事项

BigIntegerString 一样,是一个不可变的对象。初学者往往会犯以下错误:

String s = "  hello  ";
s.trim(); // doesn't "work"!!!

BigInteger bi = BigInteger.valueOf(5);
bi.add(BigInteger.ONE); // doesn't "work"!!!

由于它们是不可变的,因此这些方法不会改变它们所调用的对象,而是返回新对象,即这些操作的结果。因此,正确的用法是这样的:

s = s.trim();
bi = bi.add(BigInteger.ONE);

You use these syntax instead:

BigInteger i = BigInteger.valueOf(100000L);  // long i = 100000L;
i.compareTo(BigInteger.ONE) > 0              // i > 1
i = i.subtract(BigInteger.ONE)               // i = i - 1

So here's an example of putting it together:

    for (BigInteger bi = BigInteger.valueOf(5);
            bi.compareTo(BigInteger.ZERO) > 0;
            bi = bi.subtract(BigInteger.ONE)) {

        System.out.println(bi);
    }
    // prints "5", "4", "3", "2", "1"

Note that using BigInteger as a loop index is highly atypical. long is usually enough for this purpose.

API links


The compareTo idiom

From the documentation:

This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y)<op>0), where <op> is one of the six comparison operators.

In other words, given BigInteger x, y, these are the comparison idioms:

x.compareTo(y) <  0     // x <  y
x.compareTo(y) <= 0     // x <= y
x.compareTo(y) != 0     // x != y
x.compareTo(y) == 0     // x == y
x.compareTo(y) >  0     // x >  y
x.compareTo(y) >= 0     // x >= y

This is not specific to BigInteger; this is applicable to any Comparable<T> in general.


Note on immutability

BigInteger, like String, is an immutable object. Beginners tend to make the following mistake:

String s = "  hello  ";
s.trim(); // doesn't "work"!!!

BigInteger bi = BigInteger.valueOf(5);
bi.add(BigInteger.ONE); // doesn't "work"!!!

Since they're immutable, these methods don't mutate the objects they're invoked on, but instead return new objects, the results of those operations. Thus, the correct usage is something like:

s = s.trim();
bi = bi.add(BigInteger.ONE);
空宴 2024-09-12 20:16:15

好吧,首先,你有两个名为“i”的变量。

其次,用户输入在哪里?

第三, i=i+i 将 i 拆箱为一个原始值,可能会溢出它,并将结果装箱到一个新对象中(也就是说,如果该语句甚至可以编译,我还没有检查)。

第四,i=i+i 可以写成 i = i.multiply(BigInteger.valueof(2))。

第五,循环永远不会运行,因为 100000 <= 1 是假的。

Well, first of all, you have two variables called "i".

Second, where's the user input?

Third, i=i+i unboxes i into a primitive value, possibly overflowing it, and boxes the result in a new object (that is, if the statement even compiles, which I haven't checked).

Fourth, i=i+i can be written as i = i.multiply(BigInteger.valueof(2)).

Fifth, the loop is never run, because 100000 <= 1 is false.

不爱素颜 2024-09-12 20:16:15

我认为这段代码应该可以工作

public static void main(String[] args) {
    BigInteger bigI = new BigInteger("10000000");
    BigInteger one = new BigInteger("1");

    for (; bigI.compareTo(one) == 0; bigI.subtract(one)) {
       bigI = bigI.add(one);
    }
}

I think this code should work

public static void main(String[] args) {
    BigInteger bigI = new BigInteger("10000000");
    BigInteger one = new BigInteger("1");

    for (; bigI.compareTo(one) == 0; bigI.subtract(one)) {
       bigI = bigI.add(one);
    }
}
苦行僧 2024-09-12 20:16:15

这可以工作

BigInteger i;
        for(i = BigInteger.valueOf(0);i.compareTo(new BigInteger("100000")) 
            <=0;i=i.add(BigInteger.ONE))
        {
            System.out.println(i);
        }

(或)

for(BigInteger i = BigInteger.valueOf(0);i.compareTo(new BigInteger("100000")) 
            <=0;i=i.add(BigInteger.ONE))
        {
            System.out.println(i);
        }

注意不要声明 BigInteger 两次。

This could work

BigInteger i;
        for(i = BigInteger.valueOf(0);i.compareTo(new BigInteger("100000")) 
            <=0;i=i.add(BigInteger.ONE))
        {
            System.out.println(i);
        }

(or)

for(BigInteger i = BigInteger.valueOf(0);i.compareTo(new BigInteger("100000")) 
            <=0;i=i.add(BigInteger.ONE))
        {
            System.out.println(i);
        }

Note that don't declare BigInteger twice.

属性 2024-09-12 20:16:15

这是为了按升序运行循环:

BigInteger endValue = BigInteger.valueOf(100000L);

for (BigInteger i = BigInteger.ZERO; i.compareTo(endValue) != 0; i = i.add(BigInteger.ONE)) {
   //... your logic inside the loop
}

This is for running the loop in ascending order:

BigInteger endValue = BigInteger.valueOf(100000L);

for (BigInteger i = BigInteger.ZERO; i.compareTo(endValue) != 0; i = i.add(BigInteger.ONE)) {
   //... your logic inside the loop
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文