gpt4 book ai didi

java - 多个 System.out.println() 调用显示在一行上?

转载 作者:行者123 更新时间:2023-12-01 12:12:30 26 4
gpt4 key购买 nike

这个程序给出了一些奇怪的输出。多次执行此循环后,我看到一行输出,其中包含三个单独的 .println() 函数的内容。为什么会出现这种情况?

警告:该程序在无限循环中运行。

public class Looping
{
static int i;
public static void main(String agrs[])
{
try
{
i++;
System.out.println("In try String agrs" +i +" ");
main(i);
System.out.println(i+"After main(i).**************************!!!!!!!~~~~~~~~~~~~~```````++++_(^&$^%&%$#%$^$#%^&Y%$^#%^%&@#%#$%");
}
catch(Error e)
{

System.out.println("Catch reached"+i);
Looping q=new Looping();
q.main(++i);
}
}

public static void main(int k)
{
try
{
i++;
System.out.println("In try int k -- " +i +" __ ");
main(i);
System.out.println(i+"After main(i).**************************!!!!!!!~~~~~~~~~~~~~```````++++_(^&$^%&%$#%$^$#%^&Y%$^#%^%&@#%#$%");
}
catch(Error e)
{
System.out.println("Catch of int k -- " + i + " Why there is no new line here???______");
i++;
System.out.println("i increasing done.. next executes main(++i)");
main(++i);
}
}
}

这是输出(删除了一些中间行):

In try String agrs1 
In try int k -- 2 __
In try int k -- 3 __
In try int k -- 4 __
In try int k -- 5 __
In try int k -- 6 __
In try int k -- 7 __
In try int k -- 8 __
In try int k -- 9 __
...
...
...
...
..
...
In try int k -- 2889 __
In try int k -- 2890 __
In try int k -- 2891 __
Catch of int k -- 2891 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2894 __ Catch of int k -- 2894 Why there is no new line here???______Catch of int k -- 2894 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2897 __
In try int k -- 2898 __ Catch of int k -- 2898 Why there is no new line here???______Catch of int k -- 2898 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2901 __
In try int k -- 2902 __ Catch of int k -- 2902 Why there is no new line here???______Catch of int k -- 2902 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2905 __ Catch of int k -- 2905 Why there is no new line here???______
Catch of int k -- 2905 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2908 __
In try int k -- 2909 __
In try int k -- 2910 __ Catch of int k -- 2910 Why there is no new line here???______Catch of int k -- 2910 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2913 __
In try int k -- 2914 __ Catch of int k -- 2914 Why there is no new line here???______Catch of int k -- 2914 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2917 __ Catch of int k -- 2917 Why there is no new line here???______
Catch of int k -- 2917 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2920 __
In try int k -- 2921 __
In try int k -- 2922 __
....
.....
..

最佳答案

println() 库函数中抛出异常。

检测后,我看到了这个:

java.lang.StackOverflowError

java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
java.io.PrintStream.write(PrintStream.java:526)
java.io.PrintStream.print(PrintStream.java:669)
java.io.PrintStream.println(PrintStream.java:823)
Looping.main(Looping.java:41)

您不仅仅是无限循环,而且处于无限嵌套调用中。这会填满调用堆栈,您的程序和您进行的任何库调用都会使用该堆栈。当 println() 调用开始工作时,它会自行进行调用。在某些时候,它会耗尽堆栈空间并引发异常。这将使您脱离现有的 catch 子句,进入下一个更高层的嵌套 catch 子句。

关于java - 多个 System.out.println() 调用显示在一行上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196488/

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