class Wardrobe { private ArrayList<clothes> clothesList = new ArrayList<>(); //添加的函数 public void addClothes(clothes Clothes) { //提供了添加方法 //该方法创建了衣物 clothesList.add(Clothes); } //删除的函数 public void removeClothes(int index) { //提供了删除方法 clothesList.remove(index); } //查看的函数 public void listClothes() { //添加默认衣服 Scanner scanner=new Scanner(System.in); clothes newClothes = new clothes("默认衣物", "黑色", 38); addClothes(newClothes); //提供了查看方法 //通过进行for循环来查看多件衣服衣服 //for循环的实现 for (int i = 0; i < clothesList.size(); i++) { //获取衣服的属性 clothes clothes =clothesList.get(i); //输出衣服 System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() ); } }只能录入默认衣物修改代码
时间: 2024-02-23 16:03:09 浏览: 27
要修改代码,你可以在添加衣物的方法 `addClothes` 中添加一个判断,如果传入的衣物对象的属性值都为默认值,那么就不将该衣物对象添加到衣物列表中,而是输出一条提示信息。修改后的代码如下:
```
class Wardrobe {
private ArrayList<clothes> clothesList = new ArrayList<>();
//添加的函数
public void addClothes(clothes Clothes) {
//提供了添加方法
//该方法创建了衣物
if (Clothes.getName().equals("默认衣物") && Clothes.getColor().equals("黑色") && Clothes.getSize() == 38) {
System.out.println("不能录入默认衣物!");
} else {
clothesList.add(Clothes);
}
}
//删除的函数
public void removeClothes(int index) {
//提供了删除方法
clothesList.remove(index);
}
//查看的函数
public void listClothes() {
//添加默认衣服
clothes newClothes = new clothes("默认衣物", "黑色", 38);
addClothes(newClothes);
//提供了查看方法
//通过进行for循环来查看多件衣服衣服
//for循环的实现
for (int i = 0; i < clothesList.size(); i++) {
//获取衣服的属性
clothes clothes = clothesList.get(i);
//输出衣服
System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() );
}
}
}
```
这样就可以避免重复录入默认衣物了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)