第六周作业
1.定义长度为5的整型数组,输入他们的值,用冒泡排序后输出
package number6; import java.util.Scanner; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[] num = new int[5]; Scanner input = new Scanner(System.in); for (int i = 0; i < num.length; i++) { System.out.print("请输入第" + (i + 1) + "个数:"); num[i] = input.nextInt(); } for (int i = 0; i < num.length - 1; i++) { for (int j = 0; j < num.length - 1 - i; j++) { if (num[j] > num[j + 1]) { int temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp; } } } for (int i = 0; i < num.length; i++) { System.out.print(num[i] + " "); } } }
2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"
package number6; import java.util.Scanner; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int [] a = {34,22,35,67,45,66,12,33}; Scanner input = new Scanner(System.in); System.out.println("请输入要查找的数字:"); int b = input.nextInt(); for (int i = 0; i < a.length; i++) { if(b == a[i]) { System.out.println("数字" + b + "的下标是:" + i); return; } } System.out.println("not found"); return; } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值
package number6; import java.util.Scanner; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub double[][] a = {{1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}, {5, 6, 7, 8}}; 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] + " "); } } } }
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值
package number6; import java.util.Scanner; public class cvb { public static void main(String[] args) { // TODO Auto-generated method stub int[][] a = {{1, 1, 1, 1}, {24, 9, 6, 30}, {9, 5, 1, 12}}; int max = a[0][0]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (a[i][j] > max) { max = a[i][j]; } } } System.out.println("最大值为" + max); } }