从java教科书中绘制图形

时间:2015-08-28 12:13:06

标签: java draw figures

我正在研究Reges,Stuart和Martin Stepp的第2章自我检查问题。构建Java程序:回归基础方法。我试图得到下面的输出与我的代码。我正在尝试确定if(empty($x)){line!\的关系以及计算/所需的数学运算。这不是家庭作业,我也不需要答案,方向或指导就是我所寻求的。

for loops

截至目前我的代码是:

!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////

4 个答案:

答案 0 :(得分:1)

试试这个:(我手头没有编译器)

for (int line = 1; line <= 6; line++){
  for(int i = 1; i<=line-1; i++) {
    System.out.print("\\");
  }
  for (int i = 1; i <= 22 - 4*(line-1); i++){
    System.out.print("!");
  }
  for(int i = 1; i<=line-1; i++) {
    System.out.print("//");
  }
  System.out.println();
}

如果您无法理解任何内容,请发表评论。所有的耳朵。

答案 1 :(得分:1)

这一切都取决于您要打印的行数。 如果您有x行(此处为6),则可以打印出您想要的内容,如下所示:

int lines = 6;
for (int i = lines; i > 0; i--) { //start from the top line (6), finish at the lowest line (1)
    //print backslashes
    for (int back = 0; back < (lines-i)*2; back++) { //lines-i is the difference from the top line. add two extra slashes at each new line
        System.out.print("\\");
    }

    //print !s
    for (int up = 0; up < (i*4)-2; up++) {
        System.out.print("!");
    }

    //print slashes (as many as the backslashes)
    for (int forw = 0; forw < (lines-i)*2; forw++) {
        System.out.print("/");
    } 
    System.out.println();            
}

如果总是想要6行,那么只需跳过int lines = 6;语句,并将lines替换为6

因此,在第一行,您可以打印4*x-2&#39;以及0&#39;和&#39; /&#39; s 。
在第二行,您可以打印少4个&#39;以及另外2个&#39;和/&#39; s。
...
在最后一行,您可以打印2&#39;和(x-1)*2&#39;和&#39;&#39; s。

通常情况下,当您获得x行时,您正在寻找的关系如下:

  

在第i行,从1(最低)到x(顶部),打印:
  &#39; \&#39;:(x-i)* 2次
  &#39;!&#39;:(i * 4)-2次
  &#39; /&#39;:(x-i)* 2次

答案 2 :(得分:0)

答案直接在这条评论中表示&#34; table&#34;在你的代码中,你写了符号的计数。 for循环可以在任何步长中增加和减少

表示(int i = 22; i> 2; i = i-4),例如

希望这不是很多。

答案 3 :(得分:0)

/**
 * Created on 8/28/15.
 * Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
 * Chapter 2 Self-Check Problems Question 34 & 35
 */
public class Ch2_SelfCheckProblems {

    public static final int SIZE = 4;

    public static void main(String[] args) {
        System.out.println("Practice:");
        question34();
        System.out.println("Partially correct:");
        q34();
        System.out.println("Solution, scalable with constant:");
        solution34();

    }

    /**
     * Table 6 lines; CONSTANT = 6
     * Line 1   ! = 22  \ = 0   / = 0
     * Line 2   ! = 18  \ = 2   / = 2
     * Line 3   ! = 14  \ = 4   / = 4
     * Line 4   ! = 10  \ = 6   / = 6
     * Line 5   ! = 6   \ = 8   / = 8
     * Line 6   ! = 2   \ = 10  / = 10
     *
     * Table 4 lines; CONSTANT = 4
     * Line 1   ! = 14  \ = 0   / = 0
     * Line 2   ! = 10  \ = 2   / = 2
     * Line 3   ! = 6   \ = 4   / = 4
     * Line 4   ! = 2   \ = 6   / = 6
     */

    public static void  question34(){

        for (int line = 1; line <= 6; line++){
            for (int i = 1; i <= (22-2*(line-1)); i++){
//                for (int j = 1; j <= (line - 1); j++){
//                    System.out.print("\");
//                }
                System.out.print("!");
            }
            System.out.println();
        }
    }

    public static void q34(){
        for (int line = 1; line <= 6; line++){
            for(int i = 1; i<=line-1; i++) {
                System.out.print("\\\\");
            }
            for (int i = 1; i <= 22 -(4*(line-1)); i++){
                System.out.print("!");
            }
            for(int i = 1; i<=line-1; i++) {
                System.out.print("//");
            }
            System.out.println();
        }
    }

    public static void solution34(){
        for (int line = 1; line <= SIZE; line++){
            for(int i = 1; i<=((2 * line) - 2); i++){
                System.out.print("\\");
            }
            for (int i = 1; i <= ( -4 * line + ( 4 * SIZE + 2 ) ); i++){
                System.out.print("!");
            }
            for(int i = 1; i<= ((2 * line) - 2); i++){
                System.out.print("/");
            }
            System.out.println();
        }
    }
}