推 optimist9266:原來有PadLeft這招喔 05/04 20:08
→ gasbomb:還滿好用的,就不用對齊對到吐了 05/04 20:09
→ if4:其實我現在在學JAVA已經測試過邏輯,但是我沒有C#編譯器測試^^ 05/04 20:17
→ ssccg:其實用 Console.Write("{0,3}", x*y); 就好了 05/04 21:29
ssccg大大高招 ^_^ 程式又簡化了...為什麼可以這樣用啊?
我已經安裝 VS 2010 Express 已經可以檢驗您的說法,效果是一樣的,正確無誤。XD
這樣省掉一個字串變數的宣告...真的很精簡...^_^
Console.Write(" ");
for (int x = 1; x < 10; x++)
{
Console.Write("{0,3}", x);
}
Console.WriteLine();
for (int y = 1; y < 10; y++)
{
Console.Write(y);
for (int x = 1; x < 10; x++)
{
Console.Write("{0,3}", x * y);
}
Console.WriteLine();
}
Console.ReadLine();
※ 編輯: if4 來自: 114.25.181.174 (05/05 01:52)
※ 編輯: if4 來自: 114.25.181.174 (05/05 10:53)
→ andymai:那是string format... 05/05 11:15
→ if4:謝謝...我再查資料...感恩 ^_^ 05/05 11:29
→ andymai:是...所以是Console.WriteLine()用了string format~並不是 05/05 13:45
→ andymai:一定要Console.Write才能達成目的... 05/05 13:46
→ andymai:特別聲明一下~s大沒說錯~只是我想提醒這一點... 05/05 13:47
→ if4:謝謝 andymai 大大的解說,感恩 ^_^ 05/05 13:48
→ if4:那如果要真的印出 "{0,3}" 是不是需要用到 \ 符號?謝謝 ^_^ 05/05 13:54
→ if4:我試過了,要印出 "\{0,3\}" 無法使用,難道要自己合成字串? 05/05 15:45
不加參數就可以直接印出來 Console.Write("{0,3}") 如果後面要印出 x*y
就要寫兩行,當然...這不是本問題的求解...可能在別的問題上吧 XD
Console.Write("{0,3}");
Console.Write(x * y);
※ 編輯: if4 來自: 114.25.181.174 (05/05 16:04)
→ ssccg:不加參數就印的出來是因為會執行到 Console.Write(string) 05/05 16:27
→ ssccg:這個多載版本,而不是Write(string, params object[])這個 05/05 16:28
→ ssccg:會把前面的string當成format string來處理的版本 05/05 16:28
→ ssccg:{}並不是特殊字元,有用format string的method才會處理他 05/05 16:31
→ ssccg:如果要在format string中印{或}的話,是用{{或}} 05/05 16:36
→ if4:謝謝 ssccg 大大的解說,感恩 ^_^ 05/05 17:48