一个小程序,大家看看
我写的程序想打出一个倒三角形,但为什么出来结果是一条直线呢。
public class PrintTriag{
public static void main(String[]args)
{ int initLine=10;
int initNum=10;
PrintTriag pt = new PrintTriag();
for(int i = 0; i < initLine; i++)
for(int j = 0; j < initNum-i; j++)
{ pt.printAstar();
}
System.out.print( "\n ");
}
public void printAstar()
{ System.out.print( "* ");
}
}
------解决方案--------------------加个括号
for(int i = 0; i < initLine; i++)
{
for(int j = 0; j < initNum-i; j++)
{ pt.printAstar();
}
System.out.print( "\n ");
}
------解决方案--------------------public class PrintTriag{
public static void main(String[]args)
{ int initLine=10;
int initNum=10;
PrintTriag pt = new PrintTriag();
for(int i = 0; i < initLine; i++)
{
for(int j = 0; j < initNum-i; j++)
{ pt.printAstar();
}
System.out.print( "\n ");
}
}
public void printAstar()
{ System.out.print( "* ");
}
}
------解决方案--------------------public class PrintTriag{
public static void main(String[]args)
{ int initLine=10;
int initNum=10;
PrintTriag pt = new PrintTriag();
for(int i = 0; i < initLine; i++)
{
for(int j = 0; j < initNum-i; j++)
{ pt.printAstar();
}
System.out.print( "\n ");
}
}
public void printAstar()
{ System.out.print( "* ");
}
}
------解决方案--------------------看看这是你要的吗?
public class PrintTriag {
public static void main(String[] args) {
int initLine = 11;
int initNum = 11;
PrintTriag pt = new PrintTriag();
for (int i = 0; i < initLine; i += 2) {
for(int k = 0; k < i/2; k ++)
System.out.print( " ");
for (int j = 0; j < initNum - i; j++) {
pt.printAstar();
}
System.out.print( "\n ");
}
}
public void printAstar() {
System.out.print( "* ");
}
}
------解决方案--------------------这个怎么让它在控制台 直观的看到呢?