【Java】数组及课后动手动脑

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.结果截图

原文地址:https://www.cnblogs.com/qkmn/p/6034170.html