Yahoo奇摩知識+ 將於 2021 年 5 月 4 日 (美國東部時間) 終止服務。自 2021 年 4 月 20 日 (美國東部時間) 起,Yahoo奇摩知識+ 網站將會轉為唯讀模式。其他 Yahoo奇摩產品與服務或您的 Yahoo奇摩帳號都不會受影響。如需關於 Yahoo奇摩知識+ 停止服務以及下載您個人資料的資訊,請參閱說明網頁。

Joe 發問時間: 電腦與網際網路程式設計 · 1 0 年前

Java迴圈

import java.util.Scanner;

public class Election

{

public static void main (String[] args)

{

int votesForPolly = 0;

int votesForErnest = 0;

int totalPolly = 0; // running total of votes for Polly

int totalErnest = 0; // running total of votes for Ernest

int countPrecPolly = 0;

int countPrecErnest = 0;

int countPrecTies = 0;

String response; Scanner scan = new Scanner(System.in);

System.out.println ();

System.out.println ("Election Day Vote Counting Program");

System.out.println ();

System.out.println("Whether or not there are more precincts to report(y or n): ");

response = scan.nextLine();

while(response.equalsIgnoreCase("y"))

{

System.out.println("Votes for Polly: ");

votesForPolly = scan.nextInt();

System.out.println("Votes for Ernest: ");

votesForErnest = scan.nextInt();

if(votesForErnest > votesForPolly)

{

countPrecErnest++;

}

else if(votesForPolly > votesForErnest)

{

countPrecPolly++;

}

else

{

countPrecTies++;

}

System.out.println("Whether or not there are more precincts to report(y or n): ");

response = scan.nextLine();

}

if(response.equalsIgnoreCase("n"))

{

totalPolly += votesForPolly;

totalErnest += votesForErnest;

float percentPolly = totalPolly/(totalPolly+totalErnest);

float percentErnest = totalErnest/(totalPolly+totalErnest);

System.out.println ("Total votes for Polly: "+totalPolly);

System.out.println ("Total votes for Ernest: "+totalErnest);

System.out.println ("Percentages votes of Polly: "+percentPolly);

System.out.println ("Percentages votes of Ernest: "+percentErnest);

System.out.println ("Precincts that Polly won: "+countPrecPolly);

System.out.println ("Precincts that Ernest won: "+countPrecErnest);

System.out.println ("Precincts that ties: "+countPrecTies);

}

}

}

已更新項目:

我需要寫一個程式來蒐集選舉資料

程式一開始會問使用者有沒有選區的資料需要更新

如果y需要輸入2個人各所得到的票數,並且一直問道答案是n為止

然後要計算出總共在不同選區內,得到多少票那個人佔的票有幾%

並且算出2人贏了幾個選區的票還有打和的選區

不知道為什麼我的程式一直計算錯誤我也很搞混= =拜託大大幫忙哩

1 個解答

評分
  • 1 0 年前
    最佳解答

    Don't use a single scanner for text input and integer input.

    You need to use two separate scanner for text and integer.

    My new code as below:

    import java.util.Scanner;

    public class Election

    {

    public static void main (String[] args)

    {

    int votesForPolly = 0;

    int votesForErnest = 0;

    int totalPolly = 0; // running total of votes for Polly

    int totalErnest = 0; // running total of votes for Ernest

    int countPrecPolly = 0;

    int countPrecErnest = 0;

    int countPrecTies = 0;

    String response;

    Scanner scan = new Scanner(System.in);

    Scanner scanint = new Scanner(System.in); //new update by DYU

    System.out.println ();

    System.out.println ("Election Day Vote Counting Program");

    System.out.println ();

    System.out.println("Whether or not there are more precincts to report(y or n): ");

    response = scan.nextLine();

    System.out.println(response);

    while(response.equalsIgnoreCase("y"))

    {

    System.out.println("Votes for Polly: ");

    votesForPolly = scanint.nextInt();

    System.out.println("Votes for Ernest: ");

    votesForErnest = scanint.nextInt();

    if(votesForErnest > votesForPolly)

    {

    countPrecErnest++;

    }

    else if(votesForPolly > votesForErnest)

    {

    countPrecPolly++;

    }

    else

    {

    countPrecTies++;

    }

    System.out.println("Whether or not there are more precincts to report(y or n): ");

    response = scan.nextLine();

    System.out.println(response);

    }

    if(response.equalsIgnoreCase("n"))

    {

    totalPolly += votesForPolly;

    totalErnest += votesForErnest;

    float percentPolly = totalPolly/(totalPolly+totalErnest);

    float percentErnest = totalErnest/(totalPolly+totalErnest);

    System.out.println ("Total votes for Polly: "+totalPolly);

    System.out.println ("Total votes for Ernest: "+totalErnest);

    System.out.println ("Percentages votes of Polly: "+percentPolly);

    System.out.println ("Percentages votes of Ernest: "+percentErnest);

    System.out.println ("Precincts that Polly won: "+countPrecPolly);

    System.out.println ("Precincts that Ernest won: "+countPrecErnest);

    System.out.println ("Precincts that ties: "+countPrecTies);

    }

    }

還有問題?馬上發問,尋求解答。