JAVA–封裝一類Java對象,計算兩個大整數(如123456789123456789123456789和987654321987654321987654321)

來源:互聯網
上載者:User
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);     }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.