看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Windows 8.1 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Code Blocks 17.12 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 請問一下 當輸入為0時不會進入while迴圈,所以count的值維持=0,預期應該要印出N/A 當輸出1 2 3 0時,總共進入迴圈3次,預期的平均值為(1+2+3)/3=2.000000 實際上印出1.500000 當輸入2 2 2 2 0時,預期平均值為(2+2+2+2)/4=2.000000 實際上印出1.600000 由結果推斷count的值 似乎都比預期的值多1? 請問問題出在哪裡呢? 先謝謝大大不吝指導 餵入的資料(Input): 第一筆input: 0 第二筆input: 1 2 3 0 第三筆input: 2 2 2 2 0 預期的正確結果(Expected Output): 第一筆output: N/A 第二筆output: 2.000000 第三筆output: 2.000000 錯誤結果(Wrong Output): 第一筆output: 0.000000 第二筆output: 1.500000 第三筆output: 1.600000 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) https://pastebin.com/a1nsTruC 補充說明(Supplement): ----- Sent from JPTT on my Asus ASUS_Z01KDA. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.173.189.144 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1551329587.A.A5D.html
sun409: 輸入 0 的時候,count 還是 + 1 了 02/28 13:15
loveme00835: 第一次判斷 number 值的時候, 值是從哪裡來的? 02/28 13:15
stayhungry: 看懂了!要scanf 完才能判斷有沒有等於0 謝謝兩位大大 02/28 13:28
allensheng: count多了1 02/28 17:16
Vent: num不給初值就判斷是不是不太好啊 02/28 17:18
loveme00835: 這邊適合用 do-while 改寫 02/28 20:13
stayhungry: 謝謝大家,後來改寫成這樣,進迴圈之前先執行一次scan 02/28 20:30
stayhungry: f 02/28 20:30
stayhungry: https://pastebin.com/8bkJZ9Bw 02/28 20:30
stayhungry: do while的寫法我再想想看 02/28 20:30
stayhungry: do-while 的寫法也懂了 謝謝 02/28 21:48
stayhungry: https://pastebin.com/s6BSf9es 02/28 21:48
loveme00835: scanf 也有回傳值, 也可以一起做判斷 03/01 00:46