Java简单迭代程序中的方法类型错误
Java method type error in simple iteration program
我一直在研究 Downey 的 Think Java,并且一直被一些使用迭代打印乘法的代码所困扰 table。我尝试自己复制该程序并收到“'void' 此处不允许类型”错误。我认为可能是我犯了一些错误导致了这个错误,但我尝试编译 Downey 随书提供的代码并收到相同的编译时错误。下面是代码:
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));
}
}
如果有人能帮助我理解这里发生的事情,那就太好了。提前致谢!
删除对打印的调用,只调用该方法。
public static void main(String[] args) {
printTable(5);
}
printTable
方法没有 return 任何东西。如果需要,您可以在 printTable
方法本身中添加打印语句,而不是在 main()
中调用 System.out.println
,然后只需从 main()
中调用 printTable
方法。我不确定您要再次打印什么,因为 printRow
已经在打印输出。
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) {
printTable(5);
}
}
我一直在研究 Downey 的 Think Java,并且一直被一些使用迭代打印乘法的代码所困扰 table。我尝试自己复制该程序并收到“'void' 此处不允许类型”错误。我认为可能是我犯了一些错误导致了这个错误,但我尝试编译 Downey 随书提供的代码并收到相同的编译时错误。下面是代码:
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));
}
}
如果有人能帮助我理解这里发生的事情,那就太好了。提前致谢!
删除对打印的调用,只调用该方法。
public static void main(String[] args) {
printTable(5);
}
printTable
方法没有 return 任何东西。如果需要,您可以在 printTable
方法本身中添加打印语句,而不是在 main()
中调用 System.out.println
,然后只需从 main()
中调用 printTable
方法。我不确定您要再次打印什么,因为 printRow
已经在打印输出。
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) {
printTable(5);
}
}