有人能告訴我爲什麼下面的代碼在標記的行輸出0
?爲什麼這個整數除法產量爲0?
看起來好像一切都是正確的,但是當我試圖讓結果接近尾聲時,它每次都給我0。
#include <stdio.h>
int main() {
// Gather time-lapse variables
int frameRate, totalLengthSecs;
printf("How many frames per second: ");
scanf("%i", &frameRate);
printf("--> %i frames confirmed.", frameRate);
printf("\nDesired length of time-lapse [secs]: ");
scanf("%i", &totalLengthSecs);
printf("--> %i seconds confirmed.", totalLengthSecs);
int totalFrames = frameRate * totalLengthSecs;
printf("\nYou need %i frames.", totalFrames);
// Time-lapse interval calculation
int timeLapseInterval = totalLengthSecs/totalFrames;
printf("\n\n%i", timeLapseInterval); // <-- this prints 0
return 0;
}
你在「stdin」中輸入「期望的延時長度」是什麼? – 2012-01-11 14:24:16
如果totalLengthSecs/totalFrames小於1,則它被截斷爲0,因爲所有值都是整數,而不是浮點數。 – 2012-01-11 14:26:44