请老师检查~

请老师检查~

设置了一些简单的防报错条件和异常处理

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

登陆购买课程后可参与讨论,去登陆

1回答
晓舟 提问者 2021-06-06 16:34:51

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();

}

}

}


}


  • 提问者 晓舟 #1

    case 3:中的t1.displayAddOneHouse();修改为t1.houseCenterMenu();


    2021-06-06 17:24:07
  • 同学你好,同学作业整体完成的不错,逻辑也很清晰,但还是有一些小问题需要优化,继续加油。

    1、注意代码书写格式规范;在必要的位置添加注释;

    2、当未添加房源信息时,选择2、3时,可提示用户:没有房源,请添加房源!同学的运行结果如下:

    http://img1.sycdn.imooc.com//climg/60bca1e209f4f5cb03750236.jpghttp://img1.sycdn.imooc.com//climg/60bca2d00936d47803670201.jpg

    修改建议:可先判断集合是否为空,为空提示:没有房源,请添加房源!并进入到下一次循环

    http://img1.sycdn.imooc.com//climg/60bca26009bb564506450160.jpg

    http://img1.sycdn.imooc.com//climg/60bca2ff0960458e07010166.jpg

    3、查看房源信息时,输入非数字/错误编号,建议提示再次重新输入查看的具体房源编号,而不是直接进入下一次循环,同学的运行效果如下:

    http://img1.sycdn.imooc.com//climg/60bca5130955379103510291.jpg

    修改建议:如图

    http://img1.sycdn.imooc.com//climg/60bca8f009bf93f208200689.jpg

    4、区间查看房源信息部分,思路很好,但是不够严谨。建议做如下修改。

    当输入区间编号错误时,有提示信息,并可以再次输入,详细如下:

    http://img1.sycdn.imooc.com//climg/60bcad66091f06fa11690716.jpg

    5、需要注意添加房源时,编号不可重复

    http://img1.sycdn.imooc.com//climg/60bcb04909ac271611170754.jpg

    6、菜单提示建议放到循环内,用作每次提示用户输入。



    祝:学习愉快~~~

    2021-06-06 19:25:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师