0

在第 53 行,它给了我一个 else without if 的错误。我显然有一个 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)+".");

    }
}
4

2 回答 2

7

这是问题所在:

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

该尾随;使 Java 认为该if语句已完成,并且{}它之后的块在if条件之外,因此该else部分之前没有匹配if

这是一个相当常见的错误,而且很难发现。如果不是else块,代码会正确编译,但它会是错误的。底线:永远不要将 a放在, or语句;的开头行。ifforwhile

于 2013-10-10T00:14:47.897 回答
5
if(dDaysWorked <= WEEK); - remove last ;
于 2013-10-10T00:14:20.007 回答