Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java编程中添加时间延迟?_Java_Recursion_Time - Fatal编程技术网

如何在java编程中添加时间延迟?

如何在java编程中添加时间延迟?,java,recursion,time,Java,Recursion,Time,我有这个计划: import javax.swing.*; public static void main() { try { String stringyInp = JOptionPane.showInputDialog( null , "Enter your number" ) ; int input = Integer.parseInt(stringyInp) ; }catch(Exception e) { JOptionP

我有这个计划:

import javax.swing.*;

public static void main()
{
    try {
        String stringyInp = JOptionPane.showInputDialog( null , "Enter your number" ) ; 
        int input = Integer.parseInt(stringyInp) ;
    }catch(Exception e) {
       JOptionPane.showMessageDialog(null ,"Enter only numerical values") ; //i want here to add a time delay of 3 seconds!How?
       main(); 
    }
    System.out.print( FACTORIAL(input)) ;


}// instance variables - replace the example below with your own



 public static int FACTORIAL(int number )
{
     if (number == 0){
         return 1 ;
        }
        else {

    return FACTORIAL(number-1) * number ;

}

那么,我们如何添加时间延迟,如何执行延迟为3秒的指令??我想在此程序中添加3秒或2秒的延迟…

我想您可以使用此代码

   try {
     Thread.sleep(1000);
   } catch(InterruptedException ex) {
     Thread.currentThread().interrupt();
   }

Thread.sleep()…您可以使用计时器来代替调用mainNo,不要使用
Thread.sleep(…)
,除非您在后台线程中这样做。谷歌搜索并使用摆动计时器。此外,您的代码将无法编译,因为它缺少关键组件。请尽可能发布真实代码。编译器是无情的,所以你也应该如此。