-1

我在这里不断收到“找不到符号”错误:

    System.out.println ("Are there still more precincts to report? Please enter y or n");
    response = scan.next();
    while (response.equalsIgnoreCase(y))// Initializations
    {
    System.out.print("Enter votes for Polly: ");
   votesForPolly = scan.nextInt();
    System.out.print("Enter votes for Ernest: ");

如果问题出在其他地方,这是完整的代码:

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

    int pollyCarried = 0;
    int ernestCarried = 0;
    int tied = 0;

    Scanner scan = new Scanner(System.in);

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

    System.out.println ("Are there still more precincts to report? Please enter y or n");
    response = scan.next();
    while (response.equalsIgnoreCase(y))// Initializations
    {
    System.out.print("Enter votes for Polly: ");
   votesForPolly = scan.nextInt();
    System.out.print("Enter votes for Ernest: ");
    votesForErnest = scan.nextInt();

    totalPolly += votesForPolly;
    totalErnest += votesForErnest;

    if ( votesForPolly > votesForErnest )
    pollyCarried++;
   else if ( votesForErnest > votesForPolly )
      ernestCarried++;
   else
      tied++;

    System.out.println ("Are there still more precincts to report? Please enter y or n");
    response = scan.next();
    }// Loop to "process" the votes in each precinct
    int totalVotes = votesForPolly + votesForErnest;

    System.out.println("Polly got " + totalPolly + " votes carrying " + pollyCarried + " precincts.");// Print out the results
    System.out.println("Ernest got " + totalErnest + " votes carrying " + ernestCarried + " precincts.");
    System.out.println("Polly got " + ((totalPolly/totalVotes)*100));
    System.out.println("Ernest got " + ((totalErnest/totalVotes)*100));
    System.out.println("Polly carried " + pollyCarried + " precincts");
    System.out.println("Ernest carried " + ernestCarried + "precincts");
    System.out.println(tied + " precincts resulted in a tie");

    }
}

我还尝试制作一个名为“肯定”的字符或字符串,并将其分配为字母 y,以便我可以在循环中使用它,但这并不好。

4

2 回答 2

2

你没有声明任何与y

在这一行,

while (response.equalsIgnoreCase(y))

编译器不知道是什么y

我猜你是这个意思?

while (response.equalsIgnoreCase("y"))

于 2012-11-13T06:59:17.047 回答
1

在这一行:

while (response.equalsIgnoreCase(y))

y是一个未在任何地方定义的变量。显然,您想将其用作字符串(或字符)。该行应该是:

while (response.equalsIgnoreCase("y"))
于 2012-11-13T07:00:09.467 回答