誰能告訴我為什么我的輸出是錯誤的“1* 3 = 1”而其他乘法是正確的?謝謝!
#include <stdio.h>
int main()
{
int n,i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n",n);
while(i<= 12)
{
printf("\n%d*%d=%d", i, n, total);
i ;
total = i*n;
}
}
uj5u.com熱心網友回復:
total在為其分配任何值之前,您正在列印一個值。在while回圈的第一次迭代中,total不包含任何定義的值。
這是修復它的方法:
#include <stdio.h>
int main(void)
{
int n, i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n", n);
while (i <= 12)
{
total= i * n;
printf("\n%d*%d=%d", i, n, total);
i ;
}
}
只需total在列印前計算,而不是列印后。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/368927.html
標籤:C
上一篇:為什么這個c程式不能編譯階乘
