2016-04-13 60 views
-1

這是我的代碼,我不知道我在做什麼嚴重的錯誤。我嘗試了很多不同的東西,比如把public修飾符從get中取走。但我仍然得到同樣的東西。這個程序應該打印出pascal三角形中的第N個數字行,以便我使用遞歸一點。爲什麼javac告訴我非法表達的開始?

import java.util.*; 

public class Triangle{ 

    private int lineNumber, count; 
    private int[] num; 

    public Triangle(){ 

     lineNumber = 1; 
    } 
    public Triangle(int n){ 

     set(n); 
    } 
    public void set(int n){ 

     if(n < 1){ 

     lineNumber = 1; 
     } 
     else{ 

     lineNumber = n; 
     } 
     public int get()//Triangle.java:26: error: ';' expected // 
     { 
     return lineNumber; 
     } 
     private void pascal(int[] row){ //Triangle.java:30: error: illegal start of expression 

     if(count >= lineNumber){ 

      return; 
     } 
     num = new int[row.length + 1]; 
     num[0] = 1; 
     for(int i = 1; i < row.length; i++){ 

      num[i] = row[i - 1] + row[i]; 
     } 
     num[row.length] = 1; 
     count ++; 
     pascal(num); 
     return; 
     } 
     public int[] output(){ 

     count = 1; 
     num = new int[count]; 
     num[0] = 1; 

     pascal(num); 
     return num; 
     } 
     public static void main(String[] args){ 

     int i,userNum; 

     Scanner scnr = new Scanner(System.in); 
     System.out.println("Enter a number to get the nth line of"+ 
          " Pascal's Triangle."); 
     userNum = input.nextInt(); 

     PascalTriangle triangle = new Triangle(userNum); 
     int[] result = triangle.output(); 

     System.out.println("\n Line " + triangle.get() + " of " 
          + "Pascal's Triangle is "); 
     for(i = 0; i < result.length; i++){ 

      System.out.println(result[i] + " "); 
     } 
     } 
    } 
} 
+0

有什麼確切的錯誤,以添加結束}? – Carcigenicate

+0

您對'set(int n)'方法缺少'}'。 – kentaro

+0

您是否也可以發佈錯誤? – denis

回答

0

你需要一個更右括號中集()method.Try else語句前後

​​