Java 使文本出现延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19882885/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:01:27  来源:igfitidea点击:

making text appear delayed

javatext

提问by user2974798

I want to make text appear in the following way:

我想让文本按以下方式显示:

H
wait 0.1 seconds
He
wait 0.1 seconds
Hel
wait 0.1 seconds
Hell
wait 0.1 seconds
Hello

But I'm not sure how to do it. Any help would be appreciated.

但我不知道该怎么做。任何帮助,将不胜感激。

EDIT: I'm hoping that I will be able to do it in a way that doesn't require me to make a System.out.print(); for each letter.

编辑:我希望我能够以不需要我制作 System.out.print(); 的方式做到这一点。对于每个字母。

EDIT 3: Here's my code. EDIT 4: No more problems, it worked perfectly, thanks.

编辑 3:这是我的代码。编辑 4:没有更多的问题,它完美地工作,谢谢。

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class GameBattle
{
public static void main(String[] args) throws Exception {
    printWithDelays("HELLO", TimeUnit.MILLISECONDS, 100);
}

public static void printWithDelays(String data, TimeUnit unit, long delay)
        throws InterruptedException {
    for (char ch:data.toCharArray()) {
        System.out.print(ch);
        unit.sleep(delay);
    }
}

采纳答案by Pshemo

You can print each letter in 0.1sinterval using Thread.sleepor more readable way (at least for me) using TimeUnit.MILLISECONDS.sleepfor example

可以打印在每个字母0.1s使用间隔Thread.sleep使用或多种可读的方式(至少对我来说)TimeUnit.MILLISECONDS.sleep例如

print first letter;
TimeUnit.MILLISECONDS.sleep(100);
print second letter
TimeUnit.MILLISECONDS.sleep(100);
...

[Update]

[更新]

I'm hoping that I will be able to do it in a way that doesn't require me to make a System.out.print(); for each letter.

我希望我能够以不需要我制作 System.out.print(); 的方式做到这一点。对于每个字母。

I don't see any reason not to do it this way.

我看不出有什么理由不这样做。

public static void main(String[] args) throws Exception {
    printWithDelays("HELLO", TimeUnit.MILLISECONDS, 100);
}

public static void printWithDelays(String data, TimeUnit unit, long delay)
        throws InterruptedException {
    for (char ch : data.toCharArray()) {
        System.out.print(ch);
        unit.sleep(delay);
    }
}

回答by mattdee123

Use the Thread.sleepfunction to pause execution.

使用该Thread.sleep函数暂停执行。

System.out.print("H");
Thread.sleep(1000);
System.out.print("E");
...etc

Of course, you'd probably want to put all of that into a loop, but Thread.sleep is what you want

当然,您可能希望将所有这些都放入一个循环中,但 Thread.sleep 正是您想要的

回答by J Code

Try this:

尝试这个:

 try {
  Thread.sleep(timeInMiliseconds); // In your case it would be: Thread.sleep(100);
} catch (Exception e) {
    e.printStackTrace();
}

回答by J Code

String text = "Hello";

for(int i=1; i<=text.length();i++){
   System.out.println(text.substring(0, i));
    try {
       Thread.sleep(100); 
    } catch (Exception e) {
       e.printStackTrace();
    }
}

Edit: if you want only 1 character by 1, than:

编辑:如果你只想要 1 个字符一个 1,那么:

for(int i=0; i<text.length();i++){
   System.out.print(""+text.characterAt(i));
    try {
       Thread.sleep(100); 
    } catch (Exception e) {
       e.printStackTrace();
    }
}