当我编译它时,我收到错误
错误:令牌“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);
}
}
答案 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
没有if
,while
没有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);
// }
}
}
}
while
语句将永远不会执行,因为条件始终为false,相应的永远不会1
。在您的上下文中,似乎完全不需要此while
循环,因为您只想打印一个投票计数。response="Yes";
在这种情况下没有多大意义。