否则没有 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:42:19  来源:igfitidea点击:

Else without if error: I dont understand why java isn't recognizing my if statment

javaif-statementcompiler-errors

提问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 ifstatement is finished, and the {}block after it is outside the ifcondition, consequently the elsepart has no matching ifpreceding it.

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

This is a rather frequent bug, and a hard one to spot. If it weren't for the elseblock, 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, foror whilestatement.

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

回答by I?ya Bursov

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