2014-04-20 52 views
0

我有以下應該運行的程序,但它不是。教授給了我們合作。它旨在計算階乘。它甚至不會讓我編譯。錯誤我正在狀態遞歸程序中的錯誤

 
    Multiple markers at this line 
    - Syntax error on token "Invalid Character", invalid Expression 
    - Syntax error on tokens, delete these tokens 

這是在參考了以下行:

System.out.print (「%d! = %d\n」, counter, factorial (counter)); 

我該如何解決這個問題?我之前編寫過很多程序,但之前我從來沒有在引號內看到過模運算符。我有點困惑!整個計劃在下面發佈!謝謝!

public class FactorialTest 
{ 
    // calculate the factorial of 0 – 15 
    public static void main (String args[]) 
    { 
     FactorialCalculation factorialCalculation = new FactorialCalculation(); 
     factorialCalculation.displayFactorials(); 
    } // end of main 
} // end of the class FactorialTest 


public class FactorialCalculation 
{ 
    //recursive Factorial method 
    public long factorial(long number) 
    { 
     if (number <= 1) 
      return 1; 
     else 
      return number * factorial (number - 1); 
    } 
    //Now output the factorials of 0 through 15 
    public void displayFactorials() 
    { 
     // Calculate the factorial of o through 15 
     for (int counter = 0; counter <= 10; counter++) 
      System.out.print (「%d! = %d\n」, counter, factorial (counter)); 
    } // end of the method displayFactorials 
} // end of class FactorialCalculation 
+0

不要使用彎引號。 –

回答

1

你有這樣的:

「%d! = %d\n」 

的「和」花式引號字符不是有效的Java引號字符。使用一個普通的老「來代替。

如果你的編輯器將自動爲您創建花哨的報價,要麼禁用或找到一個更合適的編輯器。

+0

哇!我沒有意識到是這樣。我剛剛從單詞複製粘貼,因此問題!謝謝!現在它說這個錯誤,但是, 「PrintStream類型中的方法print(int)不適用於參數(String,int,long)」 我該如何解決這個問題?謝謝! – user3294617

+0

我真正想知道的是,這實際上是在做什麼,「%d! =%d \ n「?沒事吧? – user3294617

+0

@ user3294617你可能意指['printf'](http://bit.ly/PiHIsb)。不過,如果有疑問,請查看文檔。 'printf'文檔將解釋「%d」的含義。 –

0

您需要更換fancy curly braces,你也需要格式化字符串使用printf讓你期待的輸出

public class FactorialTest { 
    // calculate the factorial of 0 – 15 
    public static void main(String args[]) { 
     FactorialCalculation factorialCalculation = new FactorialCalculation(); 
     factorialCalculation.displayFactorials(); 
    } // end of main 
} // end of the class FactorialTest 


class FactorialCalculation { 
    //recursive Factorial method 
    public long factorial(long number) { 
     if (number <= 1) 
      return 1; 
     else 
      return number * factorial(number - 1); 
    } 

    //Now output the factorials of 0 through 15 
    public void displayFactorials() { 
     // Calculate the factorial of o through 15 
     for (int counter = 0; counter <= 10; counter++) { 
      System.out.printf("%s! = %s\n", counter, factorial(counter)); 
     } 
    } // end of the method displayFactorials 
} 

注意:你不需要定義爲publicFactorialCalculation此限制還沒有被編譯器執行,雖然它對於有效的包裝輸入是必需的。

我們只能有一個頂級公衆無論在任何 Java編譯單元類或接口(.java源文件)