Java第八次作业
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]
public class Qjh1 { public static void main(String[] args) { int x[] = new int[] { 10, 20, 30, 40, 50 }; for (int i = 0; i <= x.length; i++) { System.out.println(x[i]); } }
2.将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String a[] = {"china"}; String b[] =new String [a.length]; System.arraycopy(a, 0, b, 0, a.length); for (String i : b) { System.out.println(i); } }
3. 给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
public static void main(String[] args) { // TODO Auto-generated method stub int[] x = { 1, 6, 2, 3, 9, 4, 5, 7, 8}; for (int i = 0; i < x.length - 1; i++) { for (int j = 0; j < x.length - 1 - i; j++) { if (x[j] > x[j + 1]) { int temp = x[j]; x[j] = x[j + 1]; x[j + 1] = temp; } } } for (int i : x) { System.out.println(i); } }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
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; } } for (int i = 0; i < a.length; i++) { System.out.println(" "); for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]+" "); } } }
5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
package dnmd; import java.util.Scanner; public class Qjh1 { 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]; int maxidx=0; for (int i = 0; i < a.length; i++) { if(a[i]>max){ max=a[i]; maxidx=i; } } System.out.println("最大值为"+max+"其下标为"+maxidx); } }
1.将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]
package dnmd; import java.util.Scanner; public class Qjh1 { public static void main(String[] args) { // TODO Auto-generated method stub int x[] = { 1, 9, 8, 4, 6 }; int y = 0; for (int i = 0; i < x.length / 2; i++) { y = x[i]; x[i] = x[x.length - 1 - i]; x[x.length - 1 - i] = y; } System.out.println("逆序存放为:"); for (int i : x) { System.out.print(i); } } }
2、 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问
package dnmd; import java.util.Scanner; public class Qjh1 { public static void main(String[] args) { // TODO Auto-generated method stub int []arr={1,1,18,18,22,22,5,5,9,9}; for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(); System.out.println("重复项清零"); for(int j=0;j<arr.length;j++){ for(int k=j+1;k<arr.length;k++){ if(arr[j]==arr[k]) arr[k]=0; } } for(int j=0;j<arr.length;j++){ System.out.print(arr[j]+" "); } } }
3.给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)
int []a={ -10,2,3,246,-100,0,5}; int max = a[0],min = a[0]; double sum = 0; for (int i = 1; i < a.length; i++) { if (a[i] > max) { max = a[i]; } if (a[i] < min) { min = a[i]; } } for (int i = 0; i < a.length; i++) { sum += a[i]; } System.out.println("最大值为" + max); System.out.println("最小值为" + min); System.out.println("平均值为" + sum / a.length); }
4.使用数组存放裴波那契数列的前20项 ,并输出
package dnmd; import java.util.Scanner; public class Qjh1 { public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[20];a[0]=1; int x=1; int y=1; int z=1; for (int i = 1; i < a.length; i++) { a[i]=z; z=x+y; x=y; y=z; } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
5.生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
package dnmd; import java.util.Random; import java.util.Scanner; public class Qjh1 { public static void main(String[] args) { // TODO Auto-generated method stub Random random = new Random(10); int b[] = new int[10]; for (int i = 0; i < b.length; i++) { b[i] = random.nextInt(200); System.out.print(" "+b[i]); } System.out.println(); for (int i = 0; i < b.length - 1; i++) { for (int j = 0; j < b.length - 1 - i; j++) { if (b[j] > b[j + 1]) { int temp = b[j]; b[j] = b[j + 1]; b[j + 1] = temp; } } } System.out.println(); for (int i = 0; i < b.length; i++) { System.out.print(" " + b[i]); } } }