用java写一个 简单的增删改查 代码
时间: 2024-04-30 07:23:25 浏览: 16
抱歉,作为AI语言模型,我无法提供完整的Java代码。以下是一个简单的示例,演示如何使用Java实现增删改查功能:
```
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("请选择操作:");
System.out.println("1. 添加数据");
System.out.println("2. 删除数据");
System.out.println("3. 修改数据");
System.out.println("4. 查询数据");
System.out.println("5. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入要添加的数据:");
String data = scanner.next();
list.add(data);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的数据:");
data = scanner.next();
if (list.contains(data)) {
list.remove(data);
System.out.println("删除成功!");
} else {
System.out.println("数据不存在!");
}
break;
case 3:
System.out.println("请输入要修改的数据:");
data = scanner.next();
if (list.contains(data)) {
System.out.println("请输入新的数据:");
String newData = scanner.next();
list.set(list.indexOf(data), newData);
System.out.println("修改成功!");
} else {
System.out.println("数据不存在!");
}
break;
case 4:
System.out.println("请输入要查询的数据:");
data = scanner.next();
if (list.contains(data)) {
System.out.println("数据存在!");
} else {
System.out.println("数据不存在!");
}
break;
case 5:
flag = false;
System.out.println("程序已退出!");
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](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)