The solution is simple... as below
class printStars {
public static void main(String args[]) {
String strString = "";
for(int i=0; i< 5;i++)
{
strString = "";
for(int j=0;j<=i;j++)
{
strString+="*";
}
System.out.println(strString);
}
}
}
> The solution is simple... as below
And what did the OP learn from this?
> class printStars {
> public static void main(String args[]) {
>String strString = "";
> for(int i=0; i< 5;i++)
>{
>strString = "";
>for(int j=0;j<=i;j++)
>{
>strString+="*";
> }
>System.out.println(strString);
> }
> }
It's a bad example: you use magic numbers and you don't use Java's code conventions.
Hi prometheuzz,
Please let me know if the below code looks better. If you see any standards not being followed, could you please let me know?
import java.lang.*;
public class PrintStars {
public static void main(String args[]) {
PrintMyStar objPrntMyStar = new PrintMyStar(5,"*");
System.out.println(objPrntMyStar.prepareOutput());
}
}
class PrintMyStar {
private StringBuffer strStarBuff;
private int iRows;
private String strToPrnt;
static final int DEF_ROWS = 5; //Default number of rows
static final String STAR = "*";
static final String NEW_LINE = "\n";
/*
Default constructor
*/
PrintMyStar()
{
strStarBuff = new StringBuffer();
iRows = DEF_ROWS;
strToPrnt = STAR;
}
/*
Constructor only to initialize number of rows
*/
PrintMyStar(int iNoOfRows)
{
strStarBuff = new StringBuffer();
iRows = iNoOfRows;
strToPrnt = STAR;
}
/*
Constructor to initialize number of rows & string to print
*/
PrintMyStar(int iNoOfRows, String strToPrnt)
{
strStarBuff = new StringBuffer();
iRows = iNoOfRows;
this.strToPrnt = strToPrnt;
}
public String prepareOutput()
{
for(int i=0; i<iRows;i++)
{
for(int j=0;j<=i;j++)
{
strStarBuff = strStarBuff.append(strToPrnt);
}
strStarBuff.append(NEW_LINE);
}
return strStarBuff.toString();
}
}
>