第六周作业
1、定义长度位5的整型数组,输入他们的值用冒泡排序后输出。 public class Text1 { 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 - 1; i++) { for (int j = 0; j < a.length - 1; j++) if (a[j + 1] > a[j]) { int x = a[j + 1]; a[j + 1] = a[j]; a[j] = x; } } for (int i = 0; i < a.length; i++) { System.out.println(a[i] + ""); } } }
2、定义数组{34,22,35,67 ,45,66,12,33}输入-个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"。
import java.util.*; public class Text2 { 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 x = input.nextInt(); int y = 0; for (int i = 0; i < a.length; i++) { if (x == a[i]) { System.out.println("存在查找的数其下标为:" + i); y = 1; } } if (y == 0) { System.out.println("not found"); } } }
3、以矩阵的形式输出一-个double型二维数组(长度分别为5、4 ,值自己设定)的值。 public class Sx3 { public static void main(String[] args) { // TODO Auto-generated method stub double[][] a= {{1,2,3,4,5},{6,7,8,9,10}, {11,12,13,14,15},{16,17,18,19,20}}; 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 Sc4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入数组:"); int[][] a = new int[3][4]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { a[i][j] = input.nextInt(); } } int max = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (max < a[i][j]) { max = a[i][j]; } } } System.out.println("最大值为" + max); } }