zoukankan      html  css  js  c++  java
  • 数组及课后动手动脑

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

    结果和截图:


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

    结果和截图:

    三.更进一步,能否将数字表示的金额改为“汉字表达? 比如将“¥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。

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

    结果解截图:

    五:随机数

    1.设计思路

    本实验在for循环中用Math.random()函数生成十个随机数,并存入数组中 。int类型sum用于累加十个随机数和,最后用对话框依次输出即可。

    2.程序流程图

    3.源程序代码

      import javax.swing.*;

    public class Suiji {

          public static void main(String args []){

              int sum=0;int math;

              int a[]=new int[10];

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

                    math= (int)(Math.random()*100);

                    sum+=math;

                    a[i]=math;

              }

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

                     JOptionPane.showMessageDialog(null,"第"+(i+1)+"个数是:"+a[i], null,

                                   JOptionPane.INFORMATION_MESSAGE);

              }

              JOptionPane.showMessageDialog(null,"总和是:"+sum, null,

                                JOptionPane.INFORMATION_MESSAGE);

          }

    }

    4.结果截图

     

    5.编程总结

    本实验关键在于数组的运用和随机数的生成。随机数只生成0-1之间的double类型数字,需要将double类型转化为int整型。并多次使用for()循环。

     

  • 相关阅读:
    django 重建一个表
    近期数据工作的知识点总结(model-dict高级用法)
    搬运django中文网 CentOS7下部署Django项目详细操作步骤(django安装网站有时候打不开,备份用)
    创建ftp免密只读用户(外系统读取csv共享数据)
    某某系统从外部基础库读取数据
    离线安装 django-axes
    django queryset用法总结二
    django queryset用法总结一
    nginx 启动,停止 重启
    安装安全狗失败 ,linux wget的时候不去找目标ip,而是路由到其他ip,原因分析
  • 原文地址:https://www.cnblogs.com/du1269038969/p/6035941.html
Copyright © 2011-2022 走看看