否则没有错误:我不明白为什么java没有识别我的if语句

时间:2013-10-10 00:13:18

标签: java if-statement compiler-errors

在第53行,如果没有,那就给我一个错误。我显然有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)+".");

    }
}

2 个答案:

答案 0 :(得分:6)

问题在于:

if(dDaysWorked <= WEEK); // remove the ;

尾随;使Java认为if语句已完成,{}之后的if块超出else条件,因此{{1}部分之前没有匹配的if

这是一个相当频繁的错误,很难发现。如果它不是else块,代码将正确编译,但它本来是错误的。结论:永远不要将;放在ifforwhile声明的开头行中。

答案 1 :(得分:4)

if(dDaysWorked <= WEEK); - remove last ;