Java打印乘法表


今天一個同學問我怎樣打印一個乘法表,想了一下,不算難,貼代碼:

import java.util.Scanner;
public class Chengfa 
{
    public static void main(String[] args) 
    {
        System.out.println("請輸入1到10之間的一個數字:");
        try{
            Scanner sc=new Scanner(System.in);
            int num=Integer.parseInt(sc.next());
            if((num>0)&&(num<11)){
                System.out.println("打印乘法表:");
                printTable(num);
            }
            else 
                System.out.println("input error");
        }catch(Exception e){
            System.out.println("您輸入的數字有誤");            
        }
        
    }
    public static void printTable(int maxNum){
        for(int i=1;i<=maxNum;i++){
            for(int j=1;j<=i;j++){                
                    System.out.print(j+"*"+i+"="+i*j+" ");            
                    if(i==j)
                        System.out.println("\n");
            }
        }
    }
}

下面是運行結果圖:


免責聲明!

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



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