gpt4 book ai didi

java - 否则没有 if 错误 : I dont understand why java isn't recognizing my if statment

转载 作者:行者123 更新时间:2023-12-01 06:44:28 25 4
gpt4 key购买 nike

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

}
}

最佳答案

问题是这样的:

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

尾随 ;让 Java 相信 if声明完毕,{}if之外之后阻止条件,因此 else部分没有匹配 if在它之前。

这是一个相当常见的错误,而且很难发现。如果不是else block ,代码本来可以正确编译,但也可能是错误的。底线:永远、永远不要放 ;if 的开头行, forwhile声明。

关于java - 否则没有 if 错误 : I dont understand why java isn't recognizing my if statment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com