如何跳出循环而不执行该循环中的其他语句?
How to break out of a loop and not execute the other statements in that loop?
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 时结束执行。但它仍然继续并在最后中断。它不会说 "All done" 直到执行完成并且不会跳出循环。谢谢
如果您愿意,您的程序运行良好您可以在检查 employee_id 后立即将第一个 if 语句移动到程序的顶部。
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 时结束执行。但它仍然继续并在最后中断。它不会说 "All done" 直到执行完成并且不会跳出循环。谢谢
如果您愿意,您的程序运行良好您可以在检查 employee_id 后立即将第一个 if 语句移动到程序的顶部。