Created
February 8, 2025 20:29
-
-
Save kanghyeon-ma/2caa96dadb230ab5cd2f6542e35c317e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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