2017-07-22 156 views
-4

我有嵌套循環打印模式的問題。雖然大多數教程顯示金字塔礦山更復雜。java嵌套循環模式

我需要打印基於用戶輸入的行的#,例如以下:

2行:

o //\\. 
o// \\. 

4行:

o //\\. 
o // \\. 
o // \\. 
o//  \\. 

這是我到目前爲止所嘗試的:

import java.util.Scanner; 
public class Homework { 

    public static void main(String[] args) { 
     // int size = 2; 
     int noOfRows = 3; 

     //Scanner sc = new Scanner(System.in); 

     System.out.println("How Many Rows You Want In Your Pyramid?"); 

     // int noOfRows = sc.nextInt(); 

     System.out.println("Here Is Your Pyramid"); 

     for (int i = 1; i <= noOfRows; i++) { 
      System.out.print("o"); 
      //Printing i*2 spaces at the beginning of each row 

      for (int j = noOfRows * 2 + 1; j >= 1; j--) { 
       System.out.print(" "); 
      } 

      //Printing j value where j value will be from 1 to rowCount 

      for (int j = noOfRows + 1; j >= 1; j--) { 
       System.out.print("//"); 
      } 

      //Printing j value where j value will be from rowCount-1 to 1 
      for (int j = noOfRows + 2; j <= noOfRows; j++) { 
       System.out.print("\\" + "\\" + "."); 
      } 

      System.out.println(); 

      //Incrementing the rowCount 

     } 
     System.out.println(); // NEWLINE 

    } 
} 

輸出:

How Many Rows You Want In Your Pyramid? 
Here Is Your Pyramid 
o  //////// 
o  //////// 
o  //////// 

輸出不打印金字塔。我如何修復我的代碼以獲得預期的結果?歡迎任何建議。

+0

您的第二行是長於你的第二排例子中的第二排是你真正的意思嗎? – bhspencer

+0

啊,我搞砸了,應該看起來像2行,但都排隊,生病嘗試編輯。 –

+0

請解釋你正在得到什麼「問題」,包括你得到的錯誤輸出或錯誤堆棧跟蹤。 – bcsb1001

回答

0

你應該用'//'符號打印上半部分,用'\'打印另一半。符號,爲做到這一點,你需要一些for循環,檢查與解釋的例子:

public static void main(String[] args) { 
    // Number of rows 
    int nRows = 4; 

    for (int i = 0; i <= nRows; i++) { 
     StringBuilder string = new StringBuilder(); 
     string.append("o"); 

     for (int k = i; k <= nRows; k++) { 
      // Adds a white space before the first '//' symbol 
      string.append(" "); 
     } 

     // Adds the '//' symbol 
     string.append("//"); 

     int z = (i + i)/2; 

     for (int j = 1; j <= z; j++) { 
      // Adds the first half of white space between the '//' and '\\' 
      string.append(" "); 
     } 

     for (int j = 1; j <= z; j++) { 
      // Adds the second half of white space between the '//' and '\\' 
      string.append(" "); 
     } 

     // Adds the '\\\\.' symbol 
     string.append("\\\\."); 

     // Prints the string 
     System.out.println(string); 

    } 
} 

輸出:

o  //\\. 
o // \\. 
o // \\. 
o //  \\. 
o //  \\. 

修訂

對於 「O」 逆:

public static void main(String[] args) { 
    // Number of rows 
    int nRows = 4; 

    for (int i = 0; i <= nRows; i++) { 
     StringBuilder string = new StringBuilder(); 

     for (int k = i; k <= nRows; k++) { 
      // Prints a white space before the first '//' symbol 
      string.append(" "); 
     } 

     // Prints the '//' symbol 
     string.append("//"); 

     int z = (i + i)/2; 

     for (int j = 1; j <= z; j++) { 
      // Prints the first half of white space between the '//' and '\\' 
      string.append(" "); 
     } 

     for (int j = 1; j <= z; j++) { 
      // Prints the second half of white space between the '//' and '\\' 
      string.append(" "); 
     } 

     // Prints the '\\\\.' symbol 
     string.append("\\\\."); 


     for (int k = i; k <= nRows; k++) { 
      // Prints a white space before the '\\.' symbol 
      string.append(" "); 
     } 
     // Prints white spaces before the 'o' at the end 
     string.append("o"); 
     System.out.println(string); 

    } 
} 

輸出:

 //\\.  o 
    // \\. o 
    // \\. o 
    //  \\. o 
//  \\. o 
+0

太棒了,所以如果我想要「o」反轉你會有什麼建議?我需要閱讀string.append。還沒有在課堂上講過。 –

+0

檢查更新 – elmigue017

+0

哇,謝謝....你們如何做到這麼快。我花了12個小時玩,從來沒有得到它。 –

-1

這應該做你以後的事情。

public class Homework { 
    public static void main(String[] args) { 
     int noOfRows = 10; 
     System.out.println("How Many Rows You Want In Your Pyramid?"); 
     System.out.println("Here Is Your Pyramid"); 
     for (int i = 1; i <= noOfRows; i++) { 
      StringBuilder string = new StringBuilder(); 
      string.append("o"); 

      for (int k = i; k <= noOfRows; k++) { 
       string.append(" "); 
      } 
      for (int j = 1; j <= i; j++) { 
       string.append("//"); 
      } 

      System.out.println(string); 
     } 
    } 
} 
+0

感謝Krishnan Mahadevan,它是我需要的一半。我會嘗試根據您的示例來調整其餘部分。 –

0

試試這個:

public class MyClass { 
    public static void main(String args[]) { 
     int noOfRows = 4; 
     for (int i = 0; i < noOfRows; i++) { 
      System.out.print("o"); 
      for(int j=0; j< noOfRows; j++) { 
       if(i+j == noOfRows-1) 
        System.out.print("//"); 
       else 
        System.out.print(" "); 
      } 

      // Inversing previous logic 
      for(int j=noOfRows-1; j>= 0; j--) { 
       if(i+j == noOfRows-1) { 
        System.out.print("\\\\."); 
        break; 
       } 
       else 
        System.out.print(" "); 
      } 

      System.out.println(); 
     } 
    } 
} 

此輸出:

o //\\. 
o // \\. 
o // \\. 
o//  \\. 

希望它能幫助!

+0

Firoz Memon,謝謝。正是我需要讓剩下的工作完成。 –

0
public class Pyramid { 
    private static final String leftDesign = "//"; 
    private static final String rightDesign = "\\\\."; 
    private static final String startPointDesign = "o"; 

    private static String getSpace(int input) { 
     String temp = startPointDesign; 
     for (int i=0; i< input-1; i++) { 
     temp += " "; 
     } 
     return temp; 
    } 

    private static String getPyramidDesign(int tempSpace) { 
     String temp = leftDesign; 
     for (int i=0; i < tempSpace; i++) { 
      temp += " "; 
     } 
     temp += rightDesign; 
     return temp; 
    } 

    public static void main(String[] args) { 
     //assign your input 
     int input = 10; 
     int tempStartCount = 0; 
     int tempReverseCount = input; 
     for (int i = 0; i< input; i++) { 
      if(i != 0){ 
       tempStartCount +=2; 
      } 
      System.out.println(String.format("%s%s", getSpace(tempReverseCount), getPyramidDesign(tempStartCount))); 

      tempReverseCount --; 
     } 
    } 
} 

測試:輸入= 4

o //\\. 
o // \\. 
o // \\. 
o//  \\. 

測試:輸入= 10

o   //\\. 
o  // \\. 
o  // \\. 
o  //  \\. 
o  //  \\. 
o //   \\. 
o //   \\. 
o //    \\. 
o //    \\. 
o//     \\. 
在4行輸出示例