(前八章如果您有需要,直接在评论注说,我发给你)本章所有题目代码都是本人自己写的,全部自己运行成功了,可以直接复制粘贴。 这些重要都附加了英文注释,方便解析使大家明白,如果您在此阅读过程中发现程序代码有任何问题(包括代码的命名,代码逻辑,代码风格更好的建议,希望大家严格指出来,哪段代码没注释没看懂或注释不清晰,诸如此类等等…)都可以向我提出来。以便大家学习交流
Practice 8-1
/**
* 需求:打印用户输入的数组数据中的每列数字之和
* 步骤:略(以后太简单的题目步骤都省略)
* 作者:小小鱼
* 时间:2019 - 6 - 99号
*/
/** Create main method */
public static void main(String args []) {
// Read two dimensional arrays by getMatrix of method
double[][] matrix = getMatrix();
for (int column = 0; column < matrix[0].length; column++) {
System.out.println("Sum of the elements at column " + column
+ " is " + sumColumn(matrix, column));
}
}
/** Return two dimensional arrays */
public static double[][] getMatrix() {
// Create a Scanner
java.util.Scanner input = new java.util.Scanner(System.in);
// Prompt the user to enter a 3-by-4 matrix row by row
System.out.println("Enter a 3-by-4 matrix row by row: ");
// Create a arrays and row'length is 3, column'length is 4 of the matrix
double [][] matrix = new double[3][4];
// Read and store data into arrays through user input
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = input.nextDouble();
}
}
return matrix;
}
/** Return sum of column's numbers on even column */
public static double sumColumn(double[][] m, int columnIndex) {
// Declare a variable and initialize it to zero
double sumColumn = 0;
// add all numbers in even row of array
for (int row = 0; row < m.length; row++) {
sumColumn += m[row][columnIndex];
}
return sumColumn;
}
Practice 8-2
/**
* 需求:求矩阵对角线数字之和
* 分析: 这道题还是比较简单的前面我们做八皇后那道题相信大家已经懂了对角线上数据的处理
* 首先读入矩阵数据(元素),然后在遍历二维数组,将第n行的matrix[n-1][n-1]元素加起来
* 得到一个条主对角线的和.
* 步骤:略(看分析)
* Time: 0: 30 AM
* write code make :( for someone
* Write code make :) for someone
*/
/** Create main method */
public static void main(String[] args) {
// Read two dimensional arrays by getMatrix of method
double[][] matrix = getMatrix();
// Display result
System.out.println("Sum of the elements in the major diagonal is " + sumMajorDiagonal(matrix));
}
/** Return two dimensional arrays */
// 此方法可以直接引用上面的getMatrix(),但是为了让大家清楚这个程序的编译运行,修改复制一下吧
public static double[][] getMatrix() {
// Create a Scanner
java.util.Scanner input = new java.util.Scanner(System.in);
// Prompt the user to enter a 4-by-4 matrix row by row
System.out.println("Enter a 4-by-4 matrix row by row: ");
// Create a arrays and row'length is 4, column'length is 4 of the matrix
double [][] matrix = new double[4][4];
// Read and store data into arrays through user input
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = input.nextDouble();
}
}
return matrix;
}
/** Return sum of numbers on the major diagonal */
public static double sumMajorDiagonal(double[][] m) {
// Declare a variables and initialize it to zero
double SumMajorDiagonal = 0;
// Compute SumMajorDiagonal
for (int row = 0; row < m.length; row++) {
SumMajorDiagonal += m[row][row];
}
// Return SumMajorDiagonal
return SumMajorDiagonal;
}
各位实在不好意思
Practice 8 - 3 略,真不愿往上翻 😦
Practice 8-3 Omitted
Practice 8-4
/**
* 提醒: 在写每一段程序都要搞清楚程序的需求,这里题干都给了我们需求的解释, 所以我们的需求
* 按照题目来写了,再者我们要对题目进行分析,最好用注释写出步骤,这是学每个计算机语
* 言的很好的习惯。
* 需求:计算每个雇员每周工作得小时数
* 分析:这道题看似两颗星,其实我觉得很简单呀,当然这本书我还没遇到难题,但从程度上来说这
* 道题也就值一个星;前面还是一样读入雇员得每天得工时数,然后计算每位雇员工时数这周
* 得总数,然后进行降序排列
*
* 步骤: 1:读入矩阵(数组)
* 2:遍历数组计算每行得元素和
* 3:将每行得元素和储存到一个新建的数组上去
* 4:最后进行数组降序排序(冒泡排序)
* *:写了这么多是不是很清晰呢,写注释写的我想跳过这题;
*/
/** Create main method */
public static void main(String[] args) {
// Store all employees' weekly hours in a two-dimensional array
int[][] matrix = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 7, 3, 4, 1},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}};
int[][] sumHoursArrays = getSumOfPerLine(matrix);
int[][] downArrays = getDownSortArrays(sumHoursArrays);
// Display result
for (int row = 0; row < downArrays.length; row++) {
System.out.println("The employee" + downArrays[row][1] + "'s number of sum of hours are "
+ downArrays[row][0]);
}
}
/** Return getSumOfPerLineArrays*/
public static int[][] getSumOfPerLine(int[][] matrix) {
// Create a two dimensional array
int[][] sumHoursArrays = new int[matrix.length][matrix.length];
// Compute per emplyees's weekly hours and numbering, then Store in new array
for (int row = 0; row < matrix.length; row++) {
int sumHours = 0;
// Compute
for (int col = 0; col < matrix[0].length; col++)
sumHours += matrix[row][col];
// Store sum Of hours
sumHoursArrays[row][0] = sumHours;
// Store employees' numbering
sumHoursArrays[row][1] = row;
}
// Return new array
return sumHoursArrays;
}
/** 这里是要对总小时数据存储的数组进行排序的,但是呢在调用方法排序 然后赋值 是不改变数组内容的,
* 所以我们可以arraycopy复制一个新的数组,利用之前书中提到冒泡排序来进行降序排列,然后返回到
* 主函数去
*/
/** Return down sort hours of method*/
public static int[][] getDownSortArrays(int[][] sumHoursArrays) {
// Create a new array
int[][] downSortHours = new int[sumHoursArrays.length][sumHoursArrays[0].length];
// Copy into new array
System.arraycopy(sumHoursArrays, 0, downSortHours, 0, sumHoursArrays.length);
// Through two-dimensional array, and down sort to elements of per line
for (int row = 0; row < downSortHours.length; row++) {
for (int n = row + 1; n < downSortHours.length - 1; n++) {
if (downSortHours[row][0] > downSortHours[n][0]) { // Use bubble sort
// Two values of sumHours in swap
int temp = downSortHours[row][0];
downSortHours[row][0] = downSortHours[n][0];
downSortHours[n][0] = temp;
// Two values of numbering in swap
int temp1 = downSortHours[row][1];
downSortHours[row][1] = downSortHours[n][1];
downSortHours[n][1] = temp1;
}
}
}
// Return copyArray
return downSortHours;
}
Practice 8-4 Omitted
这道题你们懂得,要略了,我直接写下面第五道题,因为第五题是第四题得变型题,你们要是刚学还是多写一下吧,这道题还是有意义的题
Practice 8 -5
/** 前言:突然感觉要做的事情好多呀,先把这章做完保证三个小时之内,但是要是自己敲就三个小时,还有
* 注释应该的要六个小时,前面三个题目就花了我一个小时 哎这道题其实很简单,我都不想动手了,
* 算了为了伟大初学者觉得我是一个勤奋的人,十分钟解决了
* 注意:本人为了速度,确实题目太多了,没注意优化我的代码,如果你们有好的建议留言跟我说一下,
* 我会有空,保证让大家看到更好的代码环境
* 需求:两个矩阵相乘, 打印题目要求的结果
* 分析:肯定是创建两个输出,把用户需要的数据一一储存在对应的数组,然后用两个数组按照题目给的公
* 式得到第三个矩阵的数据,然后按照要求打印出来(本题稍微难那么一点点点点点点的就是打印部分
* 步骤: 略(看分析,看题目)
* */
/** Create main method */
public static void main(String[] args) {
// Prompt the user to enter elements of matrix_a
System.out.print("Enter matrix1: ");
double[][] matrix1 = getMatrix();
// Prompt the user to enter elements of matrix_b
System.out.print("Enter matrix2: ");
double[][] matrix2 = getMatrix();
// Compute out matrix_c, and store in matrix_3
double[][] matrix3 = multiplyMatrix(matrix1, matrix2);
// Display result
for (int row = 0; row < matrix1.len

本文分享了Java程序设计梁勇第十版第八章的编程练习题解,所有代码均为作者原创并成功运行。代码带有英文注释以帮助理解,鼓励读者指出可能存在的问题或提供改进建议,促进学习交流。

507

被折叠的 条评论
为什么被折叠?



