zoukankan      html  css  js  c++  java
  • 数组

    1.数组棋盘

    import java.io.*;

    public class QiPan
    {
    //定义一个二维数组来充当棋盘
    private String[][] board;
    //定义棋盘的大小
    private static int BOARD_SIZE = 15;
    public void initBoard()
    {
    //初始化棋盘数组
    board = new String[BOARD_SIZE][BOARD_SIZE];
    //把每个元素赋为"╋",用于在控制台画出棋盘
    for (int i = 0 ; i < BOARD_SIZE ; i++)
    {
    for ( int j = 0 ; j < BOARD_SIZE ; j++)
    {
    board[i][j] = "╋";
    }
    }
    }
    //在控制台输出棋盘的方法
    public void printBoard()
    {
    //打印每个数组元素
    for (int i = 0 ; i < BOARD_SIZE ; i++)
    {
    for ( int j = 0 ; j < BOARD_SIZE ; j++)
    {
    //打印数组元素后不换行
    System.out.print(board[i][j]);
    }
    //每打印完一行数组元素后输出一个换行符
    System.out.print(" ");
    }
    }
    public static void main(String[] args)throws Exception
    {
    QiPan gb = new QiPan();
    gb.initBoard();
    gb.printBoard();
    //这是用于获取键盘输入的方法
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String inputStr = null;
    System.out.println("请输入您下棋的座标,应以x,y的格式:");
    //br.readLine():每当在键盘上输入一行内容按回车,刚输入的内容将被br读取到。
    while ((inputStr = br.readLine()) != null)
    {
    //将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串
    String[] posStrArr = inputStr.split(",");
    //将2个字符串转换成用户下棋的座标
    int xPos = Integer.parseInt(posStrArr[0]);
    int yPos = Integer.parseInt(posStrArr[1]);
    //把对应的数组元素赋为"●"。
    gb.board[xPos - 1][yPos - 1] = "●";
    /*
    电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。
    还涉及
    1.座标的有效性,只能是数字,不能超出棋盘范围
    2.如果下的棋的点,不能重复下棋。
    3.每次下棋后,需要扫描谁赢了
    */
    gb.printBoard();
    System.out.println("请输入您下棋的座标,应以x,y的格式:");
    }
    }
    }

    2.编写一个程序将整数转化为汉字

    import java.io.*;

    public class Test1 {


    public static void main(String[] args) {
    //数据字典!
    char c[]={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
    //等待输入!
    System.out.print("请输入一个阿拉伯数字: ");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    //得到输入!
    try {
    String input=br.readLine();
    for(int count=0;count<input.length();count++){
    //转成数字
    char temp=input.charAt(count);
    switch (temp){
    case '1':System.out.print(c[1]);break;case '2':System.out.print(c[2]);break;
    case '3':System.out.print(c[3]);break;case '4':System.out.print(c[4]);break;
    case '5':System.out.print(c[5]);break;case '6':System.out.print(c[6]);break;
    case '9':System.out.print(c[9]);break;case '8':System.out.print(c[8]);break;
    case '0':System.out.print(c[0]);break;default:break;
    }

    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    }

    3大数

    前面几讲介绍过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.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.随机数 

    import javax.swing.*;
    public class Sum {
    public static void main(String args[])
    {
    String output= "10个1000以内的随机数为: ";
    int sum=0;
    int a []=new int [10];
    for(int i = 0;i<10;i++)
    {
    a[i]=(int) (Math.random()*1000);
    output += " "+a[i];
    sum += a[i];
    }
    output +=" 十个数的和是:"+sum;
    JOptionPane.showMessageDialog(null,output,"结果",
    JOptionPane.PLAIN_MESSAGE);
    }
    }

    
    

     

    开始-》int=0 sum=0-》i<10如果大于10就输出总合-》a[i]=Math。random()*1000-》sum+=a[i]-》

    输出a[i]-》输出sum-》结束 。。

  • 相关阅读:
    VC编译器遇到问题处理
    C++笔试、面试题总结
    变量自增整理
    当app出现线上奔溃,该如何办?
    Xcode9新变化
    iOS开发~制作同时支持armv7,armv7s,arm64,i386,x86_64的静态库.a
    百度总裁陆奇:人工智能时代,我们想把它变得更简单
    【转】iOS库 .a与.framework区别
    【转】谈谈 iOS 中图片的解压缩
    【转】iOS中流(Stream)的使用
  • 原文地址:https://www.cnblogs.com/shenghuizhang/p/6036741.html
Copyright © 2011-2022 走看看