作业咋无法上传包.复制代码给老师看下有何bug和有何需要改进的地方,感觉每次输入都要判断是否为数字有点重复

来源:5-2 项目作业

weixin_慕村4552609

2021-11-01 01:14:07

package com.imooc.house;

public class House {
	private int houseId;
	private String houseName;
	private String houseAddr;

	public int getHouseId() {
		return houseId;
	}

	public void setHouseId(int houseId) {
		this.houseId = houseId;
	}

	private String houseType;

	public House(int houseId, String houseName, String houseAddr, String houseType) {
		super();
		this.houseId = houseId;
		this.houseName = houseName;
		this.houseAddr = houseAddr;
		this.houseType = houseType;
	}

	public String getHouseName() {
		return houseName;
	}

	public void setHouseName(String houseName) {
		this.houseName = houseName;
	}

	public String getHouseAddr() {
		return houseAddr;
	}

	public void setHouseAddr(String houseAddr) {
		this.houseAddr = houseAddr;
	}

	public String getHouseType() {
		return houseType;
	}

	public void setHouseType(String houseType) {
		this.houseType = houseType;
	}

	@Override
	public String toString() {
		return "房源编号:"+houseId+"\n房源名称:" + houseName + "\n房源地址:" + houseAddr + "\n房源类型:" + houseType;
	}

}
package com.imooc.house;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class HouseTest {
	private Scanner sc = new Scanner(System.in);
	private int inputNum;

	public void menu() {
		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("**************************************************");
		System.out.println("请输入0-3的数字进行操作:");
	}

	/**
	 * 输入选择菜单序号
	 */
	public void selectMenu() {
		int num;
		while (true) {
			try {
				num = sc.nextInt();
				if (num >= 0 && num <= 3) {
					this.inputNum = num;
				} else {
					System.out.println("您的输入有误!请输入0-3的数字!");
					continue;
				}
				break;
			} catch (InputMismatchException e) {
				System.out.println("您的输入有误!请输入0-3的数字!");
				sc.next();// 跳过之前错误的输入
				continue;
			}
		}
	}

	/**
	 * 添加房源
	 * 
	 * @param list
	 */
	public void addHouse(List<House> list) {
		int houseId;
		System.out.println("请输入房源编号:");
		System.out.println(
				"编号从" + list.size() + "开始,并为连续数值,如:" + list.size() + "," + (list.size() + 1) + "," + (list.size() + 2));
		while (true) {
			if (sc.hasNextInt()) {
				houseId = sc.nextInt();
				if (houseId != list.size()) {
					System.out.println("您的输入有误!请从编号" + list.size() + "开始输入!");
					continue;
				}
				break;
			} else {
				System.out.println("您的输入有误!请输入数字!请从编号" + list.size() + "开始输入!");
				sc.next();// 跳过之前错误的输入
				continue;
			}
		}
		System.out.println("请输入房源名称:");
		String houseName = sc.next();
		System.out.println("请输入房源地址:");
		String houseAddr = sc.next();
		System.out.println("请输入房源户型:");
		String houseType = sc.next();
		House house = new House(houseId, houseName, houseAddr, houseType);
		list.add(house);
	}

	public void viewHouseInfo(List<House> list) {
		if (list.isEmpty()) {
			System.out.println("房源信息为空,请先添加房源!");
		} else {
			System.out.println("全部房源信息");
			for (House house : list) {
				System.out.println("房源编号      房源名称");
				System.out.println(house.getHouseId() + "                 " + house.getHouseName());
			}
			System.out.println("请输入要查看的具体房源编号:");
			int houseId;
			while (true) {
				if (sc.hasNextInt()) {
					houseId = sc.nextInt();
					if (houseId >= list.size() || houseId < 0) {
						System.out.println("您的输入有误!请输入0-" + (list.size() - 1) + "的编号!");
						continue;
					}
					break;
				} else {
					System.out.println("您的输入有误!请输入数字!请输入0-" + (list.size() - 1) + "的编号!");
					sc.next();// 跳过之前错误的输入
					continue;
				}
			}
			System.out.println(list.get(houseId).toString());
		}
	}

	public void viewPartHouseInfo(List<House> list) {
		if (list.isEmpty()) {
			System.out.println("房源信息为空,请先添加房源!");
		} else {
			System.out.println("全部房源信息");
			for (House house : list) {
				System.out.println("房源编号      房源名称");
				System.out.println(house.getHouseId() + "                 " + house.getHouseName());
			}

			// 这个写法也可以,不过有点别扭,但是报错不用重新输入startId
//			int tempId = 0, startId = 0, endId = 0;
//			for (int i = 1; i < 3; i++) {
//				System.out.println("请输入要查看的具体房源信息的第" + i + "个编号:");
//				while (true) {
//					if (sc.hasNextInt()) {
//						tempId = sc.nextInt();
//						if (tempId > list.size() || tempId < 0) {
//							System.out.println("您的输入有误!请输入0-" + (list.size() - 1) + "的编号!");
//							continue;
//						}
//						if (i == 1) {
//							startId = tempId;
//						} else {
//							endId = tempId;
//						}
//						break;
//					} else {
//						System.out.println("您的输入有误!请输入数字!请输入0-" + (list.size() - 1) + "的编号!");
//						sc.next();// 跳过之前错误的输入
//						continue;
//					}
//				}
//			}
			int startId = 0, endId = 0;
			while (true) {
				try {
					System.out.println("请输入要查看的具体房源信息的第1个编号:");
					startId = sc.nextInt();
					System.out.println("请输入要查看的具体房源信息的第2个编号:");
					endId = sc.nextInt();
					if (endId > list.size() || startId < 0 || startId > endId) {
						System.out.println("您的输入有误!请输入0-" + (list.size() - 1) + "的编号!");
						continue;
					}
					break;
				} catch (InputMismatchException e) {
					System.out.println("您的输入有误!请输入数字!请输入0-" + (list.size() - 1) + "的编号!");
					sc.next();// 跳过之前错误的输入
					continue;
				}
			}

			for (int index = startId; index < endId; index++) {
				System.out.println(list.get(index).toString());
				System.out.println("------------------------------------------");
			}
		}

	}

	public static void main(String[] args) {
		HouseTest ht = new HouseTest();
		List<House> list = new ArrayList<House>();
		while (true) {
			ht.menu();
			ht.selectMenu();
			switch (ht.inputNum) {
			case 0:
				System.exit(0);
			case 1:
				ht.addHouse(list);
				break;
			case 2:
				ht.viewHouseInfo(list);
				break;
			case 3:
				ht.viewPartHouseInfo(list);
				break;

			}
		}

	}

}


写回答

1回答

好帮手慕小蓝

2021-11-01

同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

同学可以将验证用户输入的是否为数字的代码封装成一个方法,每次需要用户输入的时候,调用一次这个方法即可。实现这个功能的代码如下:

//	添加验证用户输入是否为整数的方法
public int validateInput() {
    int num;
    while(true) {
        try {
            num = sc.nextInt();
	}catch (InputMismatchException e) {
	    System.out.println("不能使用非整数数字!请重新输入");
	    sc.next();
	    continue;
	}
	return num;
    }
}

1.当封装了验证方法之后,添加的方法可以优化为如下形式:

public void addHouse(List<House> list) {
    int houseId;
    System.out.println("请输入房源编号:");
    //为了提高性能,建议不要多次调用size方法
    int size = list.size();
    System.out.println("编号从" + size + "开始,并为连续数值,如:" + size 
                        + "," + (size + 1) + "," + (size + 2));
    while (true) {
        //此时可以保证用户输入的是整数
        houseId = validateInput();
	//判断用户输入的整数是否可用
	if(houseId != size) {
        	System.out.println("您的输入有误!请从编号" + list.size() + "开始输入!");
		continue;
	}
	break;
    }
//以下是同学的原代码
//while (true) {
//if (sc.hasNextInt()) {
//houseId = sc.nextInt();
//if (houseId != list.size()) {
//System.out.println("您的输入有误!请从编号" + list.size() + "开始输入!");
//continue;
//}
//break;
//} else {
//System.out.println("您的输入有误!请输入数字!请从编号" + list.size() + "开始输入!");
//sc.next();// 跳过之前错误的输入
//continue;
//}
//}
	System.out.println("请输入房源名称:");
	String houseName = sc.next();
	System.out.println("请输入房源地址:");
	String houseAddr = sc.next();
	System.out.println("请输入房源户型:");
	String houseType = sc.next();
	House house = new House(houseId, houseName, houseAddr, houseType);
	list.add(house);
}

2.当封装了验证方法之后,查看具体房源信息的方法可以优化为如下形式:

public void viewHouseInfo(List<House> list) {
    if (list.isEmpty()) {
        System.out.println("房源信息为空,请先添加房源!");
    } else {
	System.out.println("全部房源信息");
	for (House house : list) {
        	System.out.println("房源编号      房源名称");
		System.out.println(house.getHouseId() + "                 " + house.getHouseName());
	}
	System.out.println("请输入要查看的具体房源编号:");
			
	int houseId;
	//为了提高性能,建议不要多次调用size方法
	int size = list.size();
	while(true) {
        	//此时可以保证用户输入的是整数
		houseId = validateInput();
		//判断用户输入的整数是否可用
		if(houseId >= list.size() || houseId < 0) {
			System.out.println("您的输入有误!请输入0-" + (size - 1) + "的编号!");
			continue;
		}
		break;
	}
//以下为同学的原代码	
//while (true) {
//if (sc.hasNextInt()) {
//houseId = sc.nextInt();
//if (houseId >= list.size() || houseId < 0) {
//System.out.println("您的输入有误!请输入0-" + (list.size() - 1) + "的编号!");
//continue;
//}
//break;
//} else {
//System.out.println("您的输入有误!请输入数字!请输入0-" + (list.size() - 1) + "的编号!");
//sc.next();// 跳过之前错误的输入
//continue;
//}
//}
	System.out.println(list.get(houseId).toString());
    }
}

3.当封装了验证方法之后,区间查看房源信息的方法可以优化为如下形式:

public void viewPartHouseInfo(List<House> list) {
    if (list.isEmpty()) {
        System.out.println("房源信息为空,请先添加房源!");
    } else {
	System.out.println("全部房源信息");
	for (House house : list) {
	System.out.println("房源编号      房源名称");
	System.out.println(house.getHouseId() + "                 " + house.getHouseName());
    }
			
    int startId = 0, endId = 0;
    //为了提高性能,建议不要多次调用size方法
    int size = list.size();
    //处理第一个数字
    while(true) {
        System.out.println("请输入要查看的具体房源信息的第1个编号(可用的编号范围是0-" + (size - 1) + "):");
        //此时可以保证用户输入的是整数
        startId = validateInput();
        //判断第一个数是否可用
	if(startId < 0 || startId >= size) {
	    System.out.println("您的输入有误!请输入0-" + (size - 1) + "的编号!");
	    continue;
	}
	break;
    }
    //处理第二个数字
    while(true) {
        System.out.println("请输入要查看的具体房源信息的第2个编号(可用的编号范围是" + startId + "-" + (size - 1) +"):");
        //此时可以保证用户输入的是整数
        endId = validateInput();
	//判断第二个数必须大于或等于第一个数
	if (endId < startId || endId >= size) {
            System.out.println("您的输入有误!请输入"+ startId +"-" + (size - 1) + "的编号!");
	    continue;
        }
	break;
    }
//以下为同学的原代码
//while (true) {
//try {
//System.out.println("请输入要查看的具体房源信息的第1个编号:");
//startId = sc.nextInt();
//System.out.println("请输入要查看的具体房源信息的第2个编号:");
//endId = sc.nextInt();
//if (endId > list.size() || startId < 0 || startId > endId) {
//System.out.println("您的输入有误!请输入0-" + (list.size() - 1) + "的编号!");
//continue;
//}
//break;
//} catch (InputMismatchException e) {
//System.out.println("您的输入有误!请输入数字!请输入0-" + (list.size() - 1) + "的编号!");
//sc.next();// 跳过之前错误的输入
//continue;
//}
//}
//为了允许用户查询类似3号到3号这样的区间,将index < endId,修改为index <= endId
        for (int index = startId; index <= endId; index++) {
    	System.out.println(list.get(index).toString());
    	System.out.println("------------------------------------------");
        }
    }
}

以上就是对同学代码的所有优化,如果同学还有疑问,可以在问答区继续提问。

祝学习愉快~


0

0 学习 · 16556 问题

查看课程