我正在制作 BCD 课程作为学校练习,但遇到了一些问题。以下是我的 BCD 课程。
我的问题是与multiplyBCDs 方法有关。 它适用于较小的数字,例如 4,329 * 4,但是,对于较大的乘积,例如 4,329 和 29,385 的乘积,我在 addBCDs 方法的第一行收到 NullPointerException 错误:
int[] added = new int[other.numberOfDigits()];
我尝试过回溯问题,但找不到问题所在。为什么我会收到此错误以及如何修复它?
public class BCD {
private int[] digits;
//Constructor: takes in an array of integers as an argument
public BCD(int bcdDigits[]){
digits = bcdDigits;
}
//Constructor: takes in an integer argument
public BCD(int bcdDigits){
int b = bcdDigits;
int count = 0;
int c = 0;
int length = String.valueOf(bcdDigits).length();
digits = new int[length];
while(b >= 1){
c = b % 10;
b = b / 10;
digits[count] = c;
count++;
}
}
//Returns the number of digits in the BCD
public int numberOfDigits(){
return digits.length;
}
//Returns the nth digit in a BCD
public int nthDigit(int n){
if(n >= digits.length)
return 0;
else
return digits[n];
}
//Prints digits in reverse order. After every index divisible by three, it outputs a comma.
public void print(){
int count = 1;
for (int x=1; x<=digits.length; x++){
if (digits.length<=3){
System.out.print(digits[digits.length-x]); }
else {
if (digits.length % 3==0){
if (count % 3==0 && count != digits.length)
System.out.print(digits[digits.length-x]+"," );
else
System.out.print(digits[digits.length-x]);
}
if (digits.length % 3==1){
if (count % 3==1 && count != digits.length)
System.out.print(digits[digits.length-x]+"," );
else
System.out.print(digits[digits.length-x]);
}
if (digits.length % 3==2){
if (count % 3==2 && count != digits.length)
System.out.print(digits[digits.length-x]+"," );
else
System.out.print(digits[digits.length-x]);
}
}
count++;
}
System.out.println();
}
//Adds one more digit to the BCD
public void addADigit(int newDigit){
int[] tempArray = new int[digits.length + 1];
for(int x = 0; x < tempArray.length - 1 ; x++){
tempArray[x] = digits[x];
}
tempArray[digits.length] = newDigit;
digits = tempArray;
}
//Adds two BCDs together and creates a new BCD
public BCD addBCDs(BCD other){
//converts the BCD other to an array
int[] added = new int[other.numberOfDigits()];
for(int x = 0; x < other.numberOfDigits(); x++){
added[x] = other.nthDigit(x);
}
int greater[];
int smaller[];
int diff;
//finding greater addends
if(added.length > digits.length){
greater = added.clone();
smaller = new int[greater.length];
for(int x = 0; x < greater.length; x++){
if(x < digits.length)
smaller[x] = digits[x];
else
smaller[x] = 0;
}
}
else{
greater = digits.clone();
smaller = new int[greater.length];
for(int x = 0; x < greater.length; x++){
if(x < added.length)
smaller[x] = added[x];
else
smaller[x] = 0;
}
}
int carry = 0;
int[] sum = new int[greater.length + 1];
for(int x = 0; x < sum.length - 1; x++){
sum[x] = ((greater[x] + smaller[x]) % 10 + carry);
carry = (greater[x] + smaller[x]) / 10;
}
sum[sum.length - 1] = carry;
int[] newSum;
BCD ans;
if(sum[sum.length - 1] == 0){
newSum = new int[sum.length - 1];
for(int x = 0; x < sum.length - 1; x++){
newSum[x] = sum[x];
}
ans = new BCD(newSum);
}
else{
newSum = sum.clone();
ans = new BCD(newSum);
}
return ans;
}
public BCD multiplyByTen(){
BCD ans;
int[] newDigits;
if(digits[0] == 0 && digits.length == 1){
ans = new BCD(0);
return ans;
}
else{
newDigits = new int[digits.length + 1];
newDigits[0] = 0;
for(int x = 1; x <= digits.length ; x++){
newDigits[x] = digits[x - 1];
}
ans = new BCD(newDigits);
return ans;
}
}
public BCD multiplyBy(int num){
BCD ans = null;
if(digits.length == 1 && digits[0] == 0){
ans = new BCD(0);
return ans;
}
else if(num == 0){
ans = new BCD(0);
return ans;
}
else if(num == 1){
ans = new BCD(digits);
return ans;
}
else if(num == 10){
BCD ans1 = new BCD(digits);
ans = ans1.multiplyByTen();
return ans;
}
else{
int carry = 0;
int remainder = 0;
int prod = 0;
int[] answer= new int[(digits.length)];
int[] newDigits;
for(int x = 0; x < digits.length; x++){
prod = (num * digits[x]) + carry;
carry = prod / 10;
remainder = prod % 10;
answer[x] = remainder;
if(x == digits.length - 1 && carry != 0){
ans = new BCD(answer);
int length = String.valueOf(carry).length();
newDigits = new int[length + 1];
int b = carry;
int c = 0;
while(b > 0){
c = b % 10;
b = b / 10;
ans.addADigit(c);
}
}
}
}
return ans;
}
public BCD multiplyBCDs(BCD other){
BCD newBCD = new BCD(0);
int digit = 0;
BCD dig = new BCD(0);
do{
dig = other.multiplyBy(digits[digit]);
newBCD = newBCD.addBCDs(dig);
other = other.multiplyByTen();
digit++;
}while (digit < digits.length);
return newBCD;
}
感谢您的帮助!
方法中:
public BCD multiplyBy(int num)
在最后一个 else 语句中,从未满足以下条件:
if (x == digits.length - 1 && carry != 0)
因此“ans”永远不会被设置并保持为空。
int[] added = new int[other.numberOfDigits()];
在该行获得 NPE 的唯一方法是如果
other
为空。