簡體   English   中英

返回 scanf 並且不運行循環

[英]Back to scanf and dont run the loops

好的,為什么我的代碼總是支持我 scanf? 我不知道我在這段代碼中做錯了什么,但該代碼僅在 scanf 中備份我,僅讀取數字

int main()
{
    int m,n,i,tmp,min=9999999;
    scanf("%d%d",&m,&n); // scanf m and n right ?
    for(i=m;i<=n;i++) // for loop
    {
        tmp=i; // we give tmp a i value 
        while(tmp%10%2 == 0) 
        {
            tmp/=10;// in this while loop we check if the number is made only of even numbers
        }
        if(tmp!=0) // when the while loop break  we go down here right ? and if the number isn't made only of even digits we printf No
        printf("NO");
        else{ // else if its made of we check that that number is smaller then min number made of even digits
            if(i<min)
            min=i;
        }

    }
printf("%d",min); // on the end of the for loop we printf the min number made of even digits 
}

那么為什么我的編譯器不會運行這個程序呢?

您的程序在每個偶數組成的第一個數字上進入無限循環。 您將數字切開並將tmp引導到零,然后進入無限循環: 0 % 10 % 2為零, 0 / 2的結果為零。 你可以這樣修復它:

while (tmp > 0 && (tmp % 10) % 2 == 0) { /* ... */ }

我猜你在編譯時沒有遇到問題,但是,相反,你運行代碼並沒有任何反應。

我想你在想像“scanf(”%d%d",&m,&n)",如果你輸入“1 30”,m 將為 1,n 為 30,scanf 將返回 2。但是實際上 scanf 將返回 1 因為當它看到未反映在格式字符串中的空間時它會停止解析,因此 m 將為 1,n 在技術上將是隨機的,但在您的情況下可能為 0,並且您的循環將“運行”從1到0,即零次。 試試“%d %d”。 並檢查 scanf 的返回碼以確保輸入了 2 個數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM