作者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/cn.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