问题
import java.lang.Math;
import java.math.BigInteger;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
int e1 = 20, d = 13;
BigInteger C = BigDecimal.valueOf(e1).toBigInteger();
BigInteger po = C.pow(d);
System.out.println("pow is:" + po);
int num = 11;
BigInteger x = po;
BigInteger n = BigDecimal.valueOf(num).toBigInteger();
BigInteger p, q, m;
System.out.println("x: " + x);
q=(x / n);
p=(q * n);
m=(x - p);
System.out.println("mod is:" + m);
}
}
I've tried looking for some answers related to it but unable to solve. Please can someone tell me what's wrong in this. I changed the datatype to integer but then the power function doesn't works.
this is the error which I get:
error: bad operand types for binary operator '/'
q=(x/n);
^
first type: BigInteger
second type: BigInteger
Main.java:33: error: bad operand types for binary operator '*'
p=(q*n);
^
first type: BigInteger
second type: BigInteger
Main.java:34: error: bad operand types for binary operator '-'
m=(x-p);
^
first type: BigInteger
second type: BigInteger
3 errors
.
回答1:
Explanation
You can not use the operators on BigInteger
. They are not primitives like int
, they are classes. Java has no operator overloading.
Take a look at the class documentation and use the corresponding methods:
BigInteger first = BigInteger.ONE;
BigInteger second = BigInteger.TEN;
BigInteger addResult = first.add(second);
BigInteger subResult = first.subtract(second);
BigInteger multResult = first.multiply(second);
BigInteger divResult = first.divide(second);
Operator Details
You can look up the detailed definitions for the operators and when you can use them in the Java Language Specification (JLS).
Here are some links to the relevant sections:
- Multiplication
*
§15.17.1 - Division
/
§15.17.2 - String concatenation
+
§15.18.1 - Addition and subtraction
+
-
§15.18.2
Most of them work with the notion of Numeric Type §4, which consists of Integral Type and FloatingPointType:
The integral types are
byte
,short
,int
, andlong
, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, andchar
, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).The floating-point types are
float
, whose values include the 32-bit IEEE 754 floating-point numbers, anddouble
, whose values include the 64-bit IEEE 754 floating-point numbers.
Additionally, Java can unbox wrapper classes like Integer
into int
and vice versa, if needed. That adds the unboxing conversions §5.1.8 to the set of supported operands.
Notes
Your creation of BigInteger
is unnecessarily long and complicated:
// Yours
BigInteger C = BigDecimal.valueOf(e1).toBigInteger();
// Prefer this instead
BigInteger c = BigInteger.valueOf(e1);
And if possible, you should prefer to go from String
to BigInteger
and from BigInteger
to String
. Since the purpose of BigInteger
is to use it for numbers that are too big to be represented with the primitives:
// String -> BigInteger
String numberText = "10000000000000000000000000000000";
BigInteger number = new BigInteger(numberText);
// BigInteger -> String
BigInteger number = ...
String numberText = number.toString();
Also, please stick to Java naming conventions. Variable names should be camelCase, so c
and not C
.
Additionally, prefer to have meaningful variable names. A name like c
or d
does not help anyone to understand what the variable is supposed to represent.
回答2:
Arithmetic operations are not working on Objects in Java. However there are already methods to do so like BigInteger#add, BigInteger#divide etc. in BigInteger. Instead of doing
q=(x/n)
you would do
q = x.divide(n);
回答3:
You can't do operands such as "*","/","+" on objects in Java, if you want these operations you need to do it like this
q = x.divide(n);
p=q.multiply(n);
m=x.subtract(p);
来源:https://stackoverflow.com/questions/55791203/how-to-use-operators-for-biginteger