2012-10-08 7 views
-2

을 만드는 자바 루프 ...을 위해 나는 다음과 같은 출력을 인쇄 루프 중첩을 쓸 생각 해요 :은 피라미드

    1 
       1 2 1 
      1 2 4 2 1 
      1 2 4 8 4 2 1 
     1 2 4 8 16 8 4 2 1 
    1 2 4 8 16 32 16 8 4 2 1 
1 2 4 8 16 32 64 32 16 8 4 2 1 

가 나는 두 가지 방법을 사용하기로되어 있어요.

주 방법은 사용자가 원하는 행 수를 얻는 것으로 가정합니다.

  1. 인쇄 행의 수
  2. 반환 아무것도 피라미드를 행 수 있습니다 가 나는 것을 printPyramid라는 다른 방법을 쓰기로되어 있어요.

지금까지 내가 가진 :

import java.util.Scanner; 

    public class Pyramid { 


    //Getting number of rows, calling printPyramid in main method 

    public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    //Get user input = number of lines to print in a pyramid 
    System.out.println("Enter the number of lines to produce: "); 
    int numLines = input.nextInt(); 

    //call method: 
    printPyramid(); 

    }//End of main method 

    //Making a new method... 

    public static void printPyramid (int numLines) { 

    int row; 
    int col; 

    row = 0; 
    col = 0; 

     for (row = 1; row <= numLines; row++){ 
      //print out n-row # of spaces 
      for (col = 1; col <= numLines-row; col++){ 
       System.out.print(" "); 
      } 

      //print out digits = 2*row-1 # of digits printed 
      for (int dig= 1; dig <= 2*row-1; dig++){ 
       System.out.print(row + " "); 
      } 

     System.out.println(); 
     }//end of main for loop 

    }//end of printPyramid 

    }//end of class 

나는 오류를 얻을 내가 제대로 인쇄 할 얻는 방법을 알아낼 수 없습니다. 나는 그 방법이 엉망이라고 믿는다.

+2

오류를 게시 할 수 있습니까? 당신이 그것에 대해 아무것도 모르는 경우 오류를 수정하기가 아주 어렵습니다. ** 및 **이 숙제입니까? – elyashiv

+1

* 오류가 발생했습니다 ... * 어떤 오류가 있습니까? – Pigueiras

+0

질문을 세분화하여 관련 기능 코드/디버깅 오류가있는 단일 기능/구획을 표시하고 – Ozzy

답변

-1

두 가지 큰 오류가 있습니다. 첫째, ALL (Java 포함)은 클래스이므로 클래스 내에 메인 메소드를 넣어야합니다. 예를 들어 :

public class Anything { 
    public static void main ... 

    public static void printPyramid ... 
} 

두 번째, 당신은 먼저 호출되지 않은 경우는 실행되지 않기 때문에, 주요 내부의 방법 printPyramid를 호출해야합니다.

public class Anything { 
    public static void main ... { 
     ... 
     printPyramid (numLines); 
     ... 
    } 

    public static void printPyramid ... 
} 

이 작은 표시가 도움이되기를 바랍니다.

+0

정말 고마워요! 오류 :;' 'printPyramid를(); 클래스 피라미드의 방법 printPyramid 주어진 유형에 적용 할 수 없습니다' 가'필요 : 나는 'Pyramid.java:28가 ... 단 1 오류로 내려 가지고 int' 'found : no arguments' 'reason : 실제 및 공식 인수 목록의 길이가 다릅니다' – user1368970

+0

@ user1368970 그냥 예제 일 뿐이므로 매개 변수로 int를 사용하여'printPyramid'를 호출해야합니다. 에러가 여러분에게 설명합니다 :'required : int found : no arguments' – Pigueiras