在java中格式化数组元素的float输出

时间:2013-03-10 15:44:15

标签: java arrays formatting system.out

这是一项具有以下要求的家庭作业:

  1. 编写程序,使用2个数组一个用于5个候选人的名字,另一个用于候选人收到的投票。

  2. 计划应为每位候选人输出姓名收到的投票总投票百分比

  3. 输出格式:输出应为3列,标题为 Candidate 收到投票占总投票数,其次是宣布获胜者姓名的声明。

  4. 输出选举的胜利者。

  5. 到目前为止,我已经对投票数组进行编码以获得用户的整数,找到总票数,并通过查找输入的最高值来确定获胜者。我理解(至少我认为我这样做)每个候选人的总投票百分比应该附在这个数组上,我需要首先找到总数然后将投票指数除以总数来确定每个候选人的百分比。

    我在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); 
    
        }
    }
    

1 个答案:

答案 0 :(得分: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 );

                }

我在格式化参数前移动了我并不真正需要的空白,以便参数独立。