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