Java基本功练习八(多维数组[二维、三维、模拟评卷系统、九宫格验证])

来源:互联网 发布:12123 网络请求失败 编辑:程序博客网 时间:2024/04/28 07:39

       学习完一维数组,再进行多维数组的学习会更加容易。多维数组可以解决更为复杂的问题,在用法上与一维数组也有很多类似的地方。下面先从二维数组的基本用法展示开始讲解,然后再进行升级。

       示例一:二维数组的基本操作。

运行效果如图所示:

实现的源代码如下:

package Blog;import java.util.Scanner;public class blogTryProject {//二维数组基本操作public static void main(String[] args) {Scanner input = new Scanner(System.in);int[][] matrix = new int[5][5];System.out.println(matrix.length+" rows and "+matrix[0].length+" columns");//随机产生5*5数组for(int row = 0;row < matrix.length;row++){for(int columns = 0;columns < matrix[row].length;columns++){matrix[row][columns] = (int)(Math.random()*20);System.out.printf("%3d ",matrix[row][columns]);}System.out.println();}//按列求和for(int columns = 0;columns < matrix[0].length;columns++){int total = 0;for(int row = 0;row < matrix.length;row++)total += matrix[row][columns];System.out.println("Sum for column "+columns+" is "+total);}//按行求和for(int row = 0;row < matrix.length;row++){int total = 0;for(int columns = 0;columns < matrix[row].length;columns++)total += matrix[row][columns];System.out.println("Sum for row "+row+" is "+total);}//求最大的一行int maxSum = 0;int indexOfMax = 0;for(int columns = 0;columns < matrix[0].length;columns++){maxSum += matrix[0][columns];}for(int row = 1;row < matrix.length;row++){int maxThisRow = 0;for(int columns = 0;columns < matrix[0].length;columns++)maxThisRow += matrix[row][columns];if(maxThisRow > maxSum){maxSum = maxThisRow;indexOfMax = row;}}System.out.println("Row "+indexOfMax+" has the maximun sum of "+maxSum);//随意打乱数组的元素for(int row = 1;row < matrix.length;row++){for(int columns = 0;columns < matrix[0].length;columns++){int i = (int)(Math.random()*matrix.length);int j = (int)(Math.random()*matrix[i].length);int temp = matrix[row][columns];matrix[row][columns] = matrix[i][j];matrix[i][j] = matrix[row][columns];}}//打印打乱后的数组元素for(int row = 1;row < matrix.length;row++){for(int columns = 0;columns < matrix[0].length;columns++)System.out.printf("%3d",matrix[row][columns]);System.out.println();} }}
       学会了二维数组的基本用法,就要检验一下是否掌握了,以示例二的题目进行检验。

       示例二:显示二维空间所有距离最短的点对。程序要求输入二维点的个数,及每个二维点,然后求出这些二维点相互之间距离最短的所有点对。

运行效果如右所示:实现的源代码如下图所示:

package Blog;import java.util.Scanner;public class blogTryProject {//找出距离最短的所有点对public static void main(String[]args){Scanner input = new Scanner(System.in);System.out.print("Enter the number of points: ");//输入点个数int numberOfPoints = input.nextInt();double[][]points = new double[numberOfPoints][2];//输入每个点System.out.print("Enter "+numberOfPoints+" points: ");for(int i = 0;i <points.length;i++){points[i][0] = input.nextDouble();points[i][1] = input.nextDouble();}int[] p1 = new int[numberOfPoints];//目标点下标数组int[] p2 = new int[numberOfPoints];int ii = 0,jj = 0;double shortestDistance = distance(points[0][0],points[0][1],points[1][0],points[1][1]);//初始化点对距离//找出最短距离值for(int i = 0;i < points.length;i++){for(int j = i+1;j <points.length;j++){double distance = distance(points[i][0],points[i][1],points[j][0],points[j][1]);if(shortestDistance > distance)shortestDistance = distance;}}//记录最短距离点对下标for(int i = 0;i < points.length;i++){for(int j = i+1;j <points.length;j++){double distance = distance(points[i][0],points[i][1],points[j][0],points[j][1]);if(shortestDistance == distance){p1[ii++] = i;p2[jj++] = j;shortestDistance = distance;}}}//打印最短距离点对for(int i = 0;i < ii;i++){System.out.println("The closest two points are "+"("+points[p1[i]][0]+","+points[p1[i]][1]+") and ("+points[p2[i]][0]+","+points[p2[i]][1]+")");}}public static double distance(double x1,double y1,double x2,double y2){return Math.sqrt((x2-x1)*(x2-x1)+(y1-y2)*(y1-y2));}}
       学完了二维数组,再看看三维数组的几个例子。

       示例三:三维数组猜生日的小程序。即问用户五个问题,只需回答Yes或No,就可猜出用户生日的日期。

运行效果如图所示:

实现的源代码如下所示:

package Blog;import java.util.Scanner;public class blogTryProject {//猜生日的小程序public static void main(String[]args){int day = 0;int answer;int[][][] dates = {{{ 1, 3, 5, 7}, { 9,11,13,15}, {17,19,21,23}, {25,27,29,31}},    {{ 2, 3, 6, 7},     {10,11,14,15},     {18,19,22,23},     {26,27,30,31}},    {{ 4, 5, 6, 7}, {12,13,14,15}, {20,21,22,23}, {28,29,30,31}},{{ 8, 9,10,11}, {12,13,14,15}, {24,25,26,27}, {28,29,30,31}},{{16,17,18,19}, {20,21,22,23}, {24,25,26,27}, {28,29,30,31}}};Scanner input =  new Scanner(System.in);System.out.println("例如你的生日是25日!如实回答以下5个问题。");for(int i = 0;i < 5;i++){System.out.println("Is your birthday in Set"+(i+1)+"?");for(int j = 0;j < 4;j++){for(int k = 0;k < 4;k++)System.out.printf("%4d",dates[i][j][k]);System.out.println();}System.out.print("Enter 0 for No and 1 for Yes: ");answer = input.nextInt();if(answer == 1)day += dates[i][0][0];}System.out.println("Your birthday day is "+day);}}
       学完三维数组,相应的给出一个示例作为练习。

       示例四:模拟评卷系统,输出学生的排名。程序要求输入学生人数8和题目数10,并随机产生每个学生的答案;再给出正确答案进行改卷,最后统计每个学生做对题目的数目,进行排名输出。

运行效果如图所示:

实现源代码如下:

package Blog;import java.util.Scanner;import java.util.Arrays;public class blogTryProject {//多选题测验评分,并排名输出public static void main(String[]args){final int NUMBER_OF_STUDENT = 8;//学生数目final int NUMBER_OF_TIMU = 10;//题目数目char[][] student = new char[NUMBER_OF_STUDENT][NUMBER_OF_TIMU];//打印学生答案student = printStudentAnswer(NUMBER_OF_STUDENT,NUMBER_OF_TIMU);char[] key = {'D','B','D','C','C','D','A','E','A','D'};//打印正确答案printCorrectAnswer(NUMBER_OF_TIMU,key);int[] count = new int[NUMBER_OF_STUDENT];//模拟改卷,并评分count = printGaiJuanPingFen(student,key,count);int[] bianHaoOfStudent = new int[NUMBER_OF_STUDENT];//学生排名输出printPaiMing(bianHaoOfStudent,count);}public static char[][] printStudentAnswer(int numS,int numT){System.out.println("学生给出的题目答案如下");System.out.print("                题号:");for(int i = 0;i < numT;i++)System.out.printf("%d ",i);System.out.println();char[][] student = new char[numS][numT];for(int i = 0;i < student.length;i++){System.out.printf("Student %2d ",i);for(int j = 0;j < student[0].length;j++){char c = (char)('A'+Math.random()*('E'-'A'+1));student[i][j] = c;System.out.printf("%2c",student[i][j]);}System.out.println();}return student;}public static void printCorrectAnswer(int numT,char[] key){System.out.print("题目正确答案:");for(int i = 0;i < numT;i++)System.out.printf("%c ",key[i]);System.out.println();}public static int[] printGaiJuanPingFen(char[][] student,char[] key,int[] count){for(int i = 0;i < student.length;i++){for(int j = 0;j < student[0].length;j++){if(student[i][j] == key[j])count[i]++;}System.out.printf("Student %2d 做对了 %2d 道题\n",i,count[i]);}return count;}public static void printPaiMing(int[] bianHaoOfStudent,int[] count){for(int i = 0;i < bianHaoOfStudent.length;i++)bianHaoOfStudent[i] = i;insertSort(count,bianHaoOfStudent);//排名函数for(int i= 0;i < count.length;i++)System.out.println("Student "+bianHaoOfStudent[i]+" 排名第 "+(i+1)+"   (做对"+count[i]+"题)");}public static void insertSort(int[] chars,int[] bianHaoOfStudent){System.out.println("成绩排名如下:");for(int i = 1;i < chars.length;i++){for(int j = 0;j < i;j++){if(chars[i] > chars[j]){int temp = chars[i];chars[i] = chars[j];chars[j] = temp;int temp1 = bianHaoOfStudent[i];bianHaoOfStudent[i] = bianHaoOfStudent[j];bianHaoOfStudent[j] = temp1;}}}}}
       作为练习,童鞋们可以将此题改为任意个学生,任意个题目,手工录入正确答案,然后考虑并列的排名。请童鞋们自行练习!

       示例五:九宫格的一种验证方法。即是验证某一种输入是否满足九宫格的要求,满足显示”Valid solution“,否则显示”Invalid solution“。

运行效果如图所示:

实现的源代码如下所示:

package Blog;import java.util.Scanner;import java.util.Arrays;public class blogTryProject {//九宫格简化版本,验证输入的答案是否正确public static void main(String[]args){Scanner input = new Scanner(System.in);int[][] grid =readASolution();System.out.println(isValid(grid) ? "Valid solution" : "Invalid solution");}public static int[][] readASolution(){Scanner input = new Scanner(System.in);System.out.println("Enter a SuDu puzzle solution:");int [][] grid = new int[9][9];for(int i = 0;i < 9;i++)for(int j = 0;j < 9;j++)grid[i][j] = input.nextInt();return grid;}public static boolean isValid(int[][] grid){//检查每行是否包含1到9for(int i = 0;i < 9;i++)if(!is1To9(grid[i]))return false;//检查每列是否包含1到9for(int j = 0;j < 9;j++){int[] column = new int[9];for(int i = 0;i < 9;i++)column[i] = grid[i][j];if(!is1To9(column))return false;}//for(int i = 0;i < 3;i++){for(int j = 0; j < 3;j++){int k = 0;int[] list = new int[9];for(int row = i*3;row < i*3 + 3;row++)for(int column = j*3;column < j*3 +3;column++)list[k++] = grid[row][column];if(!is1To9(list))return false;}}return true;}public static boolean is1To9(int[] list){int[] temp = new int[list.length];System.arraycopy(list, 0, temp, 0, list.length);java.util.Arrays.sort(temp);for(int i = 0;i < 9;i++)if(temp[i] != i + 1)return false;return true;}}

       总结:多维数组的灵活运用可以解决很多复杂的问题。

0 0