2013-03-10 46 views
-5

这是一个家庭作业下列要求:使用2个阵列一个5名候选人姓名,另一个用于由候选接收票格式的数组元素的浮动输出在Java

  1. 写程序。

  2. 计划应该输出收到的票数,并为每名候选人总票数的百分比。

  3. 输出格式:输出应该是在3列与标题候选人收到的票数,总票数其次是宣布获奖者的名字的声明%。

  4. 输出选举胜利者。

到目前为止,我已经编写了票阵列从用户那里得到的整数,发现总票数,并通过寻找进入最高值决定胜负。我理解(至少我认为我是这样),每个候选人总票数的百分比应该附加到这个数组中,我需要先找到总数,然后将总数除以总票数来确定每个候选人的百分比。

我很难与格式化我的System.out声明中的结果。我想生产的应该看起来像30.91,但是我得到了0.0 5次。

下面是我的代码 - 我已经注释掉了那些不工作的部分,并且移动了一些东西以使其起作用,因此它不像我通常所写的代码那样干净整齐 - 我为此道歉。任何代码的任何帮助将受到欢迎!

import java.util.*; 

public class Candidate_Arrays 
{ 
static Scanner console = new Scanner (System.in); 
public static void main(String[] args) 
{ 
    // array holds votes received 
    final int SIZE = 5; 
    int[] votes = new int[SIZE]; 

    // arrays holds candidate's name 

    //declare variables 
    int winner;   // found highest score 
    int total;   // total after processing 
    float percent;  // candidate's percentage of votes 

    // get candidate's name 

//************************************************************************  
    // get votes from user 
    System.out.print ("Please enter candidate's total number of" 
      +"votes. "); 
    for (int index = 0; index < votes.length; index++) 
    votes[index] = console.nextInt(); 
//************************************************************************ 
    // determine winner 
      // find highest entered value 
      // declare local variable 
      int max = 0; 
      for (int index =0; index < votes.length; index++) 
       if (votes[max] < votes[index]) 
        max = index; 
      winner = votes[max]; 
//************************************************************************ 
    // total votes 
      total = 0; 
      for (int index = 0; index < votes.length; index++) 
       total = total + votes[index]; 

//************************************************************************ 
    // display votes received by each candidate 
      System.out.format("Votes Received: " 
        +" %n"); 
      for (int index = 0; index < votes.length; index++) 
       System.out.format(votes[index] + " "); 

      System.out.println(); 

//************************************************************************ 
    // display percent of total votes earned by each candidate 

//************************************************************************ 
    // total percent of votes 
    System.out.format("Candidate's percentage of votes: " 
      + "%n"); 
      percent = 0; 
      for (int index =0; index < votes.length; index++) 
       { 
       percent = votes[index]/total; 
       System.out.format(
         /*"%2f %n",*/ percent +" "); 
       } 

//************************************************************** 
    // display the winners name 
    // currently displays highest value within array 
    System.out.format("The Election's winner is: " 
      +"%1d %n", winner); 
    System.out.println(); 
//************************************************************** 
    /* 
    System.out.format("Candidate's percentage of votes: " 
      + "%n"); 
    for (int index = 0; index < votes.length; index++) 
    { 

      percent = total/votes[index]; 
     System.out.format(
      "%2f %n", percent +" "); 
    } */ 
//************************************************************** 
    // display total number of votes for the election 
    System.out.println(); 
    System.out.format("Total votes: " 
      +"%1d %n", total); 

    } 
} 
+0

http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax – 2013-03-10 15:48:01

+0

另一个薄这是我的作业的第二部分,所以我cannibalize system.out声明从我为这次会议所做的其他家庭作业,这就是为什么他们要求八分而不是五分名等等。 – Darnell 2013-03-10 15:50:57

+0

我从总票数百分比中得到这个错误信息 候选人总票数的百分比: 线程“main”中的异常java.util.IllegalFormatConversionException:f!= java.lang.String \t at java.util。格式化$ FormatSpecifier.failConversion(来源不明) \t在$的java.util.Formatter FormatSpecifier.printFloat(来源不明) \t在$的java.util.Formatter FormatSpecifier.print(来源不明) \t在的java.util.Formatter。格式(未知来源) \t位于java.io.PrintStream。(Unknown Source) \t位于java.io.PrintStream.printf(未知来源) \t at Candidate_Arrays.main(Candidate_Arrays.java:69) – Darnell 2013-03-10 17:30:44

回答

0

我希望今天能得到答案,但看起来并不会发生。不用担心,因为我找到了解决方案,我愿意分享。这似乎工作:

 // total percent of votes 
    System.out.format("Candidate's percentage of total " 
      +"votes: %n"); 
      percent = 0; 
      for (int index =0; index < votes.length; index++) 
       { 
       percent = (votes[index]/total) *100; 
       System.out.format( " " + "%2.2f %n" , percent); 

       } 

我移动了我没有真正需要的格式参数前面的空白,以便参数独立。