Java實驗項目二——二維數組實現九九乘法表


Program:打印乘法口訣表

    (1)編寫一個方法,參數(二維數組),完成將二維數組中的數據按照行列顯示的工作。

    (2)編寫一個測試方法,給出99乘法表,放入到二維數組中,調用(1)中的方法,顯示乘法口訣表。

 

Description:main方法所在類在最后給出,代碼如下:

 

 1 /*
 2  *Description:定義工具類,負責初始化二維數組和打印二維數組
 3  *
 4  * */
 5 
 6 package tools;
 7 
 8 
 9 public class Operate {
10     
11     //初始化二維數組
12     public static String[][] init() {
13         
14         String[][] temp = new String[9][9];
15         for( int i = 0; i < 9; i++ ) {
16             
17             for( int j = 0; j < 9; j++ ) {
18                 if( j <= i ) {                //保存乘法表內容
19                     temp[i][j] = (j + 1) + "*" + (i + 1) + "=" + ((i+1)*(j+1)) + "  ";
20                 }
21                 else {
22                     temp[i][j] = "";        //不需要的地方賦值為:""
23                 }
24             }
25             temp[i][i] += "\n";                //加回車
26         }
27         
28         return temp;            //返回二維數組
29     }
30     
31     
32     //打印數組元素
33     public static void printInfo(String[][] temp) {
34         
35         for( int i = 0; i < temp.length; i++ ) {
36             
37             for( int j = 0; j < temp[i].length; j++ ) {
38                 System.out.print( temp[i][j] );
39             }
40         }
41     }
42     
43 }

 

 1 /*
 2  * Description:通過二維數組打印九九乘法表
 3  * 
 4  * Written By:Cai
 5  * 
 6  * Date Written;
 7  * 
 8  * */
 9 
10 package main;
11 
12 import tools.Operate;
13 
14 public class DemoTwo2 {
15 
16     public static void main(String args[]) {
17         
18         String[][] temp = Operate.init();        //初始化二維數組
19         Operate.printInfo(temp);                //打印數組
20         
21     }
22 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM