作者lovesaber (幻想神)
看板C_and_CPP
標題[問題] 關於for迴圈問題
時間Fri Apr 22 21:00:26 2022
開發平台(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://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1650632428.A.5A7.html
1F:→ EricTCartman: "%c", i > 9 ? '*' : (i + '0') 04/22 21:05
結果出來沒錯 可是不懂原理 可以解釋一下嗎?
※ 編輯: lovesaber (111.252.100.153 臺灣), 04/22/2022 21:08:34
2F:→ EricTCartman: 阿都寫C++了 control variable(i,j) 就放for裡面吧 04/22 21:08
3F:→ EricTCartman: 印出字元 如果 i > 9 輸出 * 反之則 '0'~'9' 04/22 21:39
4F:→ EricTCartman: 如果你不知道?: 可以查conditional operator 04/22 21:40
5F:→ EricTCartman: 如果你不知道 i + '0' 去理解一下ASCII 04/22 21:40
6F:→ yesiah: 新手不懂ternary也不懂ascii的話土炮一點用 if else? 04/23 13:22
7F:→ yesiah: if (i > 9) { 04/23 13:22
8F:→ yesiah: // print * 04/23 13:22
9F:→ yesiah: } else { 04/23 13:22
10F:→ yesiah: // print 1-9 04/23 13:22
11F:→ yesiah: } 04/23 13:22
12F:推 penguinlion: 2樓,其實 C11 好像就有 int 宣告在 for loop 裡面了 04/24 22:46
13F:推 j0958322080: C99,好像就有了 04/25 01:37