看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) w10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) devc++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 用for數字迴圈畫三角型 可是超過一定的數字就不會寫了 行數超過10就用星星取代 卡很久都跑不出來 初學者 餵入的資料(Input): #include<stdio.h> int main() { int i,j,k; printf("輸入行數 = "); scanf("%d",&k); for(i=1;i<=k;i++){ for(j=1;j<=i;j++) { printf("%d",i); } printf("\n"); } } 預期的正確結果(Expected Output): 1 22 333 4444 55555 666666 7777777 88888888 999999999 ********** 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.252.100.153 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1650632428.A.5A7.html
EricTCartman: "%c", i > 9 ? '*' : (i + '0') 04/22 21:05
結果出來沒錯 可是不懂原理 可以解釋一下嗎? ※ 編輯: lovesaber (111.252.100.153 臺灣), 04/22/2022 21:08:34
EricTCartman: 阿都寫C++了 control variable(i,j) 就放for裡面吧 04/22 21:08
EricTCartman: 印出字元 如果 i > 9 輸出 * 反之則 '0'~'9' 04/22 21:39
EricTCartman: 如果你不知道?: 可以查conditional operator 04/22 21:40
EricTCartman: 如果你不知道 i + '0' 去理解一下ASCII 04/22 21:40
yesiah: 新手不懂ternary也不懂ascii的話土炮一點用 if else? 04/23 13:22
yesiah: if (i > 9) { 04/23 13:22
yesiah: // print * 04/23 13:22
yesiah: } else { 04/23 13:22
yesiah: // print 1-9 04/23 13:22
yesiah: } 04/23 13:22
penguinlion: 2樓,其實 C11 好像就有 int 宣告在 for loop 裡面了 04/24 22:46
j0958322080: C99,好像就有了 04/25 01:37