m0_54814086 05月09日
java基础问题
请问为什么执行删除房屋步骤时会报错,应该如何修改呢?

import java.util.*;

public class House {
public String name = " ";
public String telephone = " ";
public String adress = " ";
public String sent = " ";
public String state = " ";
public int id = 0;

boolean loop = true;
Scanner input = new Scanner(System.in);
String choice = " ";
String key = " ";
Collection<House> list = new ArrayList<>();

public House(int id, String name, String telephone, String adress, String sent, String state) {
this.id = id;
this.name = name;
this.telephone = telephone;
this.adress = adress;
this.sent = sent;
this.state = state;
}

public House() {

}

public String getName() {
return name;
}

public String getTelephone() {
return telephone;
}

public String getAdress() {
return adress;
}

public String getSent() {
return sent;
}

public String getState() {
return state;
}

public int getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public void setAdress(String adress) {
this.adress = adress;
}

public void setSent(String sent) {
this.sent = sent;
}

public void setState(String state) {
this.state = state;
}

public void setId(int id) {
this.id = id;
}

public void HouseMenu( ) {
do{
System.out.println("-----------------------房屋出租系统---------------------");
System.out.println("\t\t 1 新增房源");
System.out.println("\t\t 2 查找房屋");
System.out.println("\t\t 3 删除房屋");
System.out.println("\t\t 4 修改房屋信息");
System.out.println("\t\t 5 房屋列表");
System.out.println("\t\t 6 退 出");
System.out.print("请选择(1-6)");
key = input.next( );

switch(key){
case"1" : this.addHouse( );
break;
case"2" : this.searchHouse( );
break;
case"3" : this.deleteHouse( );
break;
case"4" : this.setHouse( );
break;
case"5" : this.HouseList( );
break;
case"6" : this.exit( );
break;
default :
System.out.println("输入有误,重新选择。");
}
}while(loop);
}

public void addHouse() {
System.out.println("------------添加房屋--------------");
System.out.print("编号: ");
id = input.nextInt();
System.out.print("姓名: ");
name = input.next();
System.out.print("电话: ");
telephone = input.next();
System.out.print("地址: ");
adress = input.next();
System.out.print("月租: ");
sent = input.next();
System.out.print("状态(未出租/已出租): ");
state = input.next();
House house = new House(id, name , telephone , adress, sent, state);
list.add(house);
System.out.println("-------------添加完成--------------");
}

public void searchHouse() {
System.out.println("--------------查找房屋--------------");
System.out.print("请输入你要查找的id: ");
int n = input.nextInt();
for(House house : list) {
if(house.getId() == n) {
System.out.println(house.getId()+ "\t" + house.getName() + "\t" + house.getTelephone() + "\t"
+ house.getAdress() + "\t" + house.getSent() + "\t" + house.getState());
}
}
}

public void deleteHouse() {
System.out.println("-----------删除房屋------------");
int n;

while(true) {
System.out.print("请选择待删除房屋编号(-1退出): ");
n = input.nextInt();
if(n == -1)
break;
System.out.println("确认是否删除(Y/N): 请小心选择: ");
System.out.print("请输入你的选择(Y/N): ");
choice = input.next();
if("N".equals(choice) || "Y".equals(choice)) {
break;
}
}

if(choice.equals("Y")){
for(House house : list ) {
if(house.getId() == n) {
list.remove(house);
}
}
System.out.println("-----------删除完成-----------");
}
}

public void setHouse() {
System.out.println("------------修改房屋-------------");
int m;
while(true) {
System.out.print("请选择待修改房屋编号(-1退出): ");
m = input.nextInt();
if(m == -1)
break;
for(House house : list) {
if(house.getId() == m) {
System.out.print("姓名(" + house.getName() + ")");
String name = input.next();
house.setName(name);

System.out.print("电话(" + house.getTelephone() + ")");
String telephone = input.next();
house.setTelephone(telephone);

System.out.print("地址(" + house.getAdress() + ")");
String adress = input.next();
house.setAdress(adress);

System.out.print("租金(" + house.getSent() + ")");
String sent = input.next();
house.setSent(sent);

System.out.print("状态(" + house.getState() + ")");
String state = input.next();
house.setState(state);
}
}
System.out.println("------------修改完成------------");
break;
}
}

public void HouseList() {
System.out.println("--------------房屋列表--------------");
System.out.println("编号\t房主\t电话\t地址\t月租\t状态(未出租/已出租)");
for(House house : list) {
System.out.println(house.getId()+ "\t" + house.getName() + "\t" + house.getTelephone() + "\t"
+ house.getAdress() + "\t" + house.getSent() + "\t" + house.getState());
}
System.out.println("--------------房屋列表完成--------------");
}

public void exit() {
while(true) {
System.out.print("请输入你的选择(Y/N)");
choice = input.next();
if("Y".equals(choice) || "N".equals(choice)){
break;
}
}
if(choice.equals("Y")) {
loop = false;
System.out.println("你退出了程序~~");
}
}

public static void main(String args[]) {
House newHouse = new House();
newHouse.HouseMenu();
}
}
...全文
155 点赞 收藏 8
写回复
8 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告