计算投票java初学者的java项目

时间:2014-01-12 21:40:14

标签: java

当我编译它时,我收到错误

  

错误:令牌“else”上的语法错误,{预期
  错误:令牌“)”上的语法错误,此令牌后面的EmptyStatement

如何修复这些错误,这会修复程序吗?

import java.util.Scanner;

public class Election
{
  public static void main (String[] args)
  {
    int votesForPolly;  // number of votes for Polly in each precinct
    int votesForErnest; // number of votes for Ernest in each precinct
    int totalPolly;     // running total of votes for Polly
    int totalErnest;    // running total of votes for Ernest
    String response;    // answer (y or n) to the "more precincts" question

    Scanner scan = new Scanner(System.in);

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

    // Loop to "process" the votes in each precinct
    {System.out.println ("Enter Votes? Enter Y or N");
    response=scan.next().toUpperCase();

    if (response.equals("Y"))
      response="Yes"; 

    System.out.println ("Enter votes for Polly:");
    votesForPolly=scan.nextInt();
    totalPolly=totalPolly+ votesForPolly;
    System.out.println ("Enter votes for Ernest:");
    votesForErnest=scan.nextInt();
    totalErnest=totalErnest+ votesForErnest;}
    System.out.println ("Enter another District? Enter Y or N");
    response=scan.next().toUpperCase(); }

    else
    {
      response = 0;
      while (response == 1) 
    }

    // Print out the results
    System.out.println ("Total votes for Polly is: " + totalPolly);
    System.out.println ("Total votes for Ernest is: " + totalErnest); 
  }
}

2 个答案:

答案 0 :(得分:1)

此代码如下所示

public class Election {

    public static void main(String[] args) {

        { 
           if (response.equals("Y"))  // if START
                response="Yes";       // if END 
        }

    } else { // else without if 

        while (response == 1) // while without do

    }  // main() END

} // class END

} // ???

缺少开放式大括号,else没有ifwhile没有do

您的if相当于

if (response.equals("Y")) {
    response="Yes"; 
}

这是一个没有if的{​​{1}} {。}}。

答案 1 :(得分:1)

以下是编译的代码,但您的代码中存在一些主要问题,我将尝试在下面指出其中一些:

import java.util.Scanner;

    public class Election {

    public static void main (String[] args) {
        int votesForPolly = 0;  // number of votes for Polly in each precinct
        int votesForErnest = 0; // number of votes for Ernest in each precinct
        int totalPolly = 0;     // running total of votes for Polly
        int totalErnest = 0;    // running total of votes for Ernest
        String response = "";    // answer (y or n) to the "more precincts" question

        Scanner scan = new Scanner(System.in);

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

        // Loop to "process" the votes in each precinct
        System.out.println("Enter Votes? Enter Y or N");
        response=scan.next().toUpperCase();

        if (response.equals("Y")) {
            response="Yes"; 
            System.out.println("Enter votes for Polly:");
            votesForPolly=scan.nextInt();
            totalPolly=totalPolly + votesForPolly;
            System.out.println ("Enter votes for Ernest:");
            votesForErnest=scan.nextInt();
            totalErnest=totalErnest + votesForErnest;
            System.out.println("Enter another District? Enter Y or N");
            response=scan.next().toUpperCase(); 
        } else {
            // int count = 0; <--- see below 
            // while (count == 1) { <--- makes no sense in your context
                // Print out the results
                System.out.println("Total votes for Polly is: " + totalPolly);
                System.out.println("Total votes for Ernest is: " + totalErnest); 
            // }
        }
    }
}
  1. 首先,你的一些变量从未被初始化。编译器会抱怨这个事实。
  2. 你错过了很多代码。
  3. 最后的while语句将永远不会执行,因为条件始终为false,相应的永远不会1。在您的上下文中,似乎完全不需要此while循环,因为您只想打印一个投票计数。
  4. 我还在那里重命名了你的条件变量,因为你不能将相同的变量名分配给多个变量,也不能改变类型,不用声明它。
  5. response="Yes";在这种情况下没有多大意义。