1

I am somewhat new to Java and I am working on an assignment where the user inserts the number of votes of 2 candidates in an election and I need to calculate the total percentage of the total number of votes for each candidate. I am also not that good at math and I was told to declare a "double" variable than cast it. My output prints the percentage as 0.0% and I think my formula for finding percentage is wrong. Anyways, here is my code:

import java.util.Scanner;

public class Election {

    public static void main (String []args) {
        int y;
        int n;
        int c=0;
        int t=0;
        int csum=0;
        int tsum=0;
        int c2=0;
        int t2=0;
        int tie=0;
        double cpercent;
        double tpercent;
    
        String i;
        Scanner scan = new Scanner (System.in);
    
        System.out.println ("Are there any more precincts to report?");
        i = scan.next();
    
        while (i.equalsIgnoreCase("y")) {
            System.out.println ("Insert the number of votes for Polly Clitch");
            c = scan.nextInt();
       
            System.out.println ("Insert the number of votes for Ernest Truble");
            t = scan.nextInt();
         
            System.out.println ("Are there any more precincts to report?");
            i = scan.next();
        
            csum+=c;
            tsum+=t;
        
            if (c > t)
                c2++;
            if (c < t)
                t2++;
            if (c == t)
                tie++;
        }

        System.out.println ("Polly Clitch's total amount of votes is " + csum);
        System.out.println ("Ernest Truble's total amount of votes is " + tsum);
        cpercent = (double) (csum/100);
        tpercent = (double) (tsum/100);
        System.out.println ("Polly Clitch's total amount of votes percentage is " + cpercent + "%");
        System.out.println ("Ernest Truble;s total amount of votes percentage is " + tpercent + "%");
       
    }
}

Output:

Are there any more precincts to report?

y

Insert the number of votes for Polly Clitch

48

Insert the number of votes for Ernest Truble

56

Are there any more precincts to report?

n

Polly Clitch's total amount of votes is 48

Ernest Truble's total amount of votes is 56

Polly Clitch's total amount of votes percentage is 0.0%

Ernest Truble's total amount of votes percentage is 0.0%

Community
  • 1
  • 1
Learning_Java
  • 65
  • 1
  • 4
  • 12

0 Answers0