Skip to content

Instantly share code, notes, and snippets.

@kanghyeon-ma
Created February 8, 2025 20:29
Show Gist options
  • Save kanghyeon-ma/2caa96dadb230ab5cd2f6542e35c317e to your computer and use it in GitHub Desktop.
Save kanghyeon-ma/2caa96dadb230ab5cd2f6542e35c317e to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.Random;
/*마강현 미니과제 6번*/
public class Main{
public static void ElectionSimulation(int totalVote , int totalCandidate , String[] Candidates) {
int voteNumber = 1;
double[] percentageCandidate = new double[totalCandidate];
int[] voteNumberCandidate = new int[totalCandidate];
int max = 0;
int maxIndex = 0;
for (int i = 0; i < totalVote; i++) {
double allPercent = ((double) voteNumber / totalVote) * 100;
int who = new Random().nextInt(totalCandidate);
for (int j = 0; j < totalCandidate; j++) {
if (j == who) {
voteNumberCandidate[j]++;
percentageCandidate[j] += ((double) 1 / totalVote) * 100;
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalVote;
int totalCandidate;
System.out.println("총 진행할 투표수를 입력해 주세요.");
totalVote = scanner.nextInt();
System.out.println("가상 선거를 진행할 후보자 인원을 입력해 주세요.");
totalCandidate = scanner.nextInt();
String[] candidates = new String[totalCandidate];
int[] votes = new int[totalCandidate];
for (int i = 0; i < totalCandidate; i++) {
System.out.println((i + 1) + "번째 후보자이름을 입력해 주세요.");
candidates[i] = scanner.next();
}
System.out.println("[투표진행률] : ");
for (int i = 0; i < totalCandidate; i++) {
double percentage = (votes[i] / (double) totalVote) * 100;
System.out.printf("%s: %.2f%% (득표수: %d)\n", candidates[i], percentage, votes[i]);
}
System.out.println("[투표결과] 당선인 : ");
int maxVotes = -1;
String MaxCandidate = "";
for (int j = 0; j < totalCandidate; j++) {
if (votes[j] > maxVotes) {
maxVotes = votes[j];
MaxCandidate = candidates[j];
}
}
System.out.println(MaxCandidate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment