当前位置: 代码迷 >> java >> 如何延迟我的程序,以便在文本之间有一个停顿?
  详细解决方案

如何延迟我的程序,以便在文本之间有一个停顿?

热度:82   发布时间:2023-07-25 19:05:41.0

我的代码示例:

System.out.println("Why hello there!");
System.out.println("Welcome to 'Ancient Battles and Adventures!");
***DELAY WOULD GO HERE***
System.out.println("Now, what is your name?");
public class MyClass {
    public static void main(String[] args) {
        System.out.println("Why hello there!");
        System.out.println("Welcome to 'Ancient Battles and Adventures!");
        try {
            Thread.sleep(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Now, what is your name?");
    }
}

您可以尝试此代码以取得成功

class ex 
{ 
    public static void main(String args[])
    {
        System.out.println("Why hello there!");
        System.out.println("Welcome to 'Ancient Battles and Adventures!");
        try
        {
            Thread.sleep(1000);// here you can delay for one second
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        System.out.println("Now, what is your name?");
    }
}
  相关解决方案