如何在java中进行系统暂停以进行调试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1401481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do a system pause in java for debugging?
提问by kokokok
Is there any functionality in Java for pausing the program like system("PAUSE");
does in C++?
Java 中是否有任何功能可以像system("PAUSE");
C++ 中那样暂停程序?
采纳答案by Jeroen van Bergen
Doing debugging is best done with a debugger, NetBeans (what you seem to be using if I look at other questions you have asked) has one. Just click in the left margin of an editor window to set a breakpoint and run the debugger. Program execution will stop at the breakpoint and you can have a look at the current state of variables, or step through the program line by line.
调试最好使用调试器来完成,NetBeans(如果我查看您提出的其他问题,您似乎正在使用它)有一个。只需单击编辑器窗口的左边距即可设置断点并运行调试器。程序执行将在断点处停止,您可以查看变量的当前状态,或逐行执行程序。
回答by RichardOD
You mean like Thread.sleep()? Please elaborate if you want further help...
你的意思是像Thread.sleep()?如果您需要进一步的帮助,请详细说明...
回答by Cat Plus Plus
I don't know Java very well, but if you want to debug, then you may want to use debugger, and breakpoints for pausing execution.
If you just want to wait for keyboard input, then check out System.in.read
, or something similar.
我不太了解Java,但是如果您想调试,那么您可能需要使用调试器和用于暂停执行的断点。如果您只想等待键盘输入,请查看System.in.read
或类似内容。
回答by aperkins
If you are attempting to debug, use a debugger. Netbeans and Eclipse both have one built in, and I am sure there are others.
如果您正在尝试调试,请使用调试器。Netbeans 和 Eclipse 都内置了一个,我相信还有其他的。
If you are trying to get a thread to wait on another thread, use a thread.join().
如果您试图让一个线程等待另一个线程,请使用 thread.join()。
回答by Wemilianius
The Short Is Yes, But You Have To Build It Using The Standard Output With System.out
and Using The Scanner
Class To Wait For New Line Input ('\n') From Standard Input From The System.in
短是是的,但你必须使用标准输出来构建它System.out
并使用Scanner
类来等待来自标准输入的新行输入('\n')来自System.in
public class PauseTest {
public static void main(String args[]){
System.out.println("Press Any Key To Continue...");
new java.util.Scanner(System.in).nextLine();
}
}
NOTE: I used a direct reference to the Scanner
Class in java.util
package, but you could simply add the import java.util.Scanner;
or import java.util.*;
Basically the same thing.
注意:我使用了对包中Scanner
类的直接引用java.util
,但您可以简单地添加导入java.util.Scanner;
或import java.util.*;
基本上相同的内容。
回答by rupali shah
There is a cin.get(), or cin.ignore() thing in C++, which basically makes you press the enter key to to exit the program, I know it is cheesy, but it works. What you could do is setup a scanner with a .nextLine(), like this
在 C++ 中有一个 cin.get() 或 cin.ignore() 的东西,它基本上让你按回车键退出程序,我知道它很俗气,但它有效。您可以做的是使用 .nextLine() 设置扫描仪,如下所示
import java.util.Scanner;
public class Example {
public static void main(String args[]){
Scanner pauser = new Scanner (System.in);
System.out.println("!!!Hello World!!!");
pauser.nextLine();
}
}
then the user must press enter to exit the program.
然后用户必须按回车键退出程序。