java课堂动手动脑

实验任务一:阅读并运行示例PassArray.java.

1)源代码

package demo;

//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示例程序了解如何利用二维数组和循环语句绘制五子棋盘。

源代码:

package demo;

 

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

}

    }

}

 

1)实验结果截图:

 

                      

实验任务三:请编写一个程序将一个整数转换为汉字读法字符串。比如“1123”转换为一千一百二十三。 更进一步,能否将数字表示的金额改为汉字表达? 比如将123.52”转换为壹佰贰拾叁元伍角贰分

1)程序设计思想:

2)源代码:

package demo;

import java.util.Scanner;

public class ZhuanHuan {

       private String[] a={"","","","","","","","","",""};

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

       private String[] c={"","",""};

       

       private String Change(String str)

       {

        String result="";

        int numLen=str.length();

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

        {

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

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

        {

        result+=a[num]+b[numLen-2-i];

        }

        else

        {

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

        continue;

        result+=a[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[])

       {

        ZhuanHuan nr=new ZhuanHuan();

        Scanner in=new Scanner(System.in);

        System.out.print("输入整数:");

        String x=in.next();

        System.out.print("转换后为:"+nr.Change(x));

       }

}

进一步,金额的源代码:

package demo;

import java.util.Scanner;

public class ZhuanHuan {

       private String[] a={"","","","","","","","","",""};

       private String[] b={"","","",""};

       private String[] c={"","",""};

       

       private String Change(String str)

       {

        String result="";

        int p=0;

        int numLen=str.length();

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

        {

        if(str.charAt(i)=='.')

        {

        p=i;

        break;

        }

        }

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

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

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

        {

        result+=a[num]+b[p-2-i];

        }

        else

        {

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

        continue;

        result+=a[num];

        }

        }

        result+=c[0];

        for(int i=p+1;i<numLen;i++)

        {

        int num1=str.charAt(i)-48;

        if(num1!=0)

        {

        result+=a[num1]+c[numLen-p-2];

        }

        else

        result+=a[num1];

        p--;

        }

        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[])

       {

        ZhuanHuan nr=new ZhuanHuan();

        Scanner in=new Scanner(System.in);

        System.out.print("输入金额:");

        String x=in.next();

        System.out.print("转换后为:"+nr.Change(x));

       }

}

4)实验结果截图:

 

 

实验任务四:大数

(1) 源代码:

package demo;

 

import java.util.Scanner;

 

public class BigNum{

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

            int wei=0;

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

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

            {

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

                if(c[i]<10)

                    wei=0;

                else

                {

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

                    wei=1;

                }

            }

            return c;

    }

        public static int []sub(int []a,int[]b,int w)

        {

            int wei=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]-wei;

                    if(c[i]>=0)

                    {   wei=0;}

                    else

                    {

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

                        wei=1;

                    }

                }

                    else

                    {

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

                        if(c[i]>=0)

                        {wei=0;}

                        else

                        {

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

                            wei=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 u=0;

                Scanner in=new Scanner(System.in);

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

                String aa=in.next();

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

                String bb=in.next();

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

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

                if(aa.length()>bb.length())

                {

                    u=1;

                }

                else if (aa.length()<bb.length())

                {

                    u=-1;

                }

                else

                    {u = aa.compareTo(bb);}

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

                {

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

                }

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

                {

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

                }

                    int[] c = BigNum.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 = BigNum.sub(a, b, u);

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

                    {

                        if (d[k] > 0)

                        {

                        t = k;

                        break;

                        }

                    }

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

                    if (u < 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();

 

                    }

}

(2) 程序截图

 

 

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

(1) 程序设计思路:先定义一个数组,用random算法产生随机数,利用for循环将随机数导入数组中,遍历计算数组元素的和,然后输出即可。

(2) 源代码:

package demo;

import javax.swing.JOptionPane;

public class ArrayAdd {

       public static void main(String args[]){

        int []a=new int[10];

        int sum=0;

        String str="10个随机数为:"+"\n";

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

        {

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

        a[i]=m;

        str+=a[i]+"\n";

        sum+=a[i];

        }

        str+="随机数的和为:"+sum;

        JOptionPane.showMessageDialog(null,str,"结果",JOptionPane.INFORMATION_MESSAGE);

       }

}

程序截图:

 

 

5)编程总结:学会了利用循环将随机数赋给数组,然后计算数组元素的和,遍历并输出,把问题从大化小,由繁化简,逐步分析,完成项目。

 

posted @ 2016-11-06 21:14  Joker明哥  阅读(228)  评论(0编辑  收藏  举报