2017-12-25 3 views
0

Ich möchte Ausgang haben wie dieseAnzeige upside-down Pyramide

5 4 3 2 1 2 3 4 5 
    4 3 2 1 2 3 4 
    3 2 1 2 3 
     2 1 2 
     1 

Wie kann ich einen Platz auf der linken Seite verlassen?

public class Exercise4_17 { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter the number of lines: "); 
     int num = input.nextInt(); 
     Exercise4_17 exe = new Exercise4_17(); 
     exe.displayPyramid(num); 
    } 

    private void displayPyramid(int num) { 

     for (int i = num; i >= 1; i--) { 
      for (int space = 1; space <= num - i; ++space) { 
       System.out.print(" "); 
      } 
      for (int k = num; k >= 1; k--) { 
       System.out.print(k + " "); 
      } 

      for (int j = 2; j <= num; j++) { 
       System.out.print(j + " "); 
      } 
      System.out.println(); 
      num--; 
     } 
    } 
} 

Mein Ausgang

Enter the number of lines: 5 
5 4 3 2 1 2 3 4 5 
4 3 2 1 2 3 4 
3 2 1 2 3 
2 1 2 
1 
+0

einen Debugger verwenden, um herauszufinden, warum Ihre Raum-Druckschleifenräume Druck nicht tatsächlich. – einpoklum

Antwort

2

Ihr Code ist ganz in der Nähe. Erstens, als eine Frage des Stils, würde ich den gleichen Variablennamen für die zweite Schleife (j ist jarring, verwenden Sie einfach k). Zweitens, und das eigentliche Problem, Sie ändern numin Ihre Schleife. Speichern Sie den Wert initial Wert, bevor Sie Schleife und verwenden Sie das für Ihre Speicherplatzberechnung. Wie,

final int initial = num; // <-- add this 
for (int i = num; i >= 1; i--) { 
    for (int space = 1; space <= initial - i; ++space) { // <-- use initial instead of num 
     System.out.print(" "); 
    } 
    for (int k = num; k >= 1; k--) { 
     System.out.print(k + " "); 
    } 
    for (int k = 2; k <= num; k++) { // <-- j should still work of course... 
     System.out.print(k + " "); 
    } 
    System.out.println(); 
    num--; 
} 
+0

Was für ein dummer Fehler! Vielen Dank. –

1

Hoffe das hilft.

import java.util.Scanner; 
public class MainClass { 
    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     //Taking noOfRows value from the user 
     System.out.println("How Many Rows You Want In Your Pyramid?"); 
     int noOfRows = sc.nextInt(); 
     //Initializing rowCount with noOfRows 
     int rowCount = noOfRows; 
     System.out.println("Here Is Your Pyramid"); 
     //Implementing the logic 
     for (int i = 0; i < noOfRows; i++) { 
      //Printing i*2 spaces at the beginning of each row 
      for (int j = 1; j <= i*2; j++) { 
       System.out.print(" "); 
      } 
      //Printing j where j value will be from 1 to rowCount 
      for (int j = rowCount; j >= 1; j--) { 
       System.out.print(j+" "); 
      } 
      //Printing j where j value will be from rowCount-1 to 1 
      for (int j = 2; j <= rowCount; j++) { 
        System.out.print(j+" "); 
      } 
      System.out.println(); 
      //Decrementing the rowCount 
      rowCount--; 
     } 
    } 
} 

ref link

Ausgang: , wie viele Zeilen Sie in Ihrer Pyramide wollen? Hier ist dein Pyramid

5 4 3 2 1 2 3 4 5 

    4 3 2 1 2 3 4 

    3 2 1 2 3 

     2 1 2 

     1