否则没有 if 错误:我不明白为什么 java 不能识别我的 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19284931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Else without if error: I dont understand why java isn't recognizing my if statment
提问by allenharveyiii
At line 53 it is giving me an error of else without if. I clearly have an if statement, but i don't know what i'm doing wrong to make java not recognize it. I've tried moving around the braces and nothing is working.
在第 53 行,它给了我一个没有 if 的 else 错误。我显然有一个 if 语句,但我不知道我做错了什么让 java 无法识别它。我试过在大括号周围移动,但没有任何效果。
import java.util.Scanner;
import java.text.DecimalFormat;
public class Quiz6
{
public static void displayInfo()
{
System.out.println(
"\n\tAuthor: Allen Watson \n" +
"\tClass: \tCSCI 1250-001 \n" +
"\tDate: \t10/09/2013 \n" +
"\tLab: \tQuiz6 \n");
}
public static double calculatePay(int hourWorked, double hourlyRate)
{
double dPay;
dPay = (hourWorked * hourlyRate);
return dPay;
}
public static void main(String[] args)
{
Scanner Keyboard = new Scanner(System.in);
DecimalFormat dfMoney = new DecimalFormat("$#,##0.00");
String strName;
int iCount;
int iDaysWorked;
int iTotalHoursWorked;
int iSingleDayHours;
double dHourlyRate;
final byte WEEK = 7;
displayInfo();
System.out.print("\tWhat is your name: ");
strName = Keyboard.nextLine();
System.out.print("\n\tHow many days did you work this week: ");
iDaysWorked = Keyboard.nextByte();
System.out.print("\n\tHow much do you make an hour: ");
dHourlyRate = Keyboard.nextDouble();
if(dDaysWorked <= WEEK);
{
for(iCount = 1; iCount <= iDaysWorked ; iCount++)
{
System.out.print("\tHow many hours did you work on the day"+iCount+":");
iSingleDayHours = Keyboard.nextInt();
iSingleDayHours += iTotalHoursWorked;
}
}
else
{
bDaysWorked = 0;
System.out.print("A week can only have seven days");
}
calculatePay(iTotalHoursWorked,dHourlyRate);
System.out.print("Hello "+strName+", you worked a total of "+iTotalHoursWorked+" hours over "+iDaysWorked+" days.");
System.out.print("\nWith am hourly rate of "+dfMoney(dHourlyRate)+" you made "+dfMoney(dPay)+".");
}
}
采纳答案by óscar López
Here's the problem:
这是问题所在:
if(dDaysWorked <= WEEK); // remove the ;
That trailing ;
is making Java believe that the if
statement is finished, and the {}
block after it is outside the if
condition, consequently the else
part has no matching if
preceding it.
该尾随;
使 Java 相信该if
语句已完成,并且{}
它之后的块在if
条件之外,因此该else
部分在if
它之前没有匹配项。
This is a rather frequent bug, and a hard one to spot. If it weren't for the else
block, the code would have compiled correctly, but it would have been wrong. Bottom line: never, ever put a ;
in the opening line of an if
, for
or while
statement.
这是一个相当常见的错误,而且很难发现。如果不是else
块,代码会正确编译,但它会出错。底线:永远,永远不要;
在if
,for
或while
语句的开头放置一个。
回答by I?ya Bursov
if(dDaysWorked <= WEEK); - remove last ;