java程式 (很急)

You are to write a program to help with tallying votes. The information about candidates and votes must be read from a file. The format of the file is:

The first line contains a single integer, representing the number of candidates in the race, say n

The next n lines each contain the name of a candidate. The order is important. The first is candidate 0, the next is candidate 1, and so on.

The rest of the file (read till end of file) has one integer per line. The integer represents a single vote. Only votes in the range 0 to n-1 are valid. Any other integer is considered a bad vote. You can assume the values will all be integers, but you should determine whether the vote is good or bad.

Three sample data files are available (right-click to download):

vote1.txt

vote2.txt

vote3.txt

If any candidate has a majority (more than half of the valid votes), then that candidate is declared the winner. If a winner is not elected, then a runoff election must be held. All candidates who received at least floor(valid votes/number of candidates) votes is eligible for the runoff election.

Your program should:

Prompt the user for the name of the file with the votes.

Read the contents of the named data file and accumulate the counts.

Keep track of the number of votes for each candidate, and the number of bad votes.

Output the number of candidates, their names and votes each received.

Output the total number of good and bad votes.

Output how many votes were required to win the election.

If there is a winner as defined above, indicate the name of the winner. If no winner was elected, output the number of votes required to be eligible for the runoff. Then list the names of all candidates in the runoff.

Format the output so it is easy to read.

Update:

vote1.txt

Update 2:

2

Al Franken

Norm Coleman

0

1

0

0

1

1

1

1

1

2

0

0

0

8

1

0

Update 3:

vote1.txt

5

Mickey Mouse

Daffy Duck

Roadrunner

Rod Blagojevich

Snow White

1

2

0

4

4

3

1

2

4

1

5

2

1

4

2

4

1

1

12

3

5

4

2

1

2

3

2

3

4

2

3

4

2

3

4

5

5

2

2

2

2

2

2

4

4

4

2

1

3

1

1

2

2

4

1

4

4

12

3

Update 4:

上面那一個是2

現在這個是vote3.txt

2 Answers

Rating
  • 鴨子
    Lv 6
    1 decade ago
    Favorite Answer

    import java.io.*;

    import java.util.*;

    public class Carol {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Please input the filename: ");

    String filename = input.next();

    try

    {

    FileReader file = new FileReader(filename);

    BufferedReader buffer = new BufferedReader(file);

    int num=Integer.parseInt(buffer.readLine().trim());

    String []candidates = new String[num];

    int []votes = new int[num];

    int badVotes=0, goodVotes=0;

    int i;

    for (i=0; i < num;i++)

    {

    votes[i]=0;

    candidates[i]=buffer.readLine();

    }

    while (true)

    {

    String line = buffer.readLine();

    if (line==null) // end-of-file ?

    break; //exit from the loop

    line = line.trim();

    if (line.equals("")) continue; // ignor the blank line

    int target=Integer.parseInt(line);

    if (target >= num || target < 0)

    badVotes++;

    else

    {

    votes[target]++;

    goodVotes++;

    }

    }

    buffer.close();

    System.out.println("The number of candidates: " + num);

    for (i=0; i < num; i++)

    System.out.println(candidates[i] + ": votes= " + votes[i]);

    System.out.println();

    System.out.println("Good votes= " + goodVotes);

    System.out.println("Bad votes= " + badVotes);

    int votesWin = goodVotes/2 + 1;

    System.out.println("Votes required to win: " + votesWin );

    System.out.println();

    for (i=0; i < num;i++)

    if (votes[i] >= votesWin)

    {

    System.out.println("The winner is " + candidates[i]);

    break;

    }

    if (i>=num) // no one won

    {

    System.out.println("No one won.");

    System.out.println("The list of candidates in the runoff:");

    for (i=0; i < num; i++)

    if ( votes[i] >= goodVotes/num ) //at least floor()

    System.out.println(candidates[i]);

    }

    }

    catch (Exception e) { System.out.println("ERROR : " + e.toString()) ; }

    } // end of main()

    } // end of class Carol

  • Lv 4
    1 decade ago

    我有收到你的信嚕

    程式部份大致上已經完成~

    我現在在公司沒有你的eMail

    你在這裡留個mail給我吧 ~

Still have questions? Get your answers by asking now.