清晨小练习-函数及重载运用

九九乘法表实现
本文介绍了一种使用Java编程语言实现九九乘法表的方法,并提供了三种不同的函数来生成默认的九九乘法表、用户自定义范围的乘法表及特定区间内的乘法表。

默认输出99乘法表与用户自定义乘法表

class table99{
	public static void table99(){
		for(int x=1;x<=9;x++){			//默认九九乘法表
			for(int y=1;y<=x;y++){
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
		System.out.println();
		}
	}
	public static void table99(int a){
		for(int x=1;x<=a;x++){			//自定义1-a乘法表
			for(int y=1;y<=x;y++){		
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
		System.out.println();
		}
	}
	public static void table99(int a,int b){
		for(int x=1;x<=b;x++){			//自定义a-b乘法表
			for(int y=a;y<=x;y++){
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
		}
	}
	public static void main(String[] args){
		table99();
		System.out.println();
		table99(8);
		System.out.println();
		table99(3,8);
	}
}

尝试优化函数

class table99{
	public static void table99(){			//默认九九乘法表
		table99(1,9);
	}
	public static void table99(int b){		//自定义1-b乘法表
		table99(1,b);
	}
	public static void table99(int a,int b){
		for(int x=1;x<=b;x++){			//自定义a-b乘法表
			for(int y=a;y<=x;y++){
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
			System.out.println();
		}
	}
	public static void main(String[] args){
		table99();
		System.out.println();
		table99(8);
		System.out.println();
		table99(3,8);
	}
}

 

转载于:https://my.oschina.net/Almon/blog/704477

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值