Java第八次作业
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)
public class test1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={10,20,30,40,50}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
public class test2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char a[]={'n','e','u','s','o','f','t','e','d','u','c','a','t','i','o','n'}; int b=0; for (int i = 0; i < a.length; i++) { b=i+1; } char c[]=new char[b]; for (int i = 0; i < c.length; i++) { c[i]=a[i]; System.out.print(c[i]+" "); } } }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
public class test3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={1,6,2,3,9,4,5,7,8}; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length-1; j++) { int tew=a[j]; if(a[j]<a[j+1]){ a[j]=a[j+1]; a[j+1]=tew; } } } for (int j = 0; j < a.length; j++) { System.out.println(a[j]); } } }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
public class test4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double a[][]=new double[5][4]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j]=i+j; } } System.out.println("矩阵是"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问)
public class test5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={18,25,7,36,13,2,89,63}; int max=a[0]; for (int i = 0; i < a.length; i++) { if(max<a[i]){ max=a[i]; } } System.out.println("最大值是"+max); for (int i = 0; i < a.length; i++) { if(max==a[i]){ System.out.println("其下标是"+i); } } } }
6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
package stu8; public class test6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int s[] ={1,3,5,7,9,11,13,15,17,19}; int t; for (int i = 0,j=s.length-1;i<j; i++,j--) { t=s[i]; s[i]=s[j]; s[j]=t; } for (int i = 0; i < s.length; i++) { System.out.print(s[i]+" "); } System.out.println(); } }
7、将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
package stu8; public class test7 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={2,4,6,8,2,5,4,2}; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if(a[i]==a[j]){ if(i!=j){ a[j]=0; } } } System.out.println(a[i]); } } }
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
package stu8; public class test8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]={-10,2,3,246,-100,0,5}; int sum=0; int max=0; int min=0; double j=0; for (int i = 0; i < a.length; i++) { sum+=a[i]; if(max<a[i]){ max=a[i]; } if(min>a[i]){ min=a[i]; } j=i+1; } System.out.println("平均值是:"+sum/j); System.out.println("最大值是:"+max); System.out.println("最小值是:"+min); } }
9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
package stu8; public class test9 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[20]; int x=1; int y=0; for (int i = 1; i < a.length; i++) { a[0]=1; a[i]=x+y; y=x; x=a[i]; } for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } } }
10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
package stu8; import java.util.Random; public class test91 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[]=new int[10]; Random r= new Random(); for (int i = 0; i < a.length; i++) { int b=r.nextInt(100); a[i]=b; } for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } System.out.println(); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length-1; j++) { int tew=a[j]; if(a[j]<a[j+1]){ a[j]=a[j+1]; a[j+1]=tew; } } } System.out.println("排序后输出:"); for (int j = 0; j < a.length; j++) { System.out.print(a[j]+" "); } } }