Java 程序为println(字符串s)提供正确的输出;但不打印(字符串s);为什么?
以下是我遇到问题的代码:Java 程序为println(字符串s)提供正确的输出;但不打印(字符串s);为什么?,java,eclipse,string,printing,Java,Eclipse,String,Printing,以下是我遇到问题的代码: public class testOutput { public static void main(String[] args) throws Exception { int count = 50000; String s = "Vinjith"; for(int i=0;i<count;i++) { System.out.print(s); // change this to
public class testOutput {
public static void main(String[] args) throws Exception {
int count = 50000;
String s = "Vinjith";
for(int i=0;i<count;i++) {
System.out.print(s); // change this to println(s); and it works!
System.out.flush();
}
}
}
公共类testOutput{
公共静态void main(字符串[]args)引发异常{
整数计数=50000;
字符串s=“Vinjith”;
对于(int i=0;i您是否尝试过:
for(int i=0;i<count;i++) {
System.out.print(s); // change this to println(s); and it works!
}
System.out.println("---done");
System.out.flush();
for(int i=0;i这是因为同一行中的字符太多,而Eclipse在其控制台上不支持这一点(您将无法在控制台上看到任何打印内容)。请在命令行上尝试相同的代码,它应该可以工作。这是因为您在Eclipse控制台上打印的字符长度超出了限制
试试这个,看看它是否打印出来
System.out.print(s); // change this to println(s); and it works!
System.out.println();
System.out.flush();
另外,关于限制问题,只需尝试一下。在首选项->运行/调试->控制台中,将有一个名为固定宽度控制台的复选框。它的最大限制是1000
。尝试将其设置为1000
,并按如下方式运行原始代码。您将看到它打印了一些字符,并用于恢复t、 抛出一个内部错误
System.out.print(s); // change this to println(s); and it works!
System.out.flush();
System.out.println()
的行为类似于System.out.print
,唯一的区别是打印后会添加换行符。一个或另一个的使用取决于您的需要(这将定义输出是否有效)。您必须准确地指定哪些不起作用。如果您对System.out
使用输出流会发生什么情况?好的。经过一些研究-这是一个现有的错误-…如果您需要打印它-将固定宽度控制台->最大字符宽度设置为1000或Eclipse版本支持的任何内容。Eclipse说它可以打印最多1000000个字符(在Windows->preference->run->console中可以找到)。它很难在一条直线上打印5000个字符。他的限制达到585,即计数>585。eclipse不打印任何东西print;和println;在计数到584时工作正常。之后,print(s)不工作。但是println(s)工作。输出用于打印:vinjithvinjith..for println(s);是字符串之间的新行。发布了另一个版本供您尝试。@vj07-如果您仔细注意,更改固定宽度控制台后的每行正好有1000个字符。