11.6数组

动手动脑:

阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结。

 

 分析:

按引用传递与按值传送数组类型方法参数的最大关键在于: 使用前者时,如果方法中有代码更改了数组元素的值,实际上是直接修改了原始的数组元素。 使用后者则没有这个问题,方法体中修改的仅是原始数组元素的一个拷贝。

 

 

阅读QiPan.java示例程序了解如何利用二维数组和循环语句绘制五子棋盘。

用一个二维数组充当棋盘,用“+”初始化了棋盘。定义了俩方法一个用来定义棋盘,另一个在控制台输出棋盘。

 

请编写一个程序将一个整数转换为汉字读法字符串。比如“1123”转换为“一千一百二十三”。

//2016/11.6    20153314 严鹏    将数字转换为汉字
package dome;
import java.util.Scanner;
public class change {

    public static void main(String[] args) {
        
        System.out.println("请输入要转换的数字");
        Scanner str=new Scanner(System.in);
        String s=str.next();
        char z[]=s.toCharArray();               //将这一串数字转换为字符数组存储起来
        String c[]={" ","十","百","千","万"};       //建立一个String类型数组存放单位
        for(int i=0;i<s.length();i++)          //将数字转化为汉字
        {
            if(z[i]=='1') z[i]='一';
            if(z[i]=='2') z[i]='二';
            if(z[i]=='3') z[i]='三';
            if(z[i]=='4') z[i]='四';
            if(z[i]=='5') z[i]='五';
            if(z[i]=='6') z[i]='六';
            if(z[i]=='7') z[i]='七';
            if(z[i]=='8') z[i]='八';
            if(z[i]=='9') z[i]='九';
            
        }
        for(int i=0;i<s.length();i++)        
        {
            System.out.print(z[i]+c[s.length()-i-1]);
            
        }

    }

}

 

 

 

package dome;

import java.util.Scanner;
public class change2 {

    public static void main(String[] args) {
        
        System.out.println("请输入要转换的数字");
        Scanner str=new Scanner(System.in);
        String s=str.next();
        char z[]=s.toCharArray();               //将这一串数字转换为字符数组存储起来
        String c[]={"分","角","","元","拾","佰","仟","万"};       //建立一个String类型数组存放单位
        for(int i=0;i<s.length();i++)          //将数字转化为汉字
        {
    
            if(z[i]=='1') z[i]='壹';
            if(z[i]=='2') z[i]='贰';
            if(z[i]=='3') z[i]='叁';
            if(z[i]=='4') z[i]='肆';
            if(z[i]=='5') z[i]='伍';
            if(z[i]=='6') z[i]='陆';
            if(z[i]=='7') z[i]='柒';
            if(z[i]=='8') z[i]='捌';
            if(z[i]=='9') z[i]='玖';
            if(z[i]=='.') z[i]=' ';
            
        }
        for(int i=0;i<s.length();i++)        
        {
            
            System.out.print(z[i]+c[s.length()-i-1]);
            
        }

    }

}

 

 

前面几讲介绍过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。

 

//2016.11.6 20153314 严鹏 大数转换
package dome;

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());

}

}

 

随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中。

 

 

//2016.11.6    20153314    严鹏    输出十个数字并求和
package dome;

import java.util.Random;
import javax.swing.*;
public class Random10{
public static void main(String args[]){
    
    int a[]=new int [10];
    int sum=0;
    Random rand=new Random();
    String output=" ";

    for(int i=0;i<10;i++){

       a[i]=rand.nextInt(100);
       sum+=a[i];
       output+=String.valueOf(a[i])+" ";
     
    }

    JOptionPane.showMessageDialog( null, output,
            "Initializing an Array with a Declaration",
            JOptionPane.INFORMATION_MESSAGE );
            JOptionPane.showMessageDialog(null,sum,"数组中所有元素的和:",
            JOptionPane.INFORMATION_MESSAGE);
   }

}

 

 

 

 

 

转载于:https://www.cnblogs.com/ypbk/p/6035742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值