第六周作业
package com.xuexiao; import java.util.Scanner; public class Text8 { public static void main(String[] args) { // TODO Auto-generated method stub /* 1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.*/ Scanner input=new Scanner(System.in); System.out.println("请输入数组的值"); int[]arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = input.nextInt(); } for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < arr.length-1-i; j++) { if (arr[j]>arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println("冒泡排序后输出为:"); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i] + " "); } } }
package com.xuexiao; import java.util.Scanner; public class Text8 { public static void main(String[] args) { //2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found" int[] arr = {34,22,35,67,45,66,12,33}; int j=0; Scanner sc = new Scanner(System.in); System.out.println("请输入一个数"); int a = sc.nextInt(); for(int i = 0;i<arr.length;i++) { if(a==arr[i]) { j=1; int xiabiao = i; System.out.println("下标为:"+xiabiao); break; } }if(j==0) { System.out.println("no found"); } } }
package com.xuexiao; import java.util.Scanner; public class Text8 { public static void main(String[] args) { //3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。 double[][] arr = {{11,22,33,44},{55,66,77,88},{99,10,11,12},{13,14,15,16},{17,18,19,20},{21,22,23,24}}; for(int i =0;i< arr.length;i++) { System.out.println( ); for(int j=0;j<arr[i].length;j++) { System.out.print(" "); System.out.print(arr[i][j]); } } } }
package com.xuexiao; public class Text8 { public static void main(String[] args) { //4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值. int[][] arr = {{10,21,31,14},{15,26,38,47},{18,24,83,46}}; 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); } }