我需要创建一个二维表,其中有两个列,代码如下。
public static void printCommonLogTable() {
double x = 0.0;
int i = 1;
while (x <= 10.0) {
System.out.print(x + " " + Math.log(x) + " ");
x = x + 0.5; }
System.out.println("");
}
public static void main(String[] args) {
printCommonLogTable();
}
表中的第一列应该是计算日志的编号,第二列应该是结果。
但是当我运行这个的时候,所有的事情都在同一条线上。
发布于 2015-10-09 13:59:02
这是因为您对System.out.println("");
的调用是错误的:它应该在while
循环的末尾(但在它内部)。这在代码正确缩进时更容易查看,如下所示:
public static void printCommonLogTable() {
double x = 0.0;
while (x <= 10.0) {
System.out.print(x + " " + Math.log(x) + " ");
x = x + 0.5;
System.out.println();
}
}
请注意,我删除了未使用的i
变量,并将System.out.println("");
替换为System.out.println();
。
还可以将两个print语句合并为一个:
public static void printCommonLogTable() {
double x = 0.0;
while (x <= 10.0) {
System.out.println(x + " " + Math.log(x) + " ");
x = x + 0.5;
}
}
发布于 2015-10-09 13:59:52
public static void main(String args[]) {
double x = 0.0;
int i = 1;
while (x <= 10.0) {
System.out.println(x + " " + Math.log(x) + " "); // old : System.out.print(x + " " + Math.log(x) + " ");
x = x + 0.5; }
}
(“.”)方法打印字符串“.”并将光标移动到新行。指纹(“.”)方法只打印字符串“.”,但不将光标移动到新行。因此,随后的打印指令将打印在同一行上。println()方法也可以在没有参数的情况下使用,以将光标定位到下一行。
https://stackoverflow.com/questions/33040158
复制相似问题