2012年10月18日 星期四

綜合練習-九九乘法

這幾天一直想要找怎麼把圖片貼到部落格上的方法,找了好久感覺都不方便,暫時放棄貼圖的想法,先寫一篇九九乘法的範例吧。

之前幾篇文章談論了有關變數宣告、邏輯判斷與迴圈敘述,今天就來練習一下怎麼寫九九乘法的程式。

讓我們先開一個新專案,接著回想一下小時後背的九九乘法表:
2 x 1 = 2
2 x 2 = 4
.
.
.
.
.
9 x 8 = 72
9 x 9 = 81
以2開頭系列為例,第一個數字是固定的2,第二個數字是變動的,要怎麼用迴圈達到效果呢?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise_1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 9; i++)
            {
                string result = "2" + " x " + i + " = " + 2 * i;
                Console.Write(result + "\r\n");
            }
            Console.ReadKey();
        }
    }
}

用迴圈敘述顯示出2開頭系列是不是很容易!那麼接下來3系列、4系列.......9系列,應該知道怎麼做了吧!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise_1
{
    class Program
    {
        static void Main(string[] args)
        {
            //2開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "2" + " x " + i + " = " + 2 * i;
                Console.Write(result + "\r\n");
            }
            //3開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "3" + " x " + i + " = " + 3 * i;
                Console.Write(result + "\r\n");
            }
            //4開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "4" + " x " + i + " = " + 4 * i;
                Console.Write(result + "\r\n");
            }
            //5開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "5" + " x " + i + " = " + 5 * i;
                Console.Write(result + "\r\n");
            }
            //6開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "6" + " x " + i + " = " + 6 * i;
                Console.Write(result + "\r\n");
            }
            //7開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "7" + " x " + i + " = " + 7 * i;
                Console.Write(result + "\r\n");
            }
            //8開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "8" + " x " + i + " = " + 8 * i;
                Console.Write(result + "\r\n");
            }
            //9開頭
            for (int i = 1; i <= 9; i++)
            {
                string result = "9" + " x " + i + " = " + 9 * i;
                Console.Write(result + "\r\n");
            }
            Console.ReadKey();
        }
    }
}


是不是很容易,我們今天的目的,九九乘法表就完成了。但是,從程式設計的原則來看,還有進步的空間,怎麼說?

第一點,重複的程式法太多了,怎麼修改?
注意看這些程式碼你會發現兩件事情,第一件事情是每段程式碼【差別只有一個字串與一個整數】,第二件事情是差別的部分竟然是【有規則的數列】,於是開始想,是不是有可能再用一個迴圈來替代【有規則的數列】的這件事情,讓我們來試試看。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise_1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int j = 2; j <= 9; j++)
            {
                //2開頭
                for (int i = 1; i <= 9; i++)
                {
                    string result = j + " x " + i + " = " + j * i;
                    Console.Write(result + "\r\n");
                }
            }
            Console.ReadKey();
        }
    }
}

修改了一下程式碼,增加了一個迴圈在2開頭程式碼的外面,把差異的兩個部分字串【"2"】與整數【2】替換成最外面迴圈宣告的變數【j】,運行結果與之前程式碼相同,程式碼是不是簡潔許多。

在這邊如果龜毛一點的人或許會覺得,這個排版醜死了,我想要漂亮一點的,至少要像這樣:
2 x 1 = 2 3 x 1 = 3 4 x 1 = 4.......
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8.......
.
.
.
.
.
.
2 x 8 = 16 3 x 8 = 24 4 x 8 = 32.......
2 x 9 = 18 3 x 9 = 27 4 x 9 = 36.......
怎麼辦,這就要靠一些小技巧了,記得我說過【Console.Write()】這個函數比較彈性這句話嗎,這邊要來展現他的彈性了,我們把上面的程式碼稍微修改一下。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise_1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int j = 2; j <= 9; j++)
            {
                //2開頭
                for (int i = 1; i <= 9; i++)
                {
                    string result = j + " x " + i + " = " + j * i + "  ";
                    Console.Write(result);
                }
                Console.Write("\r\n");
            }
            Console.ReadKey();
        }
    }
}

上面的程式只做了點小修改:
1、在字串變數【result】最後加上" ",作用是區隔開前面的字串。
2、把換行符號【\r\n】移到內迴圈執行完才作用。

讓我們從頭到尾看一次程式碼邏輯。

Step1、進入外迴圈,並宣告變數【j】,給予初始值2,【判斷式】【j<=9】,變動規則為【j+1】。
這個外迴圈控制的是九九乘法中第一個數字,所以由2開始。

Step2、檢查【判斷式】【j<=9】是否【成立】,【成立】則執行Step3,【不成立】則跳到Step10。

Step3、進入內迴圈,並宣告變數【i】,給予初始值1,【判斷式】【i<=9】,變動規則為【i+1】。
這個外迴圈控制的是九九乘法中第二個數字,所以由1開始。

Step4、檢查【判斷式】【i<=9】是否【成立】,【成立】則執行Step5,【不成立】則跳到Step8。

Step5、宣告字串函數【result】存放組合字串,內容是【j】x 【i】 = 【j*i】再加上【" "】區隔字串。

Step6、顯示出字串函數【result】。

Step7、i+1,因為變數【i】的值改變了,需要重新檢查【判斷式】,回到Step4。

Step8、換行。

Step9、j+1,因為變數【j】的值改變了,需要重新檢查【判斷式】,回到Step2。

Step10、結束。

這邊還有一個小瑕疵,就是還是沒有對齊,原因是因為有的積(相乘的結果)是一位數,有的是兩位數,如果真的很介意這件事情,那就把【i * j】換成【(j * i).ToString("00")】,就真的很整齊。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise_1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int j = 2; j <= 9; j++)
            {
                //2開頭
                for (int i = 1; i <= 9; i++)
                {
                    string result = j + " x " + i + " = " + (j * i).ToString("00") + "  ";
                    Console.Write(result);
                }
                Console.Write("\r\n");
            }
            Console.ReadKey();
        }
    }
}

【(j * i).ToString("00")】這是蝦米意思?意思是說,把 j 和 i 相乘的結果轉換為2位數字串,不懂,比方說2 x 1 = 2 就會變成 2 x 1 = 02,就這麼簡單。

小弟我能想到最好的解釋大概就寫在上面了,不過好像有解釋等於沒解釋,我盡力了,有問題歡迎大家提出來討論,今天就到這裡了,改天見。

P.S 如果你程式碼的結果和我說的不一樣,那也許是因為你的命令提示視窗不夠寬,更改一下視窗框度就可以看到正常的結果了。滑鼠移到視窗標題,滑鼠右鍵,內容,視窗大小調整到140,確定,就可以了。

1 則留言:

  1. 照你程式碼打會有例外。 另外 Console.ReadKey();是什麼意思啊??

    回覆刪除