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() ); } }为什么不能录入衣物怎么修改
时间: 2024-02-23 16:03:20 浏览: 18
要添加一个函数或方法来让用户录入衣物的属性,您可以添加一个名为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()函数。