您的位置:首页 > 编程语言 > Java开发

JAVA--封装一类Java对象,计算两个大整数(如123456789123456789123456789和987654321987654321987654321)

2012-11-01 18:46 302 查看
import java.math.*;
public class Date {
//3.封装一类Java对象,计算两个大整数(如123456789123456789123456789和987654321987654321987654321)
//的和、差、积和商,并计算一个大整数的因子个数(因子中不包括1和大整数本身)。(选做)

public static void main(String[] args)
{

BigInteger sum = new BigInteger("0"),

difference = new BigInteger("0"),

product  = new BigInteger("0"),

quotient = new BigInteger("0"),
type1 = new BigInteger("123456789123456789123456789"),

type2 = new BigInteger("987654321987654321987654321");

sum = type1.add(type2);

difference  = type1.subtract(type2);

product  = type1.multiply(type2);

quotient  = type1.divide(type2);

BigInteger i = new BigInteger("0");

BigInteger one = new BigInteger("1");

BigInteger two = new BigInteger("0");

BigInteger yu = new BigInteger("0");

int count =0;
while(i.compareTo(type1)<=0)
{
yu = i.remainder(type1);
if(yu==two)
{
count++;
}
i = i.add(one);
}

System.out.println("两个大整数的和是:"+sum);
System.out.println("两个大整数的差是:"+difference);
System.out.println("两个大整数的积是:"+product);
System.out.println("两个大整数的商是:"+quotient);
System.out.println("第一个大整数的因子个数为:"+count);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐