我有以下程序应该运行,但事实并非如此。教授给了我们合作。它旨在计算阶乘。它甚至不让我编译。我得到错误的错误
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中不是有效的引号字符。使用普通的旧“而不是。
如果您的编辑器自动为您创建精美的引号,请禁用它或找到更合适的编辑器。
答案 1 :(得分: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
}
注意:您无需为public
课程定义FactorialCalculation
。编译器尚未强制执行此限制,但有效的包导入是必要的。
我们只能有一个顶级公共任何类或接口 java编译单元(.java源文件)