Last active
August 2, 2023 03:02
-
-
Save kingseungil/abf9401b9a4ea414480a27a02a616f64 to your computer and use it in GitHub Desktop.
Zerobase-miniproject
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.Random; | |
import java.util.Scanner; | |
public class Miniproject6 { | |
public static void main(String[] args) { | |
try(Scanner sc = new Scanner(System.in)) { | |
System.out.print("총 진행할 투표수를 입력해 주세요."); | |
int total = sc.nextInt(); | |
System.out.print("가상 선거를 진행할 후보자 인원을 입력해 주세요."); | |
int candidateNumber = sc.nextInt(); | |
String[] candidates = new String[candidateNumber]; | |
for (int i = 0; i < candidateNumber; i++){ | |
System.out.print((i+1)+"번째 후보의 이름을 입력해 주세요."); | |
candidates[i] = sc.next(); | |
} | |
int[] votes = new int[candidateNumber]; | |
// 투표 진행 | |
StartVote(total, candidateNumber, votes, candidates); | |
System.out.format("[투표결과] 당선인 : %s",candidates[getMaxIndex(votes)]); | |
} | |
} | |
private static void StartVote(int total, int candidateNumber, int[] votes, String[] candidates) { | |
double progress; | |
for(int i = 1; i <= total; i++){ | |
progress = (i/(double) total)*100; | |
int vote = new Random().nextInt(candidateNumber); | |
votes[vote]++; | |
System.out.format("[투표진행률]: %.2f%%, %d명 투표 => %s\n",progress,i, candidates[vote]); | |
for (int j = 0; j < candidateNumber; j++){ | |
System.out.format("%s: %.2f%%, (투표수 : %d)\n", candidates[j],(votes[j]/(double)i)*100, votes[j]); | |
} | |
} | |
} | |
private static int getMaxIndex(int[] votes) { | |
int maxIndex = 0; | |
for (int i = 1; i < votes.length; i++){ | |
if (votes[maxIndex] < votes[i]){ | |
maxIndex = i; | |
} | |
} | |
return maxIndex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment