码迷,mamicode.com
首页 > 编程语言 > 详细

第七讲 数组动手动脑和课后作业

时间:2016-11-07 02:37:39      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:rpc   ndt   PFS   tun   amr   sky   wip   scp   mcc   

【动手动脑1】

数据作为方法参数示例代码

int[] arr= new int[ 24 ];

    ……

modifyArray( arr );

阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照。

// PassArray.java

// Passing arrays and individual array elements to methods

 

public class PassArray {

      

       public static void main(String[] args) {

              int a[] = { 1, 2, 3, 4, 5 };

              String output = "The values of the original array are:\n";

 

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

                     output += "   " + a[i];

 

              output += "\n\nEffects of passing array " + "element call-by-value:\n"

                            + "a[3] before modifyElement: " + a[3];

 

              modifyElement(a[3]);

 

              output += "\na[3] after modifyElement: " + a[3];

 

              output += "\n Effects of passing entire array by reference";

 

              modifyArray(a); // array a passed call-by-reference

 

              output += "\n\nThe values of the modified array are:\n";

 

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

                     output += "   " + a[i];

             

              System.out.println(output);

       }

 

       public static void modifyArray(int b[]) {

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

                     b[j] *= 2;

       }

 

       public static void modifyElement(int e) {

              e *= 2;

       }

}

实验结果:

 技术分享

按引用传递与按值传送数组类型方法参数的最大关键在于:

使用前者时,如果方法中有代码更改了数组元素的值,实际上是直接修改了原始的数组元素。

使用后者则没有这个问题,方法体中修改的仅是原始数组元素的一个拷贝。

 

【动手动脑2】

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

 技术分享

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

              }

    }

}

实验结果:

技术分享 

 

【动手动脑3】

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

程序:NumberChange1.java

//数字转换(设计型)。

//ZhaoXuan Li,November 5th,2016.

 

import javax.swing.JOptionPane;

 

public class NumberChange1

{

       public static void main(String[] args)

       {

              String number=JOptionPane.showInputDialog("Enter change number:");

              if(number.equals("0")) JOptionPane.showMessageDialog(null,number+"汉字化为零","Changed",JOptionPane.PLAIN_MESSAGE);

              else change(number);

       }

       public static void change(String number)

       {

              boolean flag=true,flag1;

              String[] shi=new String[]{"","十","百","千","万","十","百","千","亿","十","百","千"};

              String output="";

             

              int i,j,number1;

 

              for(i=0;i<number.length();i++)

              {

                     number1=Integer.parseInt(String.valueOf(number.charAt(i)));

                     flag1=true;

                     for(j=i;j<number.length();j++)

                     {

                            if(number.charAt(j)!=‘0‘)

                            {

                                   flag1=false;

                                   break;

                            }

                     }

                     if(flag1) flag=false;

                     if(flag||number1!=0) output+=changenumber(number1);

                     if(number1==0) flag=false;

                     if(number1!=0||i==(number.length()-1))

                     {

                            output+=shi[number.length()-i-1];

                            flag=true;

                     }

                     else if(i==number.length()-5||i==number.length()-9)

                     {

                            output+=shi[number.length()-i-1];

                     }

              }

             

              JOptionPane.showMessageDialog(null,number+"汉字化为"+xiugai(output),"Changed",JOptionPane.PLAIN_MESSAGE);

       }

             

       public static String changenumber(int i)

       {

              String[] shu=new String[]{"零","一","二","三","四","五","六","七","八","九"};

              return shu[i];

       }

      

       public static String xiugai(String output)

       {

              output=output.replaceAll("零亿","亿零");

              output=output.replaceAll("零万","万零");

              output=output.replaceAll("亿万","亿");

              return output;

       }

}

实验结果:

技术分享 技术分享

技术分享技术分享

技术分享技术分享

 

【动手动脑4】

更进一步,能否将数字表示的金额改为“汉字表达? 比如将“¥123.52”转换为“壹佰贰拾叁元伍角贰分”。

程序:NumberChange.java

//数字转换(设计型)。

//ZhaoXuan Li,November 5th,2016.

 

import javax.swing.JOptionPane;

 

public class NumberChange

{

       public static void main(String[] args)

       {

              String money=JOptionPane.showInputDialog("Enter change money:");

              if(money.equals("0")) JOptionPane.showMessageDialog(null,money+"汉字化为零元","Changed",JOptionPane.PLAIN_MESSAGE);

              else change(money);

       }

       public static void change(String money)

       {

              boolean flag=true,flag1;

              String[] shi=new String[]{"元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"};

              String[] xiao=new String[]{"角","分","厘"};

              String output="";

             

              int k,i,j,number;

              k=money.indexOf(".");

              if(k==-1)

              {

                     for(i=0;i<money.length();i++)

                     {

                            number=Integer.parseInt(String.valueOf(money.charAt(i)));

                            flag1=true;

                            for(j=i;j<money.length();j++)

                            {

                                   if(money.charAt(j)!=‘0‘)

                                   {

                                          flag1=false;

                                          break;

                                   }

                            }

                            if(flag1) flag=false;

                            if(flag||number!=0) output+=changenumber(number);

                            if(number==0) flag=false;

                            if(number!=0||i==(money.length()-1))

                            {

                                   output+=shi[money.length()-i-1];

                                   flag=true;

                            }

                            else if(i==money.length()-5||i==money.length()-9)

                            {

                                   output+=shi[money.length()-i-1];

                            }

                     }

              }

              else

              {

                     for(i=0;i<k;i++)

                     {

                            number=Integer.parseInt(String.valueOf(money.charAt(i)));

                            flag1=true;

                            for(j=i;j<k;j++)

                            {

                                   if(money.charAt(j)!=‘0‘)

                                   {

                                          flag1=false;

                                          break;

                                   }

                            }

                            if(flag1) flag=false;

                            if(flag||number!=0) output+=changenumber(number);

                            if(number==0) flag=false;

                            if(number!=0||i==(k-1))

                            {

                                   output+=shi[k-i-1];

                                   flag=true;

                            }

                            else if(i==k-5||i==k-9)

                            {

                                   output+=shi[k-i-1];

                            }

                     }

                    

                     for(i=k+1;i<money.length();i++)

                     {

                            number=Integer.parseInt(String.valueOf(money.charAt(i)));

                            flag1=true;

                            for(j=i;j<money.length();j++)

                            {

                                   if(money.charAt(j)!=‘0‘)

                                   {

                                          flag1=false;

                                          break;

                                   }

                            }

                            if(flag1) flag=false;

                            if(flag||number!=0) output+=changenumber(number);

                            if(number==0) flag=false;

                            if(number!=0)

                            {

                                   output+=xiao[i-k-1];

                                   flag=true;

                            }

                     }

              }

             

              JOptionPane.showMessageDialog(null,money+"汉字化为"+xiugai(output),"Changed",JOptionPane.PLAIN_MESSAGE);

       }

             

       public static String changenumber(int i)

       {

              String[] shu=new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

              return shu[i];

       }

      

       public static String xiugai(String output)

       {

              output=output.replaceAll("零亿","亿零");

              output=output.replaceAll("零万","万零");

              output=output.replaceAll("亿万","亿");

              return output;

       }

}

实验结果:

 

技术分享技术分享

技术分享技术分享

技术分享技术分享

 

【动手动脑5】

前面几讲介绍过JDK所提供的BigInteger能完成大数计算,如果不用它,直接使用数组表达大数,你能实现相同的功能吗?

要求:

(1)用你的大数类实现加和减两个功能

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

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

程序:BigNumber.java

//大数计算。

//ZhaoXuan Li,November 5th,2016.

 

import java.util.Scanner;

 

public class BigNumber

{

       private char[] a=new char[100];

       private    int b;

      

       BigNumber()

       {

              int i;

              b=0;

              for(i=0;i<99;i++)

              {

                     a[i]=‘0‘;

              }

       }

      

       public static BigNumber Add(BigNumber x,BigNumber y)

       {

              BigNumber z=new BigNumber();

              int i,l3;

              l3=x.b<y.b?x.b:y.b;

              for(i=0;i<l3;i++)

              {

                     z.a[i]=(char)(x.a[i]+y.a[i]+z.a[i]-96);

                     while(z.a[i]>=58)

                     {

                            z.a[i]-=10;

                            z.a[i+1]+=1;

                     }

              }

              if(x.b<y.b)

              {

                     for(i=l3;i<y.b;i++)

                     {

                            z.a[i]+=(y.a[i]-48);

                            while(z.a[i]>=58)

                            {

                                   z.a[i]-=10;

                                   z.a[i+1]+=1;

                            }

                     }

                     while(z.a[y.b]>=58)

                     {

                            z.a[y.b]-=10;

                            z.a[y.b+1]+=1;

                     }

              }

              else

              {

                     for(i=l3;i<x.b;i++)

                     {

                            z.a[i]+=(y.a[i]-48);

                            while(z.a[i]>=58)

                            {

                                   z.a[i]-=10;

                                   z.a[i+1]+=1;

                            }

                     }

                     while(z.a[x.b]>=58)

                     {

                            z.a[x.b]-=10;

                            z.a[x.b+1]+=1;

                     }

              }

              return z;

       }

      

       public static BigNumber Sub(BigNumber x,BigNumber y)

       {

              BigNumber z=new BigNumber();

              int i;

              for(i=0;i<y.b;i++)

              {

                     z.a[i]=(char)(x.a[i]-y.a[i]-z.a[i]+96);

                     while(z.a[i]<48)

                     {

                            z.a[i]+=10;

                            z.a[i+1]-=1;

                     }

              }

              for(i=y.b;i<x.b;i++)

              {

                     z.a[i]+=(x.a[i]-48);

                     while(z.a[i]<48)

                     {

                            z.a[i]+=10;

                            z.a[i+1]-=1;

                     }

              }

              return z;

       }

      

       public String output()

       {

              String output="";

              int i,l1=0,flag=0;

              for(i=98;i>=0;i--)

              {

                     if(a[i]!=‘0‘)

                     {

                            l1=i;

                            flag=1;

                            break;

                     }

              }

              if(flag==0) output+="0";

              else

              {

                     for(i=l1;i>=0;i--)

                     {

                            output+=a[i];

                     }

              }

             

              return output;

       }

      

       public void input()

       {

              int i;

              String d;

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

              Scanner scanner=new Scanner(System.in);

              d=scanner.nextLine();

 

              b=d.length();

              for(i=0;i<b;i++)

              {

                     a[i]=d.charAt(b-1-i);

              }

       }

      

    public static void main(String[] args)

    {

           BigNumber x=new BigNumber();

           BigNumber y=new BigNumber();

           BigNumber q=new BigNumber();

           BigNumber w=new BigNumber();

           System.out.println("加法:");

           x.input();

           y.input();

           System.out.println(x.output()+"+"+y.output()+"="+Add(x,y).output());

           System.out.println("减法(前一项大于后一项):");

           q.input();

           w.input();

           System.out.println(q.output()+"-"+w.output()+"="+Sub(q,w).output());

    }

}

实验结果:

技术分享 

 

【课后作业】

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

要求将设计思路、程序流程图、源程序代码、结果截图、编程总结等发表到博客园,并备份到课堂派。

1、设计思路:这个程序共分为部分:1、建立一个长度为10的int数组。2、利用for循环和(int)(Math.random()*100+1)函数进行填充10个随机数,同时进行求和和赋值给output输出。3、借助for循环给output添加加法格式,如1+2+3=6。4、利用对话框格式输出output的最终内容。

2、程序流程图:

 技术分享

3、源程序代码:Random.java

//随机数产生、输出和求和。

//ZhaoXuan Li,November 5th,2016.

 

import javax.swing.JOptionPane;

 

public class Random

{

  public static void main(String[] args)

  {

         int s=0,i;

         String output="数组各项为:";

         int a[]=new int[10];

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

         {

                a[i]=(int)(Math.random()*100+1);

                s+=a[i];

                output=output+a[i]+" ";

         }

         output+="\n";

         for(i=0;i<9;i++)

         {

                output=output+a[i]+"+";

         }

         output=output+a[9]+"="+s;

        

        JOptionPane.showMessageDialog(null,output,"Results",JOptionPane.PLAIN_MESSAGE );

  }   

}

4、结果截图:

技术分享 

5、编程总结:1、int[] a=new int[10];创建一个长度为10的int数组方法。2、随机数的产生int a=(int)(Math.random()*n+1)产生一个1-n的随机数。3、利用String类型的字符串和对话框输出可以进行简单的排版,具体的排版还需要利用区域和区域内容。

第七讲 数组动手动脑和课后作业

标签:rpc   ndt   PFS   tun   amr   sky   wip   scp   mcc   

原文地址:http://www.cnblogs.com/lizhaoxuan/p/6036877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!