Java的数组的作业11月06日

动手动脑

实验一:了解for循环得到棋盘结构

(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("\n");

              }

       }

    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的格式:");

              }

    }

}

实验结果图:

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

首先根据老师给的课堂例子可以得出:

package mathchange;

import java.util.Scanner;

import javax.swing.plaf.synth.SynthStyle;

//zhanxinwu,November,06,2016

public class MathChange {

 

private String[] hanArr = {"零", "一" , "二" , "三" , "四" ,

 

"五" , "六" , "七" , "八" , "九"};

 

private String[] unitArr = {"十" , "百" , "千","万","十万","百万"};

 

/**

 

* 把一个四位的数字字符串变成汉字字符串

 

* @param numStr 需要被转换的四位的数字字符串

 

* @return 四位的数字字符串被转换成的汉字字符串。

 

*/

private String toHanStr(String numStr)

{

String result = "";

 

int numLen = numStr.length();

//依次遍历数字字符串的每一位数字

for (int i = 0 ; i < numLen ; i++ )

{

//把char型数字转换成的int型数字,因为它们的ASCII码值恰好相差48

//因此把char型数字减去48得到int型数字,例如'4'被转换成4。

int num = numStr.charAt(i) - 48;

//如果不是最后一位数字,而且数字不是零,则需要添加单位(千、百、十)

if ( i != numLen-1 &&num != 0)

{

result += hanArr[num] + unitArr[numLen - 2 - i];

}

//否则不要添加单位

else

{

//上一个数是否为“零”,不为“零”时就添加

if(result.length()>0 && hanArr[num].equals("零") && result.charAt(result.length()-1)=='零')

continue;

result += hanArr[num];

}

}

//只有个位数,直接返回

if(result.length()==1)

return result;

int index=result.length()-1;

while(result.charAt(index)=='零'){

index--;

}

 

if(index!=result.length()-1)

return result.substring(0,index+1);

else {

return result;

}

}

public static void main(String[] args)

{

 

MathChange nr = new MathChange ();

System.out.println("请输入一个想要转化的数字");

System.out.println("只支持整数(0~百万)");

//测试把一个四位的数字字符串变成汉字字符串、

Scanner scanner=new Scanner(System.in);

String m=scanner.nextLine();

System.out.println(nr.toHanStr(m));

}

}

运行结果图:

实验三:JDK所提供的BigInteger

(2)阅读BigInteger类源码,弄清楚它是使用什么算法实现加减乘除四种运算的?

(3)通过互联网查找大数运算的相关资料,给你的大数类添加乘、除、求阶乘等其它功能。

package shuzuDaShu;

//zhanxinwu,November,06,2016

import java.util.Scanner;

 

public class Dashu {

 

         public static int[]add(int []a,int []b){

            int m=0;//位数

            int[]c=new int[a.length];

            for(int i=a.length-1;i>=0;i--) {

                       c[i]=a[i]+b[i]+m;

                       if(c[i]<10)

                           m=0;

                       else

                       {

                           c[i]=c[i]-10;

                           m=1;

                       }

                   }

                   return c;

          }

        public static int []jian(int []a,int[]b,int w){

           int digit=0;

           int[]c=new int[a.length];

           for(int i=a.length-1;i>=0;i--)

           {

                 if(w<=0)

                  {

                     c[i]=b[i]-a[i]-digit;

                       if(c[i]>=0)

                       {  digit=0;

                       }

                        else

                          {

                               c[i]=c[i]+10;

                               digit=1;

                          }

                       }

                           else

                           {

                               c[i]=a[i]-b[i]-digit;

                               if(c[i]>=0)

                               {digit=0;}

                               else

                               {

                                   c[i]=c[i]+10;

                                   digit=1;

                               }

                        }

                    }

                       return c;

                   }

 public static void main(String[]args){

              int a[]=new int[100];

              int b[]=new int[100];

              int m=0;

              int n=0;

              int s=0;

              int t=0;int w=0;

              Scanner in=new Scanner(System.in);

              System.out.println("请输入第一个大数:");

              String K=in.next();

              System.out.println("请输入第二个大数:");

              String H=in.next();

              m=a.length-K.length();

              n=b.length-H.length();

              if(K.length()>H.length())

                {

                    w=1;

                 }

                else if (K.length()<H.length())

                 {

                     w=-1;

                  }

                       else

                           {w = K.compareTo(H);}

                       for (int i = 0; i < K.length(); i++)

                       {

                           a[m++] = K.charAt(i) - 48;

                       }

                       for (int j = 0; j < H.length(); j++)

                       {

                           b[n++] = H.charAt(j) - 48;

                       }

                           int[] c = Dashu.add(a, b);

                           for (int k = 0; k < c.length; k++)

                           {

                               if (c[k] > 0)

                               {

                               s = k;

                               break;

                               }

                           }

                           System.out.print("大数相加的结果为:");

                           for (int i = s; i < c.length; i++) {

                           System.out.print(c[i]);

                           }

                           System.out.println();

                           int[] d = Dashu.jian(a, b, w);

                           for (int k = 0; k < d.length; k++)

                           {

                               if (d[k] > 0)

                               {

                               t = k;

                               break;

                               }

                           }

                           System.out.print("大数相减的结果为:");

                           if (w < 0)

                           System.out.print("-");

                           for (int i = t; i < d.length; i++)

                           {

                               System.out.print(d[i]);

                           }

                           System.out.println();

                           System.out.println();

                           System.out.println();

                           System.out.println();

               }

}

实验四:

(1) 设计思想:该程序应该建立数组,然后调用随机数赋值,然后通过消息框进行求和输出等等。

(2) 实验流程图:

(3)源代码

package shuzurRandom;

//zhanxinwu,November,06,2016

import javax.swing.JOptionPane;

public class ShuZuRandom {

 

  public static void main(String[] args) {

 

  // TODO 自动生成的方法存根

  int s=0;

  String result="";

 

  int a[]=new int[10];//创建一个数组

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

 

  a[i]=(int)(Math.random()*1000+1);//生成1~1000的随机数

  s+=a[i];

  result+=a[i]+" ";

  }

 

  JOptionPane.showMessageDialog(null, result+"总和为:"+s);

  }    

}

(4)实验结果运行图:

(5) 误差分析:

该实验比其他几个来说相对简单,所以难点就是在给数组赋随机数值上。

posted @ 2016-11-06 19:35  Janson鑫  阅读(289)  评论(0编辑  收藏  举报