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()函数。

相关推荐

class clothes { //定义名字name String name; //定义颜色color String color; //定义尺寸size int size; //构造clothes()函数 //定义一个公共权限的clothes函数 public clothes(String name, String color, int size) { //衣物的名字 this.name=name; //衣物的颜色 this.color=color; //衣物的尺寸 this.size=size; } //创建一个类getName() public String getName() { //返回名字 return name; } //创建一个getColor()函数 public String getColor() { //返回颜色 return color; } //创建一个getsize()函数 public int getSize() { //返回尺寸 return size; } } //创建一个wardrobe类包含了arraylist类型的成员变量clotheslist //这个类提供了addclothes,removeclothes,listclothes三个方法用来添加衣服,删除衣服,查看衣服 //在这个类中实现管理系统的功能 //主要的方法 class Wardrobe { ArrayList<clothes> clothesList = new ArrayList<>(); //添加的函数 public Wardrobe() { clothes newClothes=new clothes("默认衣物","黑色",30); addClothes(newClothes); } 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() ); } }为什么不能录入衣物

import java.util.ArrayList;import java.util.Scanner;class Clothes { private String name; private String color; private int size; public Clothes(String name, String color, int size) { this.name = name; this.color = color; this.size = size; } public String getName() { return name; } public String getColor() { return color; } public int getSize() { return size; }}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 class WardrobeManagementSystem { public static void main(String[] args) { Wardrobe wardrobe = new Wardrobe(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加衣物"); System.out.println("2. 删除衣物"); System.out.println("3. 查看衣柜中的衣物"); System.out.println("4. 退出"); int choice = scanner.nextInt(); scanner.nextLine(); switch (choice) { case 1: System.out.println("请输入衣物名称:"); String name = scanner.nextLine(); System.out.println("请输入衣物颜色:"); String color = scanner.nextLine(); System.out.println("请输入衣物尺寸:"); int size = scanner.nextInt(); scanner.nextLine(); Clothes clothes = new Clothes(name, color, size); wardrobe.addClothes(clothes); System.out.println("衣物添加成功!"); break; case 2: System.out.println("请输入要删除的衣物编号:"); int index = scanner.nextInt() - 1; scanner.nextLine(); wardrobe.removeClothes(index); System.out.println("衣物删除成功!"); break; case 3: wardrobe.listClothes(); break; case 4: System.out.println("程序已退出!"); System.exit(0); default: System.out.println("输入有误,请重新输入!"); break; } } }}扩充代码可以查看某一颜色或尺码的所有衣物

最新推荐

recommend-type

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt
recommend-type

廖倩5.14运营款.xlsx

廖倩5.14运营款.xlsx
recommend-type

setuptools-25.0.2-py2.py3-none-any.whl

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

全网第一个宝宝生活记录网站root ca

全网第一个宝宝生活记录网站,帮我写一个网站介绍,网址 https://43.136.21.175/ ,二维码分享和登录,统计记录最近时间,功能很全很实用,手机和电脑都可以访问,手机浏览器可以生成快捷方式到桌面和App一样,点右上角可以打开菜单,里面我做了图表统计可以看每天的喂养次数哪些,我们一直在用,很方便的
recommend-type

yes I can.mp3

yes I can.mp3
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。