Java作業6

來源:互聯網
上載者:User

標籤:數組   可變   使用   表達   pow   present   for迴圈   ++   repr   

1,利用二維數組和迴圈語句製作一個五子棋盤

 

 

 

 

 

 

 

2.編寫一個程式將整數轉化為漢字

 

 

 

 

 

3大數

四.大數

4.

前面幾講介紹過JDK所提供的BigInteger能完成大數計算,如果不用它,直接使用數組表達大數,你能實現相同的功能嗎? 要求: (1)用你的大數類實現加和減兩個功能 (2)閱讀BigInteger類源碼,弄清楚它是使用什麼演算法實現加減乘除四種運算的? (3)通過互連網尋找大數運算的相關資料,給你的大數類添加乘、除、求階乘等其它功能。

(1)BigInteger曆史介紹
在java中,存在很多種類的資料類型,例如byte short char int float double long,而BigInteger屬於其中一個比較特殊的資料類型,也是本教程關注的重點。BigInteger在JDK1.1中就已經存在了,屬於java.math包的類。從名字來看,BigInteger比Integer表示數值的範圍更大一些。BigInteger類的基本結構如下所示:
java.lang.Object
|_java.lang.Number
|_java.math.BigInteger
BigInteger已實現的介面:Serializable, Comparable<BigInteger>

(2)BigInteger是不可變的任意精度的整數。所有操作中,都以二進位補碼形式表示 BigInteger(如 Java 的基本整數類型)。BigInteger 提供所有 Java 的基本整數操作符的對應物,並提供 java.lang.Math 的所有相關方法。另外,BigInteger 還提供以下運算:模算術、GCD 計算、質數測試、素數產生、位操作以及一些其他動作。

(3)BigInteger屬性分析
下面看看BigInteger有哪些重點的屬性,主要的有下面三個:
(1)final int signum
signum屬性是為了區分:正負數和0的標誌位,JDK注釋裡面已經說的很明白了:
The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must have a signum of 0. This is necessary to ensures that there is exactly one representation for each BigInteger value.
(2)final int[] mag
mag是magnitude的縮寫形式,mag數組是儲存BigInteger數值大小的,採用big-endian的順序,也就是高位位元組存入低地址,低位位元組存入高地址,依次排列的方式。JDK原文注釋如下:
The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude. The magnitude must be "minimal" in that the most-significant int (mag[0]) must be non-zero. This is necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies that the BigInteger zero has a zero-length mag array.
(3)final static long LONG_MASK = 0xffffffffL;
This mask is used to obtain the value of an int as if it were unsigned。

 

package Work;import java.util.Scanner;public class BigNum {    public static void main(String[] args) {        // TODO Auto-generated method stub            int aa,bb;      System.out.println("用數組實現大數的加法和減法");      System.out.print("請輸入大數a:");      Scanner scan=new Scanner(System.in);      String a=scan.next();      System.out.print("請輸入大數b:");     String b=scan.next();     int A[]=new int[100];     int B[]=new int[100];          for(int i=0;i<a.length();i++){         A[i]=(int) ((a.charAt(i)-48)*Math.pow(10,a.length()-i-1));     }     for(int i=0;i<b.length();i++){         B[i]=(int) ((b.charAt(i)-48)*Math.pow(10,b.length()-i-1));     }     int sum=0;     int sub=0;     for(int i=0;i<a.length();i++){         sum+=A[i]+B[i];         sub+=A[i]-B[i];     }     System.out.print("a+b="+sum);      System.out.println();      System.out.print("a-b="+sub);    }}

結果解:

 

 

4隨機數

 

 

 

 

 

 

 

for迴圈在函數中產生十個隨機數,放入數組,放入sum湧入累加和。

 

Java作業6

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.