
正文
大数运算java源代码 java 大数字
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java的大整数运算,总是运行时出错,提示 java.lang.NullPointerException的异常,以下是代码
努力跟着你这位大神的思路,终于看清了;
先抛开Scanner和while 循环。
for循环里面
iter.subtract(BigInteger.ONE) 表示 iter=iter-1 造成死循环
bInt[iter.intValue()],10000的时候数组越界。
相关问答
Q1: 求java源程序代码:输入两个数,输出最大值
public class Test{
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
System.out.println("请输入a值:");
int a = scanner.nextInt();
System.out.println("请输入b的值:");
int b = scanner.nextInt();
if(a b)
System.out.println("最大的数是:" + a);
else
System.out.println("最大的数是:" + b);
}
}
Q2: java数组实现大数相加
package com.nileader.big.entity;
public class BigInt {
private String data_str; //原始数据
private int digit; //数据位数
private int[] data; //大数
private boolean carry = false; //进位标识符
/**
* setter and getter
*/
public boolean isCarry() {
return carry;
}
public void setCarry(boolean carry) {
this.carry = carry;
}
public String getData_str() {
return data_str;
}
public void setData_str(String data_str) {
this.data_str = data_str;
}
public int[] getData() {
return data;
}
public void setData(int[] data) {
this.data = data;
}
public int getDigit() {
return digit;
}
public void setDigit(int digit) {
this.digit = digit;
}
//构造方法
public BigInt(){};
public BigInt(String str_data)
{
this.setData_str(str_data);
}
//基本操作
/**
* 形成大数 初始化
*/
public void initBInt()
{
this.setDigit( this.getData_str().length() );
this.data = new int[this.getDigit()];
//将字符组成的大数逆序放入int[] data中
for(int i = 0, j=this.getDigit() ; i
{
// 1104 -- data[0] = '4',data[1] = '0',data[2]=1, data[3]= '1'
this.data[i] = Integer.parseInt( this.getData_str().substring(j-1,j) );
}
}
/**
* 进行大数相加操作
*/
public void add( BigInt bint)
{
//this的位数大于bint的位数
if( this.getDigit() bint.getDigit() )
{
int[] datatemp = this.getData();
this.setData( bint.getData());
bint.setData( datatemp);
this.setDigit(this.getData().length);
bint.setDigit(bint.getData().length);
}
//将短的那个先加完
int i =0;
for(; i
{
int tdata = 0;
//上次运算有进位
if( this.isCarry())
{
tdata = this.getData()[i] + bint.getData()[i] +1;
//取消进位标识
this.setCarry(false);
}
else tdata = this.getData()[i] + bint.getData()[i] ;
//本次结果无进位
if(tdata 10) this.data[i] = tdata;
//本次结果有进位
else if(tdata =10)
{
this.data[i] = tdata -10;
this.setCarry(true);
}
} //短的那个加完了
//剩余数的处理
for(;i
{
//有个进位的
if(this.isCarry())
{
int tdata = this.data[i]+1;
if(tdata =10) this.data[i] = tdata -10;
else
{
this.data[i] = tdata;
this.setCarry(false);
}
}
}
//对最高位益处检测
if(this.data[this.getDigit()-1] == 0)
{
int[] tdata = new int[this.getDigit()+1];
System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit());
tdata[this.getDigit()] = 1;
this.setData(tdata);
}
}
}
其中代码段
//对最高位益处检测
if(this.data[this.getDigit()-1] == 0)
{
int[] tdata = new int[this.getDigit()+1];
System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit());
tdata[this.getDigit()] = 1;
this.setData(tdata);
}
Q3: java程序代码求助关于HugeInteger(最大数)的四则运算,题目如下
错误是因为你的HugeInteger类里需要定义好多方法,但是你的HugeInteger类中都没有,我把你用到的这些方法的类型与作用说出来,你自己在HugeInteger类里面写。
1、void parse(String a) 把String a转换为HugeInteger
2、String toString() 返回HugeInteger的字符串表达形式
3、void add(HugeInteger other) 把other加到当前HugeInteger对象上
4、void substract(HugeInteger other) 用当前对象减去other
5、boolean isZero() 判断当前对象是否为0
6、boolean isNotEqualTo(HugeInteger other) 判断当前对象与other是否相等
7、boolean isGreaterThan(HugeInteger other) 判断当前对象是否比other大
8、boolean isLessThan(HugeInteger other) 判断当前对象是否比other小
9、boolean isGreaterThanOrEqualTo(HugeInteger other) 判断当前对象是否大于等于other
10、boolean isLessThanOrEqualTo(HugeInteger other) 判断当前对象是否小于等于other
Q4: 运用JAVA中大数类实现大数的四则运算
import java.math.BigInteger;
public class BigIntegerGet {
public String getAdd(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1.add(BigInt2);
Str3=BigInt1.toString();
return Str3;
}
public String getSubtract(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1.subtract(BigInt2);
Str3=BigInt1.toString();
return Str3;
}
public String getMultiply(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1.multiply(BigInt2);
Str3=BigInt1.toString();
return Str3;
}
public String getDivide(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1.divide(BigInt2);
Str3=BigInt1.toString();
return Str3;
}
}
大数运算java源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 大数字、大数运算java源代码的信息别忘了在本站进行查找喔。







