I'm attempting to divide the variable 'c' by 2(stored in 'd'). For some reason that doesn't happen. I'm passing 10 and 2 for input right now.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
BigInteger a = new BigInteger(sc.next());
BigInteger b = sc.nextBigInteger();
BigInteger d = new BigInteger("2");
System.out.println(d);
BigInteger c = a.subtract(b);
c.divide(d);
System.out.println(c);
a.subtract(c);
System.out.println(a);
System.out.println(c);
}
}
Any help would be appreciated. Thanks in advance!
You are forgetting that BigInteger
is immutable. That means that a.divide(b)
does not change a
it returns the result of the calculation .
You need a = a.divide(b)
or similar.
BigInteger a = new BigInteger(sc.next());
BigInteger b = sc.nextBigInteger();
BigInteger d = new BigInteger("2");
System.out.println(d);
BigInteger c = a.subtract(b);
// HERE!
c = c.divide(d);
System.out.println(c);
// HERE!
a = a.subtract(c);
System.out.println(a);
System.out.println(c);
BigInteger
is immutable. You get the result of c.divide(d);
as the return value, which you throw away in your code.
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.