【问题标题】:Why wont my else if statement work [duplicate]为什么我的 else if 语句不起作用[重复]
【发布时间】:2013-11-19 01:02:10
【问题描述】:
public class ComputePay
{
   /**
      Compute pay (including overtime) for worker. Hours in excess of 40 hours
      are paid at time-and-a-half.
      @param wage the hourly wage for the employee
      @param hoursWorked the number of hours worked by employee in one week
      @return the amount of pay employee earned
   */
 public static double payForWeek(double wage, double hoursWorked)
   {      
       double totalPay = 0.0;

              if (hoursWorked <= 40);
               {
                   totalPay = hoursWorked*wage;
               }
               else if(hoursWorked > 40);
                 {
                      totalPay = (40*wage+(wage*(hoursWorked-40)));
                  }
       return totalPay;
   }
}

它给了我一个没有 if 错误的 else。不知道我在这里做错了什么

【问题讨论】:

  • 问题在于 if 和 else if 语句末尾的分号。删除它们
  • 在您阅读问题之前,您可以知道答案。我们应该有一个检测这些常见错误的机器人..
  • 去掉 if 条件后的分号。此外,您的 else 子句不需要是 else-if,因为如果工作时间不 40
  • 我很想知道对于初学者来说,是否还有比这更多的基本错误

标签: java loops if-statement


【解决方案1】:

删除这些分号:

if (hoursWorked <= 40);
                      ^

else if(hoursWorked > 40);
                         ^

【讨论】:

    【解决方案2】:

    删除; if else:

    if (hoursWorked <= 40)
                   {
                       totalPay = hoursWorked*wage;
                   }
                   else if(hoursWorked > 40)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多