实验任务一:阅读并运行示例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: ";
for (int i = 0; i < a.length; i++)
output += " " + a[i];
output += " Effects of passing array " + "element call-by-value: "
+ "a[3] before modifyElement: " + a[3];
modifyElement(a[3]);
output += " a[3] after modifyElement: " + a[3];
output += " Effects of passing entire array by reference";
modifyArray(a); // array a passed call-by-reference
output += " The values of the modified array are: ";
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(" ");
}
}
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)源代码:
import java.util.Scanner;
public class Money
{
private String[] A = {"零" , "一" , "二" , "三" , "四" ,
"五" , "六" , "七" , "八" , "九"};
private String[] B = {"十" , "百" , "千","万","十万","百万"};
private String zhuanhua(String Str)
{
String result = "";
int numL = Str.length();
for (int i = 0 ; i < numL ; i++ )
{
int num = Str.charAt(i) - 48;
if ( i != numL - 1 && num != 0)
{
result += A[num] + B[numL - 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)
{
Money nr = new Money();
Scanner in=new Scanner(System.in);
System.out.println("请输入整数:");
String num= in.next();
System.out.println("转换后为:"+nr.zhuanhua(num));
}
}
金额源代码:
import java.util.Scanner;
public class Money
{
private String[] A = {"零" , "壹" , "贰" , "弎" , "肆" ,
"伍 ", "陆" , "柒" , "捌" , "玖"};
private String[] B = {"拾" , "佰" , "千","万","十万","百万"};
private String zhuanhua(String Str)
{
String result = "";
int numL = Str.length();
for (int i = 0 ; i < numL ; i++ )
{
int num = Str.charAt(i) - 48;
if ( i != numL - 1 && num != 0)
{
result += A[num] + B[numL - 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)
{
Money nr = new Money();
Scanner in=new Scanner(System.in);
System.out.println("请输入金额:");
String num= in.next();
System.out.println("转换后为:"+nr.zhuanhua(num)+"元");
}
}
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 RandomAdd {
public static void main(String[]args){
int []a=new int[10];
int sum=0;
String output = "10个随机数为 ";
for(int i=0;i<a.length;i++)
{
int temp=(int)(Math.random()*100);
a[i]=temp;
output+=" "+a[i]+" ";
sum+=a[i];
}
output+="随机数和为:"+sum;
JOptionPane.showMessageDialog( null,output,"结果",
JOptionPane.INFORMATION_MESSAGE );
}
}
(3) 流程图:
(4)程序截图:
(5)编程总结:学会了利用for循环将随机数赋给数组,以及数组的遍历输出。在遇到问题时 逐步分析,将大化小,注重流程图和思路。