新手求助String二维数组
假定一宾馆有三层,第一层有4个房间,第二层3个房间,第三层5个房间,某一天客人入住情况,打印当前的入住情况,二维数组实现。
第三层:
301 302:tom 303 304305rose
第二层:
201lily 202 203jack
第一层:
101lucy 102mike 103bob 104
利用Java数据类型中的String二维数组实现。
这里面的数据时固定的嘛 不是,房客是每天在变的,表格里是当天的 因为你只是打印当前楼层的住户,而每层楼的房间数不一样,所以我按最大的房间数来定义数组,代码如下:
//规定楼层,三层,因每层人数不一样,故第二个按最多人数定
String[][] str = new String;
str = "A";
str = "B";
str = "C";
str = "D";
str = "E";
str = "F";
str = "G";
str = "H";
str = "I";
str = "J";
str = "K";
str = "L";
for (int i = 2; i >= 0; i--) {
if (i == 0) {
System.out.println("第一层");
for (int j = 0; j < 4; j++) {
System.out.print(str + " ");
}
System.out.println();
}
if (i == 1) {
System.out.println("第二层");
for (int j = 0; j < 3; j++) {
System.out.print(str + " ");
}
System.out.println();
}
if (i == 2) {
System.out.println("第三层");
for (int j = 0; j < 5; j++) {
System.out.print(str + " ");
}
System.out.println();
}
}
运行结果如下:
不知道你想要的是不是这种的 public class Demo{
public static void main(String[] args) {
//可以看做一栋楼是一个数组,每层也是一个数组
//一栋楼
String[][] floor = new String[];
//第一层
String[] firstLayer = new String;
//第二层
String[] secondLayer = new String;
//第三层
String[] thirdLayer = new String;
floor = firstLayer;
floor = secondLayer;
floor = thirdLayer;
//当天一层入住情况
firstLayer = "lucy";
firstLayer = "mike";
firstLayer = "bob ";
firstLayer = null;
//当天二层入住情况
secondLayer = "lily";
secondLayer = null;
secondLayer = "jack";
//当天三层入住情况
thirdLayer = null;
thirdLayer = "tom ";
thirdLayer = null;
thirdLayer = null;
thirdLayer = "rose";
//打印当天入住情况
for (int i = 0; i < floor.length; i++) {
System.out.print("第" + (i + 1) + "层:");
for (int j = 0; j < floor.length; j++) {
String name = floor;
String str = (i + 1) + "0" + (j + 1) + " " + (name == null ? "empty" : name) + "\t";
System.out.print(str);
}
System.out.println();
}
}
}
输出如下:
第1层:101 lucy 102 mike 103 bob 104 empty
第2层:201 lily 202 empty 203 jack
第3层:301 empty 302 tom 303 empty 304 empty 305 rose
页:
[1]