Java basic grammar-print 99 multiplication table

code show as below:

public class PrintMultiplication{
    
    
	public static void main(String[] args){
    
    
		for(int i = 1; i<=9; i++){
    
    
			for(int j = 1; j<=i; j++){
    
    
				System.out.print(j+"*"+i+"="+j*i+" ");	
			}
		System.out.println();
		}
	}
}

Misalignment will appear after printing:
99 multiplication table-try1
Change the space in System.out.print(j+" "+i+"="+j i+" "); to \t, \ is the escape character, and t represents the tab key:
99 multiplication table-try2
Code improvements:

import java.util.Scanner;

public class PrintMultiplication{
    
    
	public static void main(String[] args){
    
    
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		print99(n);
	}
	
	public static void print99(int n){
    
    
		for(int i = 1; i<=n; i++){
    
    
			for(int j = 1; j<=i; j++){
    
    
				System.out.print(j+"*"+i+"="+j*i+"\t");	
			}
		System.out.println();
		}
	}
}

Implement method call, implement manual input of cut-off number:
99 multiplication table-try3

Guess you like

Origin blog.csdn.net/xiaonuanhu/article/details/108575929
Recommended