Java作业6

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涌入累加和。

时间: 2024-12-24 19:53:38

Java作业6的相关文章

关于提高字节流问题暨第四次java作业

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException; public class CopyFile { /** * @param args */ public static void main(String[] args) { try { FileInputStream fis = new FileInputStream ("a.mp3"); FileOutpu

java作业4

(一)  请查看String.equals()方法的实现代码,注意学习其实现方法.(发表到博客作业上) (二)  整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toCharArray()使用说明 Length():获取字串长度 String s1 = "Welcome to java"; System.out.println("s1's length

java作业3

一.构造方法 1.源代码 public class Test{ public static void main(String[] args){ Foo obj1=new Foo(); } } class Foo{ int value; public Foo(int initValue){ value=initValue; } } 2.程序截图 3.结果分析 若构造方法已提供,则系统不再提供默认构造方法. 二.JAVA字段初始化 1.源代码 public class InitializeBlock

java作业1

编辑路径,但由于JAVA故障 JAVA不能正常安装所以在cmd输入javac产生错误不能正常运行出来 所有作业的文件已经输入,只需要在cmd中运行即可,但是JAVA有问题不能实现只能写出过程没有结果图.

JAVA作业02

一,      课堂练习 (一)构造方法 1,源代码 public class Test{ public static void main(String[] args){ Foo obj1=new Foo(); } } class Foo{ int value; public Foo(int initValue){ value=initValue; } } 2,运行结果 3,结果分析 如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法. (二)JAVA字段初始化 1,源代码 pub

JAVA作业 03

动手动脑 一.JAVA的类的对象实例化 1)定义:在面向对象的编程中,通常把用类创建对象的过程称为实例化,其格式为:类名 对象名 = new 类名(参数1,参数2...参数n); 如 Date date=new Date();就是用日期类创建了一个日期的对象,就叫对象的实例化.实例化一个对象 就是为对象开辟内存空间,或者是不用声明,直接使用new 构造函数名(),建立一个临时对象. 2)例子:图1:没有对象实例化 图2:进行了对象实例化: 二.利用线性同余法生成随机数 1)定义: 2)例子: 课

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods

第1次Java作业+140201131+刘思阳

[例3.1]验证如果数据过长则可能出现的问题 1 class DataDemo01{ 2 public static void main(string[] args){ 3 int nun=99999999999999999999999;//定义整型变量,超出长度范围,错误 4 } 5 } 程序运行结果(报错) [例3.2]将整型的最大值加1和加2 1 class DataDemo02{ 2 public static void main(String[] args){ 3 int max =

java 作业-梭哈--待完成

作业:定义一个类,该类用于封装一桌梭哈游戏,这个类应该包含桌上剩下的牌的信息,并包含5个玩家的状态的信息,他们各自的位置,游戏状态(正在游戏或已放弃),手上已有的牌等信息.如果有可能,这个类还应该实现发牌方法,这个方法需要控制从谁开始发牌,不要发牌给放弃的人,并修改桌上剩下的牌: ???? 梭哈游戏规则: 游戏开始时,每名玩家会获发一张底牌,此牌为暗牌:当派发第二张牌后,便由牌面大者决定下注额,其他人有权选择"跟注"."加注"或"放弃".当五张牌