【Java】數組及課後動手動腦

來源:互聯網
上載者:User

標籤:分析   his   字元   sign   數值   相同   string   value   位元組   

1.閱讀QiPan.java樣本程式瞭解如何利用二維數組和迴圈語句繪製五子棋盤。

結果及:

2.請編寫一個程式將一個整數轉換為漢字讀法字串。比如“1123”轉換為“一千一百二十三”。

結果:

3.更進一步,能否將數字表示的金額改為“漢字表達? 比如將“¥123.52”轉換為“壹佰貳拾三元伍角貳分”。

結果:

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。

 

import java.math.BigInteger;

public class text {

public static void main(String[] args) {

// TODO Auto-generated method stub

  BigInteger aa =new BigInteger("100");

  BigInteger bb= new BigInteger("25");

  BigInteger sub=aa.subtract(bb);//大整數的減

  BigInteger add=aa.add(bb);//大整數的加

  BigInteger mul=aa.multiply(bb);//大整數的乘

  BigInteger div=aa.divide(bb);//大整數的除

  System.out.println("大整數的減:"+sub.toString());

  System.out.println("大整數的加:"+add.toString());

  System.out.println("大整數的乘:"+mul.toString());

  System.out.println("大整數的除:"+div.toString());

  }

}

大整數的減:75

大整數的加:125

大整數的乘:2500

大整數的除:4

5.隨機產生10個數,填充一個數組,然後用訊息框顯示數組內容,接著計算數組元素的和,將結果也顯示在訊息框中。

1.設計思路

 隨機產生10個數,填充一個數組,利用for迴圈計算數組元素的和,用訊息框顯示數組內容和計算結果。

2.程式流程圖

 

3.原始碼

 

4.結果

 

【Java】數組及課後動手動腦

聯繫我們

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