2013年11月21日星期四

for loop to print the triangle problem

 This post last edited by the jiangjiewudi on 2013-11-21 14:29:17

**
******
**********
**************
*****************
java print as shown in the graph ( two triangles connected ) , seeking answers. Really can not think of a solution. . .
------ Solution ---------------------------------------- ----
2 of their own change it not to end up
public class Test{ 
public static void main(String[] args) { 
printDengYao(5,9);//打印等腰三角形 

public static void printDengYao(int yao,int di){ 
if(di%2==0){
di+=1;

for(int i=0;i<yao;i++){
 
for(int j = 0;j<di/2-i;j++){ 
System.out.print(" "); 
}

for(int k=0;k<=2*i;k++){ 
System.out.print("*"); 

  for(int n = 0;n<2*(di/2-i);n++){ 
System.out.print(" "); 
}
for(int m=0;m<=2*i;m++){ 
System.out.print("*"); 

System.out.println();

}
}

------ Solution ------------------------------------- -------

        int h = 5;//高度
        h--;
        int l = 4 * h;
        for (int i = 0; i <= h; i++) {
            for (int j = 0; j <= l; j++) {
                if (j >= h - i && j <= h + i || j >= 3 * h - i && j <= 3 * h + i) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

------ For reference only ------------------------- --------------
these problems can be attributed to two-dimensional array of issues
------ For reference only --------- ------------------------------
Thank you for reminding !
------ For reference only -------------------------------------- -
public class Test{ 
public static void main(String[] args) { 
printDengYao(5,9);//打印等腰三角形 

public static void printDengYao(int yao,int di){ 
if(di%2==0){
di+=1;

 
for(int i=0;i<yao;i++){ 
for(int j = 0;j<di/2-i;j++){ 
System.out.print(" "); 
}
for(int k=0;k<=2*i;k++){ 
System.out.print("*"); 

System.out.println();

}
}

------ For reference only ------------------------------- --------


The two triangles , not one. . .
------ For reference only -------------------------------------- -
two-dimensional array how to write ?
------ For reference only -------------------------------------- -
two triangles connected to the corner is a coincidence , how to write ?
------ For reference only -------------------------------------- -
two triangles connected to the corner is a coincidence , how to write ?  

What do you mean ? What angle coincidence ? me are ok ah

------ For reference only ----- ----------------------------------
two triangles connected to the corner is a coincidence , this how write ?          
  
What do you mean ? What angle coincidence ? me are ok ah   
        is the lower right triangle to the left of the " *" and the right of the lower left corner of the triangle "*" share a .
------ For reference only -------------------------------------- -
upstairs can ah. What coincidence ?
------ For reference only -------------------------------------- -

public class Test{ 
public static void main(String[] args) { 
printDengYao(5,9);//打印等腰三角形 

public static void printDengYao(int yao,int di){ 
if(di%2==0){
di+=1;

for(int i=0;i<yao;i++){
 
for(int j = 0;j<di/2-i;j++){ 
System.out.print(" "); 
}

for(int k=0;k<=2*i;k++){ 
System.out.print("*"); 

  for(int n = 0;n<2*(di/2-i)-1;n++){ 
System.out.print(" "); 
}
if(i<yao-1)
{
for(int m=0;m<=2*i;m++){
System.out.print("*");
}
}
  else
  {
  for(int o=0;o<=2*i-1;o++){
System.out.print("*");
}
  }
System.out.println();

}
}


carefully counted before we know what you mean, you want a second triangle left one , plus a judgment on o it. . .

没有评论:

发表评论