我在这里不断收到“找不到符号”错误:
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,以便我可以在循环中使用它,但这并不好。