Created
February 8, 2025 14:53
-
-
Save MinAhPark57/2c18bc7b972bc9e323f8a9b00c4851e6 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
//BE파트타임 박민아 | |
import java.util.*; | |
public class JavaMini_6 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.print("총 진행할 투표수를 입력해 주세요. : "); | |
int numVote = sc.nextInt(); | |
System.out.print("가상 선거를 진행할 후보자 인원을 입력해 주세요."); | |
int numCandidate = sc.nextInt(); | |
sc.nextLine(); // 버퍼에 남아 있는 엔터 키 처리 | |
String[] candidate = new String[numCandidate]; | |
int[] score = new int[numCandidate]; | |
for(int i = 0 ; i < candidate.length ;i++) { | |
System.out.printf("%d번째 후보자 이름을 입력해 주세요. : ",i+1); | |
candidate[i] = sc.nextLine(); | |
} | |
Random random = new Random(); | |
boolean isTie; // 동률 여부 확인 | |
//초기화 | |
do { | |
isTie = false; // 동률 여부 초기화 | |
int max = 0; | |
int maxCount = 0; | |
String win = ""; | |
// 점수 초기화 | |
for (int i = 0; i < score.length; i++) { | |
score[i] = 0; | |
} | |
for (int voteCount = 1; voteCount <= numVote; voteCount++) { | |
int rnd = random.nextInt(numCandidate); | |
score[rnd] ++; //랜덤 투표 | |
double rate = (voteCount /(double)numVote)*100; | |
System.out.printf("\n[투표진행율] : %.2f%% %d명 투표 => %s\n", rate, voteCount, candidate[rnd]); | |
for (int j = 0; j < candidate.length; j++) { | |
double sp = (score[j] / (double) numVote) * 100; | |
System.out.printf("[기호 %d] %s : %.2f%%, (투표수 : %d)\n", j + 1, candidate[j], sp, score[j]); | |
} | |
} | |
//1위 찾기 | |
for (int i = 0; i < score.length; i++) { | |
if (score[i] > max) { | |
max = score[i]; | |
win = candidate[i]; | |
} | |
} | |
//동률 체크 | |
for (int i = 0; i < score.length; i++) { | |
if (score[i] == max) | |
maxCount++; | |
} | |
if (maxCount >1){ | |
isTie = true; | |
}else { System.out.println("\n[[투표결과]] 당선인 : " + win);} | |
} while (isTie); | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment