用Java模拟微信投票程序,可以添加姓名,投票,查询,退出,并且将数据记录在本地文件中。
时间: 2023-05-16 13:06:03 浏览: 57
好的,这个问题可以回答。以下是一个简单的Java代码示例,可以模拟微信投票程序:
```java
import java.io.*;
import java.util.*;
public class WeChatVote {
private static final String FILE_NAME = "votes.txt";
private static Map<String, Integer> votes = new HashMap<>();
public static void main(String[] args) {
loadVotes();
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();
switch (choice) {
case 1:
addName(scanner);
break;
case 2:
vote(scanner);
break;
case 3:
query(scanner);
break;
case 4:
saveVotes();
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入");
}
}
}
private static void addName(Scanner scanner) {
System.out.println("请输入姓名:");
String name = scanner.next();
if (votes.containsKey(name)) {
System.out.println("该姓名已存在");
} else {
votes.put(name, 0);
System.out.println("添加成功");
}
}
private static void vote(Scanner scanner) {
System.out.println("请输入姓名:");
String name = scanner.next();
if (!votes.containsKey(name)) {
System.out.println("该姓名不存在");
} else {
int count = votes.get(name);
votes.put(name, count + 1);
System.out.println("投票成功");
}
}
private static void query(Scanner scanner) {
System.out.println("请输入姓名:");
String name = scanner.next();
if (!votes.containsKey(name)) {
System.out.println("该姓名不存在");
} else {
int count = votes.get(name);
System.out.println(name + " 的得票数为:" + count);
}
}
private static void loadVotes() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String name = parts[0];
int count = Integer.parseInt(parts[1]);
votes.put(name, count);
}
} catch (IOException e) {
System.out.println("读取文件失败:" + e.getMessage());
}
}
private static void saveVotes() {
try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_NAME))) {
for (Map.Entry<String, Integer> entry : votes.entrySet()) {
String name = entry.getKey();
int count = entry.getValue();
writer.println(name + "," + count);
}
} catch (IOException e) {
System.out.println("保存文件失败:" + e.getMessage());
}
}
}
```
这个程序可以添加姓名、投票、查询、退出,并将数据记录在本地文件中。具体实现细节可以参考代码注释。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)