用JAVA代码编写学生投票系统
时间: 2024-05-11 21:15:35 浏览: 75
java-在线投票系统(源码).rar
好的,以下是一个简单的学生投票系统的JAVA代码:
```java
import java.util.Scanner;
public class StudentVoteSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numOfStudents;
String[] studentNames;
int[] voteCounts;
int choice;
// 获取学生人数
System.out.print("请输入学生人数:");
numOfStudents = scanner.nextInt();
// 初始化学生名字和投票数
studentNames = new String[numOfStudents];
voteCounts = new int[numOfStudents];
for (int i = 0; i < numOfStudents; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的名字:");
studentNames[i] = scanner.next();
voteCounts[i] = 0;
}
// 进行投票
while (true) {
System.out.println("请选择要投票的学生编号(输入-1结束投票):");
for (int i = 0; i < numOfStudents; i++) {
System.out.println((i + 1) + ". " + studentNames[i]);
}
choice = scanner.nextInt();
if (choice == -1) {
break;
} else if (choice < 1 || choice > numOfStudents) {
System.out.println("输入错误,请重新输入!");
} else {
voteCounts[choice - 1]++;
System.out.println("投票成功!");
}
}
// 输出投票结果
System.out.println("投票结束,结果如下:");
for (int i = 0; i < numOfStudents; i++) {
System.out.println(studentNames[i] + ":" + voteCounts[i] + " 票");
}
}
}
```
希望这个代码能够帮助到你!
阅读全文