Skip to content

Instantly share code, notes, and snippets.

@zhyunk
Created March 6, 2023 16:21
Show Gist options
  • Save zhyunk/9589903db1ea9519023d700f57b79f6a to your computer and use it in GitHub Desktop.
Save zhyunk/9589903db1ea9519023d700f57b79f6a to your computer and use it in GitHub Desktop.
과제 6 피드백 반영!
import java.util.Random;
/*
* [과제 6. 가상 대선 당선 시뮬레이션 프로그램] 의 피드백 반영!
*
* 작성자 : 김지현
* 작성일 : 2023-03-06
* */
public class MiniAssignment06InFeedback {
public static void main(String[] args) {
String[] arrHuBoName = {"이재명", "윤석열", "심상정", "안철수"};
int[] arrHuBoGetTicket = {0, 0, 0, 0};
int totalCount = 10000; // 전체 투표자 수
float huBoGetTicketPer = 0; // 후보의 득표 수 퍼센티지 계산할때 사용될 용도
Random rd = new Random();
int nowChoice = -1; // 지금 뽑힌 후보 번호
for (int i = 1; i <= totalCount; i++) {
nowChoice = rd.nextInt(arrHuBoName.length);
arrHuBoGetTicket[nowChoice]++;
System.out.println(String.format("\n[투표진행율]: %.2f%%, %d명 투표 => %s", (i/(float)totalCount) * 100, i, arrHuBoName[nowChoice]));
// 후보 출력
for(int j = 0; j < arrHuBoName.length; j++) {
huBoGetTicketPer = (arrHuBoGetTicket[j] == 0) ? 0 : (arrHuBoGetTicket[j] / (float)totalCount) * 100;
System.out.println(String.format("[기호:%d] %s: %05.2f%%, (투표수: %d)", j + 1, arrHuBoName[j], huBoGetTicketPer, arrHuBoGetTicket[j]));
}
}
// 당선인 찾아서 출력
int maxIdx = 0;
for (int i = 1; i < arrHuBoGetTicket.length; i++) {
if (arrHuBoGetTicket[maxIdx] < arrHuBoGetTicket[i]) {
maxIdx = i;
}
}
System.out.println("[투표결과] 당선인: " + arrHuBoName[maxIdx]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment