2015-08-28 109 views
1

我正在研究Reges,Stuart和Martin Stepp的第2章自檢問題。構建Java程序:迴歸基礎方法。我正在嘗試將下面的輸出與我的代碼進行比較。我試圖識別line!\/的關係以及計算for loops所需的數學。這不是家庭作業,我也不需要答案,我正在尋找方向或指導。從java教科書中繪製圖形

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

我以現在的代碼是:

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



} 
+2

答案:在'line'行你需要畫什麼?多少'''?多少'!'(22 - 2 *(line -1))等等。 –

+0

我的提示:3個內部循環。 – zubergu

+0

@JoopEggen謝謝你,雖然'(22-2 *(line-1))'只減少了'!'兩倍,我需要減少因子4,這導致了最終的結果第6行僅包含'!!'(兩個!) – phillipsK

回答

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

如果你不能不懂,發表評論。全部耳朵。

+0

那麼你的回答是絕對正確的,我個人並不希望增加for循環之外的變量,也沒有在for循環之外定義變量,因爲我還沒有漸進地雖然我知道你的代碼邏輯的工作原理,但我還沒有涉及到,因此試圖遠離這種方法,你是否能夠重寫而不這樣做? – phillipsK

+0

@phillipsK我編輯了答案。 – Saud

+0

System.out.print(「\\\\」); – phillipsK

0

答案是直接在此評論,說「表」,在你的代碼中你寫的countings的符號。 for循環能夠增加,並且在任何步長

爲減少(INT I = 22; I> 2; I = I-4)例如

希望這並不是太大。

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;發言,並與6到處取代lines


所以,在第一行,打印4*x-2 '!' 和0 '\' S和 '/' s。
在第二行打印4個'!'和另外2個'\'和'/'。
...
在最後一行打印2'!'和(x-1)*2'\'和'/'。


在generall,你正在尋找的關係,當你給出x線,如下:

在行i,從1(最低)計數到x(頂部) ,打印:
'\':(ⅹⅰ)* 2倍
'!':(I * 4)-2倍
'/':(ⅹⅰ)* 2倍

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