gpt4 book ai didi

java - 递归程序中的错误

转载 作者:行者123 更新时间:2023-12-01 13:07:15 24 4
gpt4 key购买 nike

我有以下程序应该正在运行,但它没有运行。教授把它交给我们来使用。它旨在计算阶乘。它甚至不让我编译。我收到的错误状态

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

This is in reference to the following line:

 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

最佳答案

你有这个:

“%d! = %d\n”

“和”花哨的引号字符在 Java 中不是有效的引号字符。使用普通的旧 "代替。

如果您的编辑器自动为您创建精美的引号,请禁用该功能或​​找到更合适的编辑器。

关于java - 递归程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com