//1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出. package homework4; import java.util.Scanner; public class Code1 { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = new int[5]; Scanner input = new Scanner(System.in); System.out.print("请输入5位数数组:"); for (int i = 0; i < 5; i++) { a[i] = input.nextInt(); } 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]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } System.out.print("排序输出:"); 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" package homework4; import java.util.Scanner; public class Code2 { 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.print("输入一个数:"); int i = input.nextInt(); int b = 0; for (int j = 0; j < a.length; j ++) { if (i == a[j]){ System.out.println("存在该数,该数下标为:" + j); b = 1; } } if(b != 1){ System.out.println("not found"); } } }
package homework4; public class Code3 { public static void main(String[] args) { // TODO Auto-generated method stub double[][] arr = {{2000,2001,2002,2003},{2004,2005,2006,2007},{2008,2009,2010,2011},{2012,2013,2014,2015},{2016,2017,2018,2019},{2020,2021,2022,2023}}; 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]); } } } }
//4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值. package homework4; public class Code4 { public static void main(String[] args) { // TODO Auto-generated method stub int[][] a= {{11, 22, 33, 44},{55, 66, 77, 88},{99, 111, 222, 333}}; int max = a[0][0]; for(int i = 0; i < 3; i ++){ for(int j = 0; j < 4; j ++){ if(max < a[i][j]){ max = a[i][j]; } } } System.out.println("output max=" + max); } }