C#上手练习2(FOR语句)
程序员文章站
2022-07-01 17:04:13
C#上手练习2(FOR语句) ......
c#上手练习2(for语句)
using system; using system.collections.generic; using system.text; namespace kingtest02 { class fortest { static void main(string[] args) { fortest fortest = new fortest(); fortest.sum(); fortest.factorial(); } public void sum()//计算1到10的和 { int a = 0; for (int i = 1; i < 11; i++) { if (i == 10) { console.write(i + "="); a = a + i; } else { console.write(i + "+"); a = a + i; } } console.writeline(a); } public void factorial()//99乘法口诀 { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { console.write(j + "*" + i + "=" + j * i + "\t");//\t是转义字符(制表符),如果不使用转义字符\t,则结果显示比较乱 } console.writeline(); } } } }