Java中的二维数组:
java中没有严格意义上的二维数组:所谓的二维数组就是数组的数组
技巧:
- 可以把二维数组看成多行多列的矩阵显示问题;行和列从从0开始
- 双重for循环可以解决多行多列矩阵显示问题
package day09;
import java.util.Arrays;
/**
* Java中的二维数组:
* java中没有严格意义上的二维数组:所谓的二维数组就是数组的数组
*
* 技巧:
* 可以把二维数组看成多行多列的矩阵显示问题;行和列从从0开始
* 双重for循环可以解决多行多列矩阵显示问题
*@author DEYOU
*/
public class ArrayDemo {
public static void main(String[] args) {
//定义二维数组 默认为0
int[][] ary=new int[3][4];//第一个[]:几行 第二个[]:几列(每行有几个元素)
//等价于
for (int i = 0; i < ary.length; i++) {
for (int j = 0; j < ary[i].length; j++) {
System.out.print(ary[i][j]+"\t");
}
System.out.println();
}
int [][] ary1={
{1,2,3,4},
{5,6,7,8},
{9,0,11,12}
};
//获取二维数组的长度 格式: 数组名.length;
System.out.println(ary1.length);
//获取二维数组中的数据元素 格式 :数组名[下标]
System.out.println(Arrays.toString(ary1[0]));//打印ary1[0]中的全部元素
System.out.println(ary1[1][1]);//打印输出6
ary1[2][1]=15;//将ary1中的元素0改为15
//遍历二维数组
for (int i = 0; i < ary1.length; i++) {
//ary1[i] 取出来的还是一个数组
//System.out.println(ary1[i].length);//输出的是每行中元素的个数
//遍历ary1[i]数组
for (int j = 0; j < ary1[i].length; j++) {
System.out.print(ary1[i][j]+"\t");
}
System.out.println();
}
}
}
一个简单的酒店订房系统:
package day09;
import java.util.Scanner;
/**
* 酒店订房系统
*
* 作业: 订房时,如果输入房间号在酒店中不存在,要进行提示
* 退房时,如果输入房间号在酒店中不存在,要进行提示
* 订房或退房时,如果操作错了,可以让他继续操作,不直接显示主菜单
*
* @author DEYOU
*/
public class RoomTest {
// 房间号存不存在的状态,默认不存在
static int state;
// 定义酒店的楼层
static int row = 9;
// 定义每层楼的房间数
static int col = 9;
// 定义一个二维数组,用来存放房间的入住状态.没人入住,就显示empty
static String[][] rooms = new String[row][col];
// 存放每个房间对应入住的名字
static String[][] names = new String[row][col];
static Scanner key = new Scanner(System.in);
public static void main(String[] args) {
// 初始化酒店的房间,所有的入住状态都设置为empty
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
rooms[i][j] = "empty";
// System.out.print(rooms[i][j]+"\t");
}
// System.out.println();
}
// 干个循环,让系统可以一直运行
while (true) {
help();
// 接受输入指令
String con = key.next();
if (con.equals("search")) {
// 查询房间的入住状态
searchRoom();
} else if (con.equals("in")) {
// 预定房间
inTheRoom();
} else if (con.equals("out")) {
// 退房
outTheRoom();
} else if (con.equals("exit")) {
System.out.println("客官慢走~~~~~记得常来");
// 退出系统
break;
}
}
}
/**
* 退房
*/
public static void outTheRoom() {
while (true) {
System.out.println("请输入要退出的房间号:");
state = 0;
int num = key.nextInt();
// 判断该房间号是否存在
state = crossTheBorder(num);
// 根据房间号是否存在进行执行代码
if (state == 1) {
int row = num / 1000 - 1;
int col = num % 1000 - 1;
// 判断房间号的居住状态
if (rooms[row][col].equals("empty")) {
System.out.println("房间无人入住,无需退房");
} else {
System.out.println("请输入姓名:");
String name = key.next();
// 根据用户名进行判断房间是否为空
if (names[row][col].equals(name)) {
rooms[row][col] = "empty";// 修改房间入住状态
names[row][col] = null;// 将房间对应的名字清空
System.out.println(name + "退房成功~~");
break;
} else {
System.out.println("房间必须由本人来退");
}
}
} else {
System.out.println("该房间号不存在!请重新输入");
}
}
}
/**
*
* 预定房间
*/
public static void inTheRoom() {
while (true) {
System.out.println("请输入你要预定的房间");
state = 0;
int num = key.nextInt();
// 判断该房间号是否存在
state = crossTheBorder(num);
// 根据房间号是否存在进行执行代码
if (state == 1) {
int row = num / 1000 - 1;
int col = num % 1000 - 1;
// 无人居住
if (rooms[row][col].equals("empty")) {
System.out.println("请输入你的姓名:");
String name = key.next();
// 将房间的入住状态修改成****
rooms[row][col] = "****";
// 保存名字
names[row][col] = name;
System.out.println(name + "成功预定" + num + "号房间~");
break;
} else {
System.out.println("该房间已被预订!");
}
} else {
System.out.println("抱歉!该房间号不存在");
}
}
}
/**
* 查询房间的入住状态
*/
public static void searchRoom() {
for (int i = 0; i < rooms.length; i++) {
// 房间号
for (int j = 0; j < rooms[i].length; j++) {
System.out.print((i + 1) * 1000 + j + 1 + "\t");
}
System.out.println();
// 遍历房间的状态
for (int j = 0; j < rooms[i].length; j++) {
System.out.print(rooms[i][j] + "\t");
}
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
/**
* 判断房间号是否越界
*
* @param sum
* @return
*/
public static int crossTheBorder(int sum) {
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[i].length; j++) {
if (sum == (i + 1) * 1000 + j + 1) {
state = 1;
}
}
}
return state;
}
/**
* 系统帮助方法,提供操作提示
*/
public static void help() {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("~~~欢迎来到 枫林晚酒店~~~");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("~~~请选择操作");
System.out.println("~~~请入 search 查询房间输入状态");
System.out.println("~~~输入in 预定房间");
System.out.println("~~~ 输入out 退房");
System.out.println("~~~输入exit退出系统");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("请输入指令:");
}
}