请老师检查~
设置了一些简单的防报错条件和异常处理
package com.imooc.homework;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public void houseCenterMenu() {
System.out.println("*******************************************");
System.out.println(" 房源中心 ");
System.out.println(" 1--添加房源");
System.out.println(" 2--查看具体房源信息");
System.out.println(" 3--区间查看房源信息");
System.out.println(" 0--退出房源中心");
System.out.println("*******************************************");
}
public void displayAddOneHouse() {
System.out.println("添加房源");
System.out.println("输入房源编号:");
System.out.println("编号从0开始并为连续数值,如0,1,2,3");
}
public static void main(String[] args) {
Test t1=new Test();
t1.houseCenterMenu();
@SuppressWarnings("resource")
Scanner console=new Scanner(System.in);
List<PropertyListings> propertyListings=new ArrayList<>();
while(true) {
System.out.println("请输入对应数字进行操作:");
try {
int n=console.nextInt();
if(n<0 ||n>3) {
System.out.println("请输入菜单旁边的序号!");
continue;
}
switch(n) {
case 0:
System.out.println("结束程序");
System.exit(1);
break;
case 1:
t1.displayAddOneHouse();
int houseId=console.nextInt();
System.out.println("请输入房源名称:");
String houseName=console.next();
System.out.println("请输入房源地址:");
String houseAddress=console.next();
System.out.println("请输入房源户型:");
String typesOfHouses=console.next();
PropertyListings pl=new PropertyListings(houseId, houseName, houseAddress, typesOfHouses);
propertyListings.add(pl);
System.out.println("*******************************************");
break;
case 2:
System.out.println("房源编号 房源名称");
for(int i=0; i<propertyListings.size(); i++) {
System.out.print(" "+propertyListings.get(i).getHouseId()+" ");
System.out.println(propertyListings.get(i).getHouseName());
}
System.out.println("请输入需要查看的具体房源编号:");
int m=console.nextInt();
System.out.println(propertyListings.get(m));
break;
case 3:
System.out.println("请输入要查看房源信息的第一个编号:");
int one=console.nextInt();
System.out.println("请输入要查看房源信息的第二个编号:");
int two=console.nextInt();
for(int i=one; i<two; i++) {
System.out.println(propertyListings.get(i));
System.out.println("---------------------------");
}
t1.displayAddOneHouse();
break;
}
}catch(Exception e) {
System.out.println("您的输入有错误");
console.next();
}
}
}
}
package com.imooc.homework;
public class PropertyListings {
private int houseId;
private String houseName;
private String houseAddress;
private String typesOfHouses;
public PropertyListings(int houseId, String houseName, String houseAddress, String typesOfHouses) {
super();
this.houseId = houseId;
this.houseName = houseName;
this.houseAddress = houseAddress;
this.typesOfHouses = typesOfHouses;
}
public int getHouseId() {
return houseId;
}
public void setHouseId(int houseId) {
this.houseId = houseId;
}
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getHouseAddress() {
return houseAddress;
}
public void setHouseAddress(String houseAddress) {
this.houseAddress = houseAddress;
}
public String getTypesOfHouses() {
return typesOfHouses;
}
public void setTypesOfHouses(String typesOfHouses) {
this.typesOfHouses = typesOfHouses;
}
@Override
public String toString() {
return "房源编号:"+this.getHouseId()+
"\n房源名称:"+this.getHouseName()+
"\n房源地址:"+this.getHouseAddress()+
"\n房屋类型:"+this.getTypesOfHouses();
}
}
正在回答 回答被采纳积分+1
Test类的数组越界做一点修改
package com.imooc.homework;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public void houseCenterMenu() {
System.out.println("*******************************************");
System.out.println(" 房源中心 ");
System.out.println(" 1--添加房源");
System.out.println(" 2--查看具体房源信息");
System.out.println(" 3--区间查看房源信息");
System.out.println(" 0--退出房源中心");
System.out.println("*******************************************");
}
public void displayAddOneHouse() {
System.out.println("添加房源");
System.out.println("输入房源编号:");
System.out.println("编号从0开始并为连续数值,如0,1,2,3");
}
public static void main(String[] args) {
Test t1=new Test();
t1.houseCenterMenu();
@SuppressWarnings("resource")
Scanner console=new Scanner(System.in);
List<PropertyListings> propertyListings=new ArrayList<>();
while(true) {
System.out.println("请输入对应数字进行操作:");
try {
int n=console.nextInt();
if(n<0 ||n>3) {
System.out.println("请输入菜单旁边的序号!");
continue;
}
switch(n) {
case 0:
System.out.println("结束程序");
System.exit(1);
break;
case 1:
t1.displayAddOneHouse();
int houseId=console.nextInt();
System.out.println("请输入房源名称:");
String houseName=console.next();
System.out.println("请输入房源地址:");
String houseAddress=console.next();
System.out.println("请输入房源户型:");
String typesOfHouses=console.next();
PropertyListings pl=new PropertyListings(houseId, houseName, houseAddress, typesOfHouses);
propertyListings.add(pl);
System.out.println("*******************************************");
break;
case 2:
System.out.println("房源编号 房源名称");
for(int i=0; i<propertyListings.size(); i++) {
System.out.print(" "+propertyListings.get(i).getHouseId()+" ");
System.out.println(propertyListings.get(i).getHouseName());
}
System.out.println("请输入需要查看的具体房源编号:");
int m=console.nextInt();
System.out.println(propertyListings.get(m));
break;
case 3:
System.out.println("请输入要查看房源信息的第一个编号:");
int one=console.nextInt();
System.out.println("请输入要查看房源信息的第二个编号:");
int two=console.nextInt();
if(one<0||one>propertyListings.size()) {
one=0;
}
if(two<0||two>propertyListings.size()) {
two=propertyListings.size();
}
for(int i=one; i<two; i++) {
System.out.println(propertyListings.get(i));
System.out.println("---------------------------");
}
t1.displayAddOneHouse();
break;
}
}catch(Exception e) {
System.out.println("您的输入有错误");
console.next();
}
}
}
}
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星