Else没有如果错误:我不明白为什么java不能识别我的if语句 [英] Else without if error: I dont understand why java isn't recognizing my if statment

查看:296
本文介绍了Else没有如果错误:我不明白为什么java不能识别我的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第53行,它给我一个错误的else没有if。我明显有一个if语句,但我不知道我做错了,使java不能识别它。我试着绕过花括号,没有什么工作。

  import java.util.Scanner; 
import java.text.DecimalFormat;

public class Quiz6
{

public static void displayInfo()
{
System.out.println(
\\\
\tAuthor:Allen Watson \\\
+
\tClass:\tCSCI 1250-001 \\\
+
\tDate:\t10 / 09/2013 \\\
+
\tLab:\tQuiz6 \\\
);
}

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是你的名字:);
strName = Keyboard.nextLine();
System.out.print(\\\
\t这个星期你工作了多少天:);
iDaysWorked = Keyboard.nextByte();
System.out.print(\\\
\t你做了多少时间:);
dHourlyRate = Keyboard.nextDouble();

if(dDaysWorked< = WEEK);
{
for(iCount = 1; iCount <= iDaysWorked; iCount ++)
{
System.out.print(\t您在当天工作多少小时+ iCount +:);
iSingleDayHours = Keyboard.nextInt();
iSingleDayHours + = iTotalHoursWorked;
}
}
else
{
bDaysWorked = 0;
System.out.print(一周只能有七天);
}

calculatePay(iTotalHoursWorked,dHourlyRate);

System.out.print(Hello+ strName +,您在+ iDaysWorked +天内总共工作了+ iTotalHoursWorked +小时。
System.out.print(\\\
W小时费率+ dfMoney(dHourlyRate)+你做+ dfMoney(dPay)+。

}
}


解决方案

这里有问题:

  if(dDaysWorked< = WEEK); // 去除 ; 

尾随; if 语句完成后, {} / code>条件,因此 else 部分在其前面没有匹配 if b
$ b

这是一个相当频繁的错误,很难找到。如果它不是 else 块,代码将正确编译,但它是错误的。底线:从不,在的开始行中,如果的时候放置; 语句。


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.

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)+".");

    }
}

解决方案

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.

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没有如果错误:我不明白为什么java不能识别我的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆