class Wardrobe { private ArrayList<clothes> clothesList = new ArrayList<>(); //添加的函数 public void addClothes(clothes clothes) { //提供了添加方法 //该方法创建了衣物 clothesList.add(clothes); } public class _面向对象之衣柜管理系统 { //开始进行主函数编写实现代码 public static void main(String[] args) { //调用衣物 Wardrobe wardrobe = new Wardrobe(); //添加默认衣物在衣柜中保证不出现异常 clothes defaultClothes0 = new clothes("默认衣物", "黑色",38); clothes defaultClothes1 = new clothes("默认衣物","白色", 38); clothes defaultClothes2 = new clothes("默认衣物","黄色", 38); clothes defaultClothes3 = new clothes("默认衣物","黑色", 38); clothes defaultClothes4 = new clothes("默认衣物","黑色", 38); clothes defaultClothes5 = new clothes("默认衣物","黑色", 38); clothes defaultClothes6 = new clothes("默认衣物","黑色", 38); clothes defaultClothes7 = new clothes("默认衣物","黑色", 38); clothes defaultClothes8 = new clothes("默认衣物","黑色", 38); clothes defaultClothes9 = new clothes("默认衣物","黑色", 38); clothes defaultClothes10 = new clothes("默认衣物","黑色",38); clothes defaultClothes11 = new clothes("默认衣物","黑色",38); clothes defaultClothes12 = new clothes("默认衣物","黑色",38); clothes defaultClothes13 = new clothes("默认衣物","黑色",38); clothes defaultClothes14 = new clothes("默认衣物","黑色",38); clothes defaultClothes15 = new clothes("默认衣物","黑色",38); clothes defaultClothes16 = new clothes("默认衣物","黑色",38); clothes defaultClothes17 = new clothes("默认衣物","黑色",38); clothes defaultClothes18 = new clothes("默认衣物","黑色",38); clothes defaultClothes19 = new clothes("默认衣物","黑色",38); clothes defaultClothes20 = new clothes("默认衣物","黑色",38); wardrobe.addClothes(defaultClothes1); Scanner scanner = new Scanner(System.in);为什么只能添加默认衣物
时间: 2023-07-05 18:20:04 浏览: 32
这段代码中实际上是可以添加其他衣物的,只是在主函数中只添加了默认衣物而已。你可以在主函数中添加其他衣物,像这样:
```
clothes otherClothes1 = new clothes("其他衣物1","红色",36);
wardrobe.addClothes(otherClothes1);
```
这样就可以添加其他衣物了。只是默认衣物数量比较多,可能是为了方便测试而已。
相关问题
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); //无法录入衣物怎么修改
在你的代码中,已经提供了添加衣物的方法,但是在查看函数中添加默认衣物的代码不应该放在这个位置。你需要将添加默认衣物的代码放在Wardrobe类的构造函数中,这样每次创建一个新的Wardrobe对象时都会自动添加这个默认衣物。修改后的代码如下:
```
class Wardrobe {
private ArrayList<clothes> clothesList = new ArrayList<>();
public Wardrobe() {
// 添加默认衣物
clothes newClothes = new clothes("默认衣物", "黑色", 38);
addClothes(newClothes);
}
// 添加的函数
public void addClothes(clothes Clothes) {
clothesList.add(Clothes);
}
// 删除的函数
public void removeClothes(int index) {
clothesList.remove(index);
}
// 查看的函数
public void listClothes() {
System.out.println("衣柜中的衣物列表:");
for (clothes c : clothesList) {
System.out.println(c);
}
}
}
```
这样,每次创建一个Wardrobe对象时,都会自动添加一个默认衣物,并且可以使用listClothes()方法来查看衣柜中的所有衣物。
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); //提供了查看方法 //通过进行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() ); } }为什么不能录入衣物怎么修改
要添加一个函数或方法来让用户录入衣物的属性,您可以添加一个名为addNewClothes()的函数,该函数接收用户的输入并创建一个新的clothes对象,最后将其添加到clothesList列表中。以下是一个可能的修改版本:
```
import java.util.Scanner;
public 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() {
for (int i = 0; i < clothesList.size(); i++) {
clothes clothes = clothesList.get(i);
System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() + ")");
}
}
public void addNewClothes() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the clothes: ");
String name = scanner.nextLine();
System.out.print("Enter the color of the clothes: ");
String color = scanner.nextLine();
System.out.print("Enter the size of the clothes: ");
String size = scanner.nextLine();
clothes newClothes = new clothes(name, color, size);
addClothes(newClothes);
}
}
```
现在,您可以在需要添加新衣物时调用addNewClothes()函数。