1

我正在研究 Reges、Stuart 和 Martin Stepp 的第 2 章自检问题。构建 Java 程序:回归基础的方法。我试图得到下面的输出与我的代码。我正在尝试确定to toline关系!以及计算. 这不是家庭作业,我也不需要答案,方向或指导是我所寻求的。\/for loops

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

我现在的代码是:

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



}
4

4 回答 4

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

如果您有任何不明白的地方,请发表评论。所有人的耳朵。

于 2015-08-28T12:28:05.000 回答
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;语句并替换lines6到处。


所以,在第一行,你打印4*x-2'!'s 和 0 '\'s 和 '/'s。
在第二行,您打印 4 少 '!' 和 2 更多 '\' 和 '/'。
...
在最后一行打印 2 个 '!'s 和(x-1)*2'\'s 和 '/'s。


一般来说,当您获得x线路时,您正在寻找的关系如下:

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

于 2015-08-28T12:49:16.593 回答
0

答案直接在此注释中,该注释在您的代码中写着“表格”,您在其中编写了符号的计数。for 循环能够以任何步长增加和减少

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

希望这不是太多。

于 2015-08-28T12:22:19.387 回答
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();
        }
    }
}
于 2015-08-29T10:47:33.470 回答