我對編碼相對比較陌生,目前正在學習C語言。我在教科書練習中遇到了一些麻煩,並且非常感謝您的幫助。儘管循環變量初始化和變量類型(C)
該練習的主要目標是通過輸入完成的工作時間和小時費率來計算員工應該得到的總工資。費率100%可達40小時,但40小時後達到150%。下面是我寫的代碼:
#include <stdio.h>
int main (void) {
int employee, hour, ratenormal, totalrate, counter, rate;
int salary;
hour = 1;
while (hour != -1) {
printf("Enter # of hours worked (-1 to end): ");
scanf_s("%d", &hour);
if (hour == -1) break;
printf("Enter hourly rate of the work ($00.00): $");
scanf_s("%d", &rate);
if (hour <= 40) {
salary = hour * rate;
}
else if (hour > 40) {
salary = salary + ((hour - 40) * (rate * 1.5));
}
printf("Salary is $%d\n\n", salary);
}
return 0;
}
我有幾個問題:
1)如果我宣佈變量「工資」的浮動,那麼結果只打印0,這是爲什麼?
2)循環工作正常,但只適用於第一次迭代。問題是我需要再次初始化變量薪水,否則它會保存以前使用的一些值。我怎麼能這樣做?
謝謝你的幫助!