2017-07-22 171 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行输出示例