JAVA课堂练习-动手动脑--数组

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

源代码:

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.以下代码的输出结果是什么?为什么会有这个结果?

  int[]  a  =  {5,  7  ,  20};

       int[]  b  =  new  int[4];

       System.out.println("b数组的长度为:"  +  b.length);

       b  =  a;

       System.out.println("b数组的长度为:"  +  b.length);

输出结果

   b数组的长度为:4

   b数组的长度为:3

原因

   代码 b = a;表示的是将数组a的地址赋值给b,所以数组b就相当于是数组a,数组a有3个元素,所以数组b的长度为3;

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

   源代码

import java.util.Scanner;

 

public class zhuanhuan {

static String wei []={"","十","百","千","万"};

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

static String output="";

public static void main(String[] args){

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

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int c=weishu(n);

shu(n,c);

}

public static int weishu(int x){   //判断数的位数

int a=10,n=1;

double b=x/a;

if(b<1)

return n;

else

{n=2;

 while(b>1)

 {

 a=a*10;

 n++;

 b=x/a;

 }

 return n;

}

}

  public static void shu(int x,int y){   //将数的各个位分离存到数组中

int a=1;

int j=0;

int c[]=new int[y];

for(int i=1;i<y;i++)

{

a=a*10;

}

while(a>0)

{

int b=x/a;

c[j]=b;

j++;

x=x%a;

a=a/10;

}

int n=0;

for(int i=0;i<y;i++)    //将数字转换成汉字

{

int k=c[i];

if(k==0)

{

n++;

if(n>1)

{

n--;

continue;

}

output+=han[k];

continue;

}

output+=han[k]+wei[y - i -1];

}

System.out.println(output);

}

}

结果截图

 

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

要求将设计思路、程序流程图、源程序代码、结果截图、编程总结等

 

设计思路:

1.首先定义一个长度为实的数组;

2.循环十次产生十个随机数并存到数组中;

3.定义一个String类型变量;

4.将数组中的元素都连到String类型的变量中;

5.最后用消息框输出;

源代码

public class RandomInt {

public static void main(String[] args){

int sum=0;

String output="";

int a []=new int[10];

Random r=new Random();

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

{

a[i]=(int) r.nextInt();

output+=a[i]+" ";

}

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

sum+=a[i];

JOptionPane.showMessageDialog(null, "数组元素为:"+output);

JOptionPane.showMessageDialog(null, "数组元素的和是:"+sum);

}

}

流程图

 

截图

 

 

 

 

原文地址:https://www.cnblogs.com/Zhanghaonihao/p/6036744.html