简体   繁体   中英

Why the program gives wrong output?

BigInteger p = new BigInteger("1");
BigInteger m = new BigInteger("1");
BigInteger j = new BigInteger("1");
BigInteger n = new BigInteger("3");
BigInteger one = new BigInteger("1");
while (m.compareTo(n) == -1) {
    while (j.compareTo(n) == -1) {
        p = m.multiply(j);
        System.out.println("m=" + m + " j=" + j + " p=" + p);
        j = j.add(one);
    }
    m = m.add(one);
}

Why the output is

m=1 j=1 p=1  
m=1 j=2 p=2 

Shouldn't it be

m=1 j=1 p=1  
m=1 j=2 p=2  
m=2 j=1 p=1  
m=2 j=2 p=2  

?

No. Why would j revert from 2 to 1? You are only ever adding to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM