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,但内部的程序都是正确的……
Java--学习九九乘法表
最新推荐文章于 2024-05-04 23:55:47 发布