有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从java教科书中绘制图形

我正在研究Reges、Stuart和Martin Stepp的第2章自检问题。构建Java程序:一种回归基础的方法。我试图得到下面的输出与我的代码。我试图确定line!\/的关系,以及计算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();
        }
    }



}

共 (1) 个答案

  1. # 1 楼答案

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