当前位置: 动力学知识库 > 问答 > 编程问答 >

How to use for-loop in Java to print out an image that made by symbols?

问题描述:

I got stuck on making the symbols at the end of the second line using for-loop,I had tried couple times and still not work. Can someone tell me where I did wrong?

 ////////////////\\\\\\\\\\\\\\\\

////////////********

////////****************

////************************

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

And it suppose to be look like this:

 ////////////////\\\\\\\\\\\\\\\\

////////////********\\\\\\\\\\\\

////////****************\\\\\\\\

////************************\\\\

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

Here is my code:

public static void main (String [] args){

for (int lineOne=1; lineOne<=16; lineOne += 2){

System.out.print("//");

}

for (int lineOne=1; lineOne<=32; lineOne += 2){

System.out.print("\\");

}

System.out.println("");

for (int a=8; a<=32; a += 8)

{

for (int b=12; b < (28 - a / 2); b +=2)

{

System.out.print("//");

}

for (int c=0; c<a; c++)

{

System.out.print("*");

}

System.out.println("");

}

}

网友答案:

It seems that you just did not print it at all.

It should work fine:

    for (int lineOne=1; lineOne<=16; lineOne += 2){
    System.out.print("//");
                                                 }
    for (int lineOne1=1; lineOne1<=32; lineOne1 += 2){
    System.out.print("\\");
       }
    System.out.println("");
    for (int a=8; a<=32; a += 8)
      {
    for (int b=12; b < 28-a/2; b +=2)
    {
        System.out.print("//");
    }
    for (int c=0; c<a; c++)
    {
        System.out.print("*");

    }

    // this prints your "\\"
    for(int k = 0;k<16-a/2;k++) 
    {
     System.out.print("\\");   
    }

    System.out.println(" ");

}   
网友答案:

Firstly, "\\" is actually just a single backslash, escaped. That's why you had to print 32 "\\"s to balance out the 16 "//"s. You probably meant to print out 16 "\\\\"s.

Secondly you had no code to print backslashes after the first line. So I suggest you add some code to handle this.

Thirdly your source code was a mix of tabs and space indentation. Which may have confused you when reviewing the logic flow of your own program. Consistent indenting might really assist you here.

分享给朋友:
您可能感兴趣的文章:
随机阅读: