①4名住院患者,一天中3次测量体温。设计二维数组保存这些数据并在控制台上输出保存的数据; ②使用Scanner类读取控制台上逐次输入的4名患者体温测量数据; ③将患者的体温数据保存进二维数组; ④输入结束后,在控制台上输出所有患者的体温数据。
时间: 2023-05-11 12:03:37 浏览: 127
好的,我来回答你的问题:
①这是一个二维数组的示例代码:
double[][] temperatures = new double[4][3];
temperatures[0][0] = 36.5;
temperatures[0][1] = 36.7;
temperatures[0][2] = 36.8;
temperatures[1][0] = 37.2;
temperatures[1][1] = 37.1;
temperatures[1][2] = 37.3;
temperatures[2][0] = 36.9;
temperatures[2][1] = 36.8;
temperatures[2][2] = 36.7;
temperatures[3][0] = 37.5;
temperatures[3][1] = 37.4;
temperatures[3][2] = 37.6;
for (int i = 0; i < temperatures.length; i++) {
for (int j = 0; j < temperatures[i].length; j++) {
System.out.print(temperatures[i][j] + " ");
}
System.out.println();
}
②这是使用Scanner类读取控制台输入的代码:
Scanner scanner = new Scanner(System.in);
double[][] temperatures = new double[4][3];
for (int i = 0; i < temperatures.length; i++) {
System.out.println("请输入第" + (i + 1) + "名患者的体温数据:");
for (int j = 0; j < temperatures[i].length; j++) {
temperatures[i][j] = scanner.nextDouble();
}
}
③这是将患者体温数据保存进二维数组的代码:
double[][] temperatures = new double[4][3];
// 假设已经从控制台输入了患者体温数据
temperatures[0][0] = 36.5;
temperatures[0][1] = 36.7;
temperatures[0][2] = 36.8;
temperatures[1][0] = 37.2;
temperatures[1][1] = 37.1;
temperatures[1][2] = 37.3;
temperatures[2][0] = 36.9;
temperatures[2][1] = 36.8;
temperatures[2][2] = 36.7;
temperatures[3][0] = 37.5;
temperatures[3][1] = 37.4;
temperatures[3][2] = 37.6;
④这是输出所有患者体温数据的代码:
double[][] temperatures = new double[4][3];
// 假设已经从控制台输入了患者体温数据
temperatures[0][0] = 36.5;
temperatures[0][1] = 36.7;
temperatures[0][2] = 36.8;
temperatures[1][0] = 37.2;
temperatures[1][1] = 37.1;
temperatures[1][2] = 37.3;
temperatures[2][0] = 36.9;
temperatures[2][1] = 36.8;
temperatures[2][2] = 36.7;
temperatures[3][0] = 37.5;
temperatures[3][1] = 37.4;
temperatures[3][2] = 37.6;
for (int i = 0; i < temperatures.length; i++) {
for (int j = 0; j < temperatures[i].length; j++) {
System.out.print(temperatures[i][j] + " ");
}
System.out.println();
}
阅读全文