【问题标题】:How to break out of a loop and not execute the other statements in that loop?如何跳出循环而不执行该循环中的其他语句?
【发布时间】:2020-02-26 01:43:03
【问题描述】:
void process(){
  int ID;
  float hours_employee_worked, hourly_wages_for_employees;
  float tax = 0.4;
  float overtime = 1.5;
  float time_without_overtime, total_overtime;
  float total, average;


  do {
    printf("Enter Employee ID (or -1 to exit): ");
    scanf("%d", &ID);
    printf("Enter hours worked (e.g 8.5 hours): ");
    scanf("%f", &hours_worked);
    printf("Enter hourly wage (e.g 20.25): ");
    scanf("%f", &hourly_wage);
    if (ID == -1)
    {
      puts("All done");
      break;
    }
    else if (hours_employee_worked <=40)
    {
      total = hours_employee_worked* hourly_wages_for_employees;
      average = total * tax;
      average = total - average;
      printf("%.2f", average);
    }
    else if (hours_employee_worked > 40)
    {
      total_overtime = ((hours_employee_worked- 40) * 1.5) * hourly_wage_for_employees;
      total = (40 * hourly_wages_for_employees+ total_overtime) * tax;
      time_without_overtime = 40 * hourly_wages_for_employees;
      average = time_without_overtime + total_overtime - total;
      printf("%f", average);
    }

  }while (ID != -1);

}

基本上,如果员工 ID 等于 -1,我希望我的程序结束执行。但它仍然继续并在最后中断。在执行完成并且不会跳出循环之前,它不会说“全部完成”。谢谢。

【问题讨论】:

  • nvm 想通了

标签: c loops break


【解决方案1】:

如果您愿意,您的程序可以正常运行,您可以在检查employee_id 后立即将第一个 if 语句移至程序顶部。

【讨论】:

    猜你喜欢
    • 2021-08-31
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2013-04-13
    • 2019-05-06
    • 2012-04-02
    相关资源
    最近更新 更多