8-1 PassTwoDimensionalArray.java from page 253
用方法得到二维数组,用另一个方法求二维数组和
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
//get the array
int[][] arr = getArr();
int sum = 0;
sum = sum(arr);
//diaplay the sum
System.out.println("the sum of the array is " + sum);
}
//**求数组的方法**//
public static int[][] getArr() {
//做一个扫描
Scanner input = new Scanner(System.in);
//初始化数组
int[][] arr = new int[3][4];
//用循环做数组
//行优先
for (int row = 0; row < arr.length; row++) {
for (int column = 0; column < arr[row].length; column++)
arr[row][column] = input.nextInt();
}
return arr;
}
//** 求和的方法 **//
public static int sum(int[][] arr) {
//初始化总和
int sum = 0;
for (int row = 0;row < arr.length; row++) {
for (int column = 0; column < arr[row].length; column++)
sum += arr[row][column];
}
return sum;
}
}
8-2 GradeExam.java from page 255 (示例学习)
多选题测验评分,创建二维数组存储学生的答案,创建一维数组存储正确答案,利用for循环判断正确个数,并输出。
由于每行代表一个学生的答案,因此在每行结束后,初始化正确个数的标记count=0。
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
//存储学生的答案
char[][] answers = {
{'A','B','A','C','C','D','E','E','A','D'},
{'D','B','A','B','C','A','E','E','A','D'},
{'E','D','D','A','C','B','E','E','A','D'},
{'C','B','A','E','D','C','E','E','A','D'}};
//存储正确答案
char[] keys = {'D','B','D','C','C','D','A','E','A','D'};
//对比
for (int row = 0; row < answers.length; row++) {
int count = 0; //每一个学生初始化一次
for (int col = 0; col < answers[row].length; col++)
//记录正确个数
if (answers[row][col] == keys[col])
count++;
//打印结果
System.out.println("the correct number is :" + count);
}
}
}
8-3 FindNearestPoints.java from page 256(示例学习)
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
//点的个数
int numOfPoints;
System.out.println("plz input the number of the points :");
Scanner input = new Scanner(System.in);
numOfPoints = input.nextInt();
//得到数组内容
double[][] points= new double[numOfPoints][2];
System.out.println("enter the contents of array:");
for (int row = 0; row < numOfPoints; row++){
points[row][0] = input.nextDouble();
points[row][1] = input.nextDouble();
}
//初始化最短距离
int p1 = 0;
int p2 = 1;
double dis;
double shortDis = distance(points[p1][0],points[p1][1],points[p2][0],points[p2][1]);
//对比
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) { //要写数组的长度,而不是numberOfPoints
dis = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
if (shortDis > dis) {
p1 = i;
p2 = j;
shortDis = dis;
} //要加上括号,不然识别不到j
}
}
//输出结果
System.out.println("the nearest points is " + points[p1][0]+","+points[p1][1]+" "+points[p2][0]+","+points[p2][1]);
}
//** 计算距离的方法**//
public static double distance(double x,double y,double m,double n) {
return Math.sqrt((m-x)*(m-x)+(n-y)*(n-y)); //是加
}
}
8-4 CheckSSudokuSolution.java from page 258 (示例学习)
检验数组每行、每列及每列每3*3的小方块都不一致,3个方法,方法重载
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
//构建正确解答
int[][] grid = readASolution();
//输出是否正确
System.out.println(isValid(grid)? "valid":"invalid");
}
//**构建数组的方法
public static int[][] readASolution() {
Scanner input = new Scanner(System.in);
System.out.println("enter the Suko solution:");
int[][] a = new int[9][9];
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++)
a[row][col] = input.nextInt();
}
return a;
}
//**判断每行每列输入合理
public static boolean isValid(int[][] a) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++)
if (a[i][j] < 1 || a[i][j] > 9 || !isValid(i, j, a))
return false;
}
return true;
}
//**判断唯一性
public static boolean isValid(int i, int j, int[][] a) {
//每行不重合
for (int col = 0; col < 9; col++) {
if (col != j && a[i][col] == a[i][j])
return false;
}
//每列不重合
for (int row = 0; row < 9; row++) {
if (row != i && a[row][j] == a[i][j])
return false;
}
//每个小方盒不重合
for (int row = (i / 3) * 3 ; row < (i / 3) * 3 + 3; row++) {
for ( int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (!(row == i && col == j) && a[i][j] == a[row][col]) //&&前即DMG律
return false;
}
return true;
}
}
8-5 Weatehr.java from page 262(示例学习)
三维数组的输入,求每行的和并输出
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
//初始化
final int NUMBER_OF_DAYS = 10;
final int NUMBER_OF_HOURS = 24;
double[][][] data = new double[NUMBER_OF_DAYS][NUMBER_OF_HOURS][2]; //三个空都写值
//输入数据
Scanner input = new Scanner(System.in);
System.out.println("enter the day:");
int day = input.nextInt();
System.out.println("enter the hour:");
int hour = input.nextInt();
for (int k = 0; k < NUMBER_OF_DAYS * NUMBER_OF_HOURS ;k++) {
System.out.println("enter the temperature:");
double temperature = input.nextDouble();
System.out.println("enter the humidity:");
double humidity = input.nextDouble();
data[day - 1][hour - 1][0] = temperature;
data[day - 1][hour - 1][1] = humidity; //0 为温度,1 为湿度
}
//求和
double tTotal = 0;
double hTotal = 0;
for (int i = 0; i < NUMBER_OF_DAYS; i++) {
for(int j = 0; j < NUMBER_OF_HOURS; j++) {
tTotal += data[i][j][0];
hTotal += data[i][j][0]; //如果二层循环不加括号会找不到j
}
//打印结果
System.out.println("the Day" + i + "'s avg of temperatur is " + tTotal/NUMBER_OF_DAYS );
System.out.println("the Day" + i + "'s avg of humidity is " + hTotal/NUMBER_OF_DAYS );
}
}
}
命令行的键入繁琐 用java ~ < ~.txt
8-6 GuessBirthdayUsingArray.java from page 263 (示例学习)
简化4-3猜测生日的程序,三维数组打印显示
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int day = 0;
int answer;
//输入3维数组
int[][][] dates =
{....}
//提示用户猜测
Scanner input = new Scanner(System.in);
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("enter 1 if in the set ,otherwise 0");
answer = input.nextInt();
if (answer == 1)
day += dates[i][0][0];
}
//展示答案
System.out.println("your birthday day is " +day);
}
}