麻烦计算百分比

时间:2014-03-18 01:53:21

标签: java

我输入了一个选举代码,我无法获得候选人的投票百分比。 这是我的代码:

import java.util.Scanner;

public class Voting

{
  public static void main (String [] args)
   {
    int candidate1;
    int candidate2;
    int Toronto1;
    int Toronto2;
    int Mississauga1;
    int Mississauga2;
    int Brampton1;
    int Brampton2;
    int total_votes1;
    int total_votes2;
    int totalall;
    double percentage1;
    double percentage2;

    Scanner input = new Scanner (System.in);


    System.out.print("Votes for Candidate 1 from Toronto");
    Toronto1=input.nextInt();
    System.out.print("Votes for Candidate 2 from Toronto");
    Toronto2=input.nextInt();
    System.out.print("Votes for Candidate 1 from Mississauga");
    Mississauga1=input.nextInt();
    System.out.print("Votes for Candidate 2 from Mississauga");
    Mississauga2=input.nextInt();
    System.out.print("Votes for Candidate 1 from Brampton");
    Brampton1=input.nextInt();
    System.out.print("Votes for Candidate 2 from Brampton");
    Brampton2=input.nextInt();

    total_votes1=Toronto1+Mississauga1+Brampton1;
    total_votes2=Toronto2+Mississauga2+Brampton2;
    totalall=total_votes1+total_votes2;
    percentage1=(Toronto1+Mississauga1+Brampton1)/(total_votes1+total_votes2);
    percentage2=(Toronto2+Mississauga2+Brampton2)/(total_votes1+total_votes2);

    System.out.println("Election Results for Toronto:");
    System.out.println("Candidate 1 = " +Toronto1+ "");
    System.out.println("Candidate 2 = " +Toronto2+ "\n");

    System.out.println("Election Results for Mississauga:");
    System.out.println("Candidate 1 = " +Mississauga1+ "");
    System.out.println("Candidate 2 = " +Mississauga2+ "\n");

    System.out.println("Election Results for Brampton:");
    System.out.println("Candidate 1 = " +Brampton1+ "");
    System.out.println("Candidate 2 = " +Brampton2+ "\n");


    System.out.println("Candidate        Vote             Percentage\n");
    System.out.println("Candidate1         "  +total_votes1+ "                   "+percentage1+"%");
    System.out.println("Candidate2         "  +total_votes2+ "                   "+percentage2+"%");
    System.out.println("Total Votes        "  +totalall+"");

  }
}

这就是答案:

Votes for Candidate 1 from Toronto [5] 
Votes for Candidate 2 from Toronto [5]
Votes for Candidate 1 from Mississauga [5]
Votes for Candidate 2 from Mississauga [5]
Votes for Candidate 1 from Brampton [5]
Votes for Candidate 2 from Brampton [5]

Election Results for Toronto:
Candidate 1 = 5
Candidate 2 = 5

Election Results for Mississauga:
Candidate 1 = 5
Candidate 2 = 5

Election Results for Brampton:
Candidate 1 = 5
Candidate 2 = 5

Candidate        Vote             Percentage

Candidate1         15                   0.0%
Candidate2         15                   0.0%
Total Votes        30

正如你所看到的,它表示0.0%而不是50%。

1 个答案:

答案 0 :(得分:6)

看起来你在做分区。 int除以int得到一个int!

所以20/40将== 0,因为20< 40.如果需要浮点结果,请使用双精度:

(double) 20 / 40

或百分比,至少先乘以100。

即改变

percentage1=(Toronto1+Mississauga1+Brampton1)/(total_votes1+total_votes2);

percentage1=(100 * (Toronto1+Mississauga1+Brampton1))/(total_votes1+total_votes2);