Skip to content

Instantly share code, notes, and snippets.

@dongsub-joung
Last active July 4, 2023 09:17
Show Gist options
  • Save dongsub-joung/575e9821df6134206c992d98ad063abe to your computer and use it in GitHub Desktop.
Save dongsub-joung/575e9821df6134206c992d98ad063abe to your computer and use it in GitHub Desktop.
정동섭
// 정동섭
import java.util.Random;
import java.util.Scanner;
public class Election {
static final int MAX_SIZE= 10;
String[] candidates= new String[MAX_SIZE];
int people;
Election(){
try(Scanner sc= new Scanner(System.in)) {
System.out.printf("총 진행할 투표수를 입력해 주세요: ");
this.people=sc.nextInt();
System.out.printf("가상 선거를 진행할 후보자 인원을 입력해 주세요.(최대 10명)");
int candidate_size= sc.nextInt();
if (candidate_size > 10){
System.out.println("10명이 최대입니다.");
candidate_size=10;
}else if (candidate_size>0){
System.out.println("입후보한 사람은 "+ candidate_size +"명");
}
for (int i=0; i<candidate_size; i++){
System.out.printf(String.format("%d 번째 후보자 이름을 입력해 주세요.", i+1));
this.candidates[i]= sc.next();
}
}catch (Exception e){
System.out.println(e);
}
}
private int getSizeOfCandidate(){
int count= 0;
for (var e: this.candidates){
if (e != null)
count++;
}
return count;
}
public static void main(String[] args) {
Election election= new Election();
int count= election.getSizeOfCandidate();
int[] voting= new int[count];
Random random= new Random();
for (int i=1; i<= election.people; i++){
int one_vote= random.nextInt(count);
double percentage= ((double) i/election.people) * 100;
voting[one_vote]+= 1;
System.out.println(String.format("[투표 진행률] %.2f%% %d명 투표 => %s", percentage,i, election.candidates[one_vote]));
for (int j=1; j<=count; j++){
String name= election.candidates[j-1];
double present= ((double) voting[j-1]/election.people) *100;
System.out.println("[기호"+j+"] "+name+" : "+present+ "%"+" (투표수: "+ voting[j-1] +")");
// System.out.printf(String.format("[기호 %d] %s : %.2f (투표수: %d)\n"), j,name,present,voting[j-1]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment