第六周JAVA作业
1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
import java.util.*; public class jkk { public static void main(String[] args) { int[] a = { 3, 9, 8, 6, 4 }; int temp = 0; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }
2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"
import java.util.*; public class jkk { public static void main(String[] args) { int[] arr = { 34, 22, 35, 67, 45, 66, 12, 33 }; Scanner input = new Scanner(System.in); System.out.print("请输入一个数字:"); int n = input.nextInt(); for (int i = 0; i < arr.length; i++) { if (n == arr[i]) { System.out.println(i); break; } else { System.out.println("not found"); break; } } } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
import java.util.*; public class jkk { public static void main(String[] args) { double[][] a = { { 4.6, 5.3, 7.6, 5.9, 12.0 }, { 1.9, 6.5, 5.9, 86.6, 4.8 }, { 8.85, 41.2, 12.3, 5.3, 1.11 }, { 4.9, 8.9, 7.9, 39.4, 57.8 } }; for (int i = 0; i < a.length; i++) { for (int j = 0; j < 5; j++) { System.out.print(a[i][j] + "\t"); } System.out.print("\n"); } } }
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.
import java.util.*; public class jkk { public static void main(String[] args) { int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; int max = arr[0][0]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } } System.out.println("该二维数组的最大值" + max); } }