Java--学习九九乘法表

class For{
	public static void main(String[] args){
		/*
			 *****
			 ****
			 ***
			 **
		 	 *
		*/
		//错误代码:
		
		int z=5;
		for(int x=0;x<5;x++)//外部for循环决定行数,内部for循环决定列数。
			{
				for(int y=0;y<z;y++)
					{
						System.out.print ("*");
						z--;
					}
			System.out.println ();
			}
		System.out.println("-----------------");
		/*
			 错误!!
			 结果为:
				 ***
				 *
				 *
				 (空格)
				 (空格)
			流程:第一次循环,输出***
				  第二次循环,z=2,输出*
				  第三次循环,z=1,输出*
				  第四、五次循环,z=0,均输出 换行
		
		*/	
	    //正确代码_1
	    int z=5;
		for(int x=0;x<5;x++)
			{
				for(int y=0;y<z;y++)
					{
						System.out.print ("*");
					}
				System.out.println ();
				z--;
			}
		System.out.println("-----------------");
		//正确代码_2
		int z=0;						//int z=0;(省略)
		for(int x=0;x<5;x++)
		{
			for(int y=z;y<5;y++)		//for(int y=x;y<5;y++)因为z=0,x=0;z=1,x=1;
			{
				System.out.print("*");
			}
			System.out.println();
			z++;						//z++(省略)
		}
		
		/*
			 *
			 **
			 ***
			 ****
			 *****
		*/
		for(int x=0;x<5;x++)
		{
			for(int y=0;y<=x;y++)
			{
				System.out.print ("*");
			}
			System.out.println ();
		}
		System.out.println("-----------------");
		//尖朝上记住改变条件即可。如果y<x,就是0<0,结果第一行是换行。所以,应该是y<=x就靠谱了.
		
		
		
		//九九乘法表
		
		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 ();
			
		}
		System.out.println("-----------------");
	}
}
会编译出错,因为多次定义了z,但内部的程序都是正确的……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值