gpt4 book ai didi

简单迭代程序中的Java方法类型错误

转载 作者:行者123 更新时间:2023-12-02 02:51:33 24 4
gpt4 key购买 nike

我一直在研究 Downey 的 Think Java,并且对一些使用迭代来打印乘法表的代码感到非常困惑。我尝试自己复制该程序并收到“此处不允许'void'类型”错误。我认为可能是我犯的一些错误导致了这个错误,但我尝试编译唐尼随书提供的代码并收到相同的编译时错误。下面是代码:

public class Table {

public static void printRow(int n, int cols) {
int i = 1;
while (i <= cols) {
System.out.printf("%4d", n*i);
i = i + 1;
}
System.out.println();
}

public static void printTable(int rows) {
int i = 1;
while (i <= rows) {
printRow(i, rows);
i = i + 1;
}
}
public static void main(String[] args) {
System.out.print(printTable(5));
}
}

如果有人能帮助我了解这里发生的事情,那就太好了。提前致谢!

最佳答案

删除对 print 的调用,只调用该方法。

public static void main(String[] args) {
printTable(5);
}

关于简单迭代程序中的Java方法类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43772968/

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