简体   繁体   English

从Java教科书中绘制图形

[英]Draw figure from java textbook

I am working working on Chapter 2 Self-Check Problems from Reges, Stuart, and Martin Stepp. 我正在研究Reges,Stuart和Martin Stepp的第2章“自检问题”。 Building Java Programs: A Back to Basics Approach. 构建Java程序:“回归基础”方法。 I am trying to get the output below vs. my code. 我正在尝试使输出低于我的代码。 I am trying to identify the line to ! 我正在尝试确定line ! to \\ to / relationships and the math needed to compute the for loops . \\ /关系和计算for loops所需的数学。 This is not homework and nor do I need an answer, direction or guidance is what I am seeking. 这不是作业,我也不需要答案,方向或指导。

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

My code as of now is: 到目前为止,我的代码是:

/**
 * Created on 8/28/15.
 * Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach.
 * Chapter 2 Self-Check Problems
 */
public class Ch2_SelfCheckProblems {
    public static void main(String[] args) {
        question34();

    }

    public static void  question34(){
/**
* Table
* 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
*/

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



}

Try this: (I don't have a compiler at hand) 试试这个:(我手边没有编译器)

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();
}

If you can't understand anything, leave a comment. 如果您什么都不懂,请发表评论。 em all ears. 他们全耳。

It all depends on the number of lines that you wish to print. 这完全取决于您要打印的行数。 If you have x lines (here 6), then you can print what you wish, as follows: 如果您有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();            
}

If you always want 6 lines, then just skip the int lines = 6; 如果您总是需要6行,那么只需跳过int lines = 6; statement and replace lines with 6 everywhere. 声明,到处都用6替换lines


So, at the first line, you print 4*x-2 '!'s and 0 '\\'s and '/'s. 因此,在第一行中,您将打印4*x-2 '!'和0'\\'和'/'。
At the second line you print 4 less '!'s and 2 more '\\'s and '/'s. 在第二行,少打印4个“!”,再打印2个“ \\”和“ /”。
... ...
At the last line you print 2 '!'s and (x-1)*2 '\\'s and '/'s. 在最后一行打印2'!'和(x-1)*2 '\\'和'/'。


In generall, the relationship that you are looking for, when you are given x lines, is the following: 通常,在给定x线时,您要查找的关系如下:

At line i , counting from 1 (lowest) to x (top), print: 在第i行,从1(最低)到x (顶部)计数,打印:
'\\': (xi)*2 times '\\':(xi)* 2次
'!': (i*4)-2 times '!':(i * 4)-2次
'/': (xi)*2 times '/':(xi)* 2次

the answer is direct in this comment that says "table" in your code where your wrote the countings for the symbols. 答案是直接在此注释中直接说出来的,该注释在您的代码中写有“ count”的代码中写有“ table”。 for loops are able to increase and decrease in any stepsize for循环能够以任何步长增加和减少

for(int i = 22 ; i>2; i=i-4) for example 例如,for(int i = 22; i> 2; i = i-4)

hope this was not to much. 希望这不会太大。

/**
 * 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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM