2017-07-22 6 views
-4

Ich habe Probleme mit verschachtelten Schleife, um ein Muster zu drucken. Während die meisten Tutorials eine Pyramide Mine zeigen, ist komplexer.Java verschachtelte Schleifenmuster

ich brauche die folgenden, basierend auf Benutzereingabe für die Anzahl der Zeilen, zum Beispiel zu drucken:

2 Reihen:

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

4 Zeilen:

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

Dies ist, was ich bisher versucht habe:

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 

    } 
} 

Ausgang:

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

Ausgabe druckt nicht die Pyramide. Wie kann ich meinen Code reparieren, um das erwartete Ergebnis zu erhalten? Vorschläge sind willkommen.

+0

in Ihrer 4 Zeile Ausgabe ist der 2. Reihe länger als Ihre zweite Zeile in Ihrem 2-Zeilen-Beispiel ist das wirklich, was Sie meinen? – bhspencer

+0

Ahh ich vermasselt das, sollte wie die 2 Zeile aussehen, aber alle in der Linie, krank versuchen zu bearbeiten. –

+0

Bitte erläutern Sie, welche Probleme genau Sie bekommen, einschließlich der falschen Ausgabe oder Fehler Stacktrace erhalten Sie. – bcsb1001

Antwort

0

Sie sollten die erste Hälfte mit dem '//' Symbol und die andere Hälfte mit '\.' Symbol, für tun, dass Sie einige für Schleifen benötigen, überprüfen Sie das Beispiel mit der Erklärung:

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

    } 
} 

Ausgang:

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

AKTUALISIERT

Für die "o" inverse :

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

    } 
} 

Output:

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

Ehrfürchtig, also, wenn ich das "O" Gegenteil wollte, was würden Sie vorschlagen? Ich muss auf string.append nachlesen. Ich habe das noch nicht in der Klasse behandelt. –

+0

Überprüfen Sie das Update – elmigue017

+0

Wow, Danke .... wie geht es euch so schnell. Ich habe 12 Stunden gespielt und es nie bekommen. –

-1

Dies sollte tun, was Sie wollen. diese

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

Dank Krishnan Mahadevan, seine Hälfte von dem, was ich brauche. Ich werde versuchen, den Rest basierend auf Ihrem Beispiel anzupassen. –

0

Versuchen:

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

Diese Ausgänge:

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

Hoffe, es hilft!

+0

Firoz Memon, Danke. Es ist genau das, was ich brauche, um den Rest fertigzustellen. –

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

Test: input = 4

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

Test: input = 10

o   //\\. 
o  // \\. 
o  // \\. 
o  //  \\. 
o  //  \\. 
o //   \\. 
o //   \\. 
o //    \\. 
o //    \\. 
o//     \\. 
Verwandte Themen