打印“九九乘法表”遇到格式问题
编写的程序如下:
Private Sub Form_click() For i = 1 To 9 For j = 1 To 9 k = i * j Print Tab((j - 1) * 9 + 1); i; "*"; j; "="; k; Next j Print Next iEnd Sub
Private Sub Command1_Click() Dim i%, j%, tpS$For i = 1 To 9 For j = 1 To 9 tpS = tpS & i & "×" & j & "=" & i * j & IIf(i * j > 9, Space(2), Space(3)) Next tpS = tpS & vbCrLfNextLabel1.Caption = tpSEnd Sub
[解决办法]
根本原因,其实你每一次
Print Tab((j - 1) * 9 + 1); i; "*"; j; "="; k;
输出的字符串长度都不止9个,所以每次你用Tab指定的9个间距都只能移到下一行显示。当然,这主要还是因为你不知print打印数字时还要考虑前后会自动加空格的问题。不管那么多,直接对数字去空格吧:
Private Sub Form_click() Dim i, j, k For i = 1 To 9 For j = 1 To 9 k = i * j Print Tab((j - 1) * 9 + 1); Trim(i); "*"; Trim(j); "="; Trim(k); Next j Print Next iEnd Sub