java实验数组与方法答案_JAVA 数组作业——动手动脑以及课后实验性问题

JAVA课后作业——动手动脑

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

1、源代码

// 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、运行结果

The values of the original array are:

1   2   3   4   5

Effects of passing array element call-by-value:

a[3] before modifyElement: 4

a[3] after modifyElement: 4

Effects of passing entire array by reference

The values of the modified array are:

2   4   6   8   10

3、小结

首先输出的是a[i]的原始数组,再输出引用和按值传递(使用前者时,如果方法中有代码更改了数组元素的值,实际上是直接修改了原始的数组元素。使用后者则没有这个问题,方法体中修改的仅是原始数组元素的一个拷贝。)后的值,最后输出加倍的值。

二、阅读QiPan.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("\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的格式:");

}

}

}

2、解释:见实验作业

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

1)参考答案内容总结

1、源程序

public class Num2Rmb

{

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)

{

Num2Rmb nr = new Num2Rmb();

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

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

System.out.println(nr.toHanStr("0"));

System.out.println(nr.toHanStr("1"));

System.out.println(nr.toHanStr("10"));

System.out.println(nr.toHanStr("15"));

System.out.println(nr.toHanStr("110"));

System.out.println(nr.toHanStr("123"));

System.out.println(nr.toHanStr("105"));

System.out.println(nr.toHanStr("1000"));

System.out.println(nr.toHanStr("1100"));

System.out.println(nr.toHanStr("1110"));

System.out.println(nr.toHanStr("1005"));

System.out.println(nr.toHanStr("1105"));

System.out.println(nr.toHanStr("1111"));

System.out.println(nr.toHanStr("10000"));

System.out.println(nr.toHanStr("10001"));

System.out.println(nr.toHanStr("10011"));

System.out.println(nr.toHanStr("10111"));

System.out.println(nr.toHanStr("11111"));

System.out.println(nr.toHanStr("11000"));

System.out.println(nr.toHanStr("11100"));

System.out.println(nr.toHanStr("11110"));

System.out.println(nr.toHanStr("101110"));

System.out.println(nr.toHanStr("1001110"));

}

}

2)更进一步尝试

1、在尝试……(小数的处理的问题)

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

要求:

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

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

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

见程序设计作业(查找结果http://swiftlet.net/archives/296)

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

1、源程序

//mao2015.11.2

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

import javax.swing.*;

public class BlankArray {

public static void main(String[] args) {

// TODO Auto-generated method stub

String output = "";

int n[] = {25,6,8,58,98,45,7,4,32,25};

int a=0;

output += "数组为: ";

for(int i=0;i

output += n[i] + "\n";

output += "数组和为:";

for(int i=0;i

a += n[i];

output += a;

JTextArea outputArea = new JTextArea( 11, 10 );

outputArea.setText( output );

JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array with a Declaration",JOptionPane.INFORMATION_MESSAGE );//以对话框的形式出现

System.exit( 0 );

}

}

2、结果

ba5bf0db493a49c2f1f6b480f0fda283.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值