Cant find the percentage - JAVA

Lum gashi :

so I have this problem where finding the percentage doesnt work and I really dont know why,so my assignment is to find the number of candidates for election and the number of electors and at the end it should show the percentage of the votes example if there are 3 candidates and 6 electors and 1st candidate gets 3 votes,2nd gets 2 votes, and the 3rd gets 1 vote, it should show : 50.00%,33.33%,16.67%. Below is my code, it gets right the number of votes but when it comes to percentage it just shows 0.0% in all cases.I hope you guys can help me out.


import java.util.Scanner;

public class ElectionPercentage {
    public static void main(String[]args){
        //https://acm.timus.ru/problem.aspx?space=1&num=1263

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter how many candidates are : ");
        int candidates = sc.nextInt();
        int [] allCandidates = new int[candidates];
        int startingCandidate = 1;
        for(int i = 0; i < candidates;i++){
            allCandidates[i] = startingCandidate++; //now value of the first element will be 1 and so on.
        }

       //for testing System.out.println(Arrays.toString(allCandidates));

        System.out.println("enter the number of electors : ");
        int electors = sc.nextInt();
        int [] allVotes = new int[electors];

        for(int i =0;i < electors;i++){
            System.out.println("for which candidate has the elector voted for :");
            int vote = sc.nextInt();
            allVotes[i] = vote; //storing all electors in array
        }

        System.out.println();
        int countVotes = 0;
        double percentage;
        for(int i = 0;i<allCandidates.length;i++){
            for(int k = 0; k < allVotes.length;k++){
                if(allCandidates[i]==allVotes[k]){
                    countVotes++;
                }
            }
            System.out.println("Candidate "+allCandidates[i]+" has :  "+countVotes+" votes.");
            percentage = ((double)(countVotes/6)*100);
            System.out.println(percentage+"%");
            countVotes = 0;
        }

    }

}




rzwitserloot :

countVotes is an int 6 is also an int. Thus, (countVotes/6) which is in your code, near the end, is integer division. 11/6 in integer division is 1. 5/6 is 0. It rounds by lopping off all decimals. That's probably not what you want, especially because you try to cast it to double afterwards.

You're casting the wrong thing. But you don't even need the cast at all; if either side is double, the whole thing becomes double division. So, instead of: percentage = ((double)(countVotes/6)*100); try percentage = 100.0 * countVotes / 6.0;

Also, presumably, that 6 should really be a variable that counts total # of votes, no? i.e. electors, so: percentage = 100.0 * countVotes / electors;

The fact that we kick off the math with 100.0 means it'll be double math all the way down.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27769&siteId=1