print pattern in JAVA

2006-11-01 2:06 pm
By JAVA program, how to print outout the following pattern by for loop?

*
***
***********
***
*

回答 (2)

2006-11-05 12:37 am
✔ 最佳答案
public class Test
{
 public static void main( String[] args )
 {
  int[] dataArray = { 1, 3, 11, 3, 1 };

  for ( int i = 0 ; i < dataArray.length ; i++ )
  {
   // get the number of start at line "i"
   int numberOfStart = dataArray[ i ];

   // print a line of star
   for ( int j = 0 ; j < numberOfStart ; j++ )
   {
    System.out.print("*");
   }

   // go to new line
   System.out.println();
  }
 }
}
2006-11-01 5:34 pm
try following code:

import java.io.*;
import java.lang.*;

public class PrintStar
{
public static void main( String[ ] args )
{
int[ ] nPrint = new int[ 5 ];
String sPrint = new String( );

nPrint[ 0 ] = 1;
nPrint[ 1 ] = 3;
nPrint[ 2 ] = 11;
nPrint[ 3 ] = 3;
nPrint[ 4 ] = 1;

for ( int i = 0; i < 5; i++ )
{ sPrint = new String( );
for ( int j = 0; j < nPrint[ i ]; j++ )
sPrint = sPrint + “*”;
System.out.println( sPrint );
}
}
}


收錄日期: 2021-05-03 05:44:15
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061101000051KK00459

檢視 Wayback Machine 備份