Skip to content

Instantly share code, notes, and snippets.

@tbielawa
Created December 9, 2009 07:55
Show Gist options
  • Save tbielawa/252341 to your computer and use it in GitHub Desktop.
Save tbielawa/252341 to your computer and use it in GitHub Desktop.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Vote
{
//main method will collect data from the user and place it within arrays
public static void main (String[] args){
Scanner sc = new Scanner (System.in);
int row;
int col;
//int sum = 0;
String[]candidates = new String[3];
int[][] vote = new int[3][11];
for(row = 0; row < vote.length ; row++){
//prompts the user for the next candidate
System.out.println("Enter the names of the candidates:");
candidates[row] = sc.next();
System.out.println("Enter the votes for the candidate:");
for(col = 0; col < vote[row].length -1 ; col++){
vote[row][col] = sc.nextInt();
}
}
theChart(vote,candidates);
}
//method "theChart" will do the number crunching and print out the results
public static void theChart(int vote[][],String candidates[]){
DecimalFormat round = new DecimalFormat("#.##");
int a;
int b = 0;
int i;
int j = 0;
double testpercent = 0;
double percent = 0;
int total = 0;
double tempPer = 0;
double max = 0;
String winner = "someone";
// Total number of votes for all candidates
for(a = 0; a < vote.length ; a++){
for(b = 0; b < vote[a].length -1 ; b++){
total += vote[a][b];
}
}
//vote is /really/ "vote-tally"
for(i = 0; i < vote.length ; i++){
System.out.printf(" " + candidates[i]);
int sum = 0; //sum of total votes for this candidate
// Loop over the tally of votes for candidate i.
for(j = 0; j < vote[i].length -1 ; j++){
sum += vote[i][j];
System.out.print(" "+vote[i][j]); //print the votes
}
percent = (double)sum/total*100; //"percent votes won total"
//print "candidate bla got x%"...
//Ask yourself a question. What are we trying to do? Is
// the percent of votes this candidate got higher than any
// other candidates perecnt?
//current greatest percent of votes < this candidates percent of the votes?
if(max < percent){
max = percent;
//winner = this candidate
//winnner_row = i
}
int k;
int h;
// //what does this do?
// for(h = 0; h < vote.length; h++){
// if(percent == max){
// winner = candidates[h];
// break;
// }
// }
//System.out.print("the sum of elements of row " + (i + 1) +" = " + sum +" the total of the sums = "+total+" \n");
System.out.print("the percent of votes =" +round.format(percent)+"% " + max +"\n" );
}
System.out.println(winner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment