Created
March 2, 2023 14:26
-
-
Save zhyunk/ab104f0bbaedab888e85106e4d319ac0 to your computer and use it in GitHub Desktop.
과제 6. 가상 대선 당선 시뮬레이션 프로그램
This file contains hidden or 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; | |
/* | |
* 과제 6. 가상 대선 당선 시뮬레이션 프로그램 | |
* | |
* 작성자 : 김지현 | |
* 작성일 : 2023-03-02 | |
* */ | |
public class MiniAssignment06 { | |
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(4); | |
arrHuBoGetTicket[nowChoice]++; | |
System.out.println(String.format("\n[투표진행율]: %.2f%%, %d명 투표 => %s", (i/(float)totalCount) * 100, i, arrHuBoName[nowChoice])); | |
// 후보 출력 | |
for(int j = 0; j < 4; 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