[英]How to print each element of a string array in a different time gap?
我已经进行了足够的研究,并找到了一些类似的质量检查。 但是我找不到确切的方法。
我有一个字符串数组:
public static String[] lyric = {"It's been a long day without you, my friend",
"And I'll tell you all about it when I see you again",
"We've come a long way from where we began",
"Oh, I'll tell you all about it when I see you again",
"When I see you again",
"Damn, who knew?",
"All the planes we flew",
"Good things we've been through",
"That I'll be standing right here talking to you"};
我想打印数组中句子(元素)的每个字母。 我也想在单词之间留出时间间隔。 然后甚至是句子。
例如:每个字母应在间隔的1/2秒内打印。 然后,我想用单词和句子给出时间段。
在这里,我只打印字母时出错
public class OperationPlay {
public static String[] para;
public static char[] singleW;
public static int i,j,k;
public static void main(String[] args) {
Timer timer = new Timer();
for (i = 0; i < Lyrics.lyric.length; i++) {
para = Lyrics.lyric[i].split(" ");
for (j = 0; j < para.length; j++) {
singleW = para[j].toCharArray();
timer.schedule(new TimerTask() {
@Override
public void run() {
for (k = 0; k < singleW.length; k++) {
System.out.print(singleW[k]);
}
}
}, 6000);
}
}
}
有人帮我执行此操作。
您可以将整个歌词视为一个长字符串,然后逐个字符处理,如果经过了足够的时间,则使用循环计时器编写下一个字符。
在此示例中,我每5毫秒运行一次计时器,并假设在5毫秒后打印每个字母,每10毫秒打印一个空格,在20毫秒之后打印每行。
public class OperationPlay
{
private static final char lineSeparator = '/';
static int ticksSinceLastWrite = 0;
static int indexOfNextCharacter = 0;
private static final int ticksPerLetter = 1;
private static final int ticksPerSpace = 2;
private static final int ticksPerLine = 4;
private static final int millisecondsPerTick = 5;
static String allLyrics = "";
public static boolean Tick()
{
ticksSinceLastWrite++;
if (indexOfNextCharacter >= allLyrics.length())
{
return false;
}
int ticksForNextCharacter = 0;
switch(allLyrics.charAt(indexOfNextCharacter))
{
case ' ':
ticksForNextCharacter = ticksPerSpace;
break;
case lineSeparator:
ticksForNextCharacter = ticksPerLine;
break;
default:
ticksForNextCharacter = ticksPerLetter;
}
if (ticksForNextCharacter <= ticksSinceLastWrite)
{
WriteNextCharacter();
}
return true;
}
private static void WriteNextCharacter()
{
char nextChar = allLyrics.charAt(indexOfNextCharacter);
if (nextChar == lineSeparator)
{
System.out.println();
}
else
{
System.out.print(nextChar);
}
indexOfNextCharacter++;
ticksSinceLastWrite = 0;
}
public static String[] lyric = {"It's been a long day without you, my friend",
"And I'll tell you all about it when I see you again",
"We've come a long way from where we began",
"Oh, I'll tell you all about it when I see you again",
"When I see you again",
"Damn, who knew?",
"All the planes we flew",
"Good things we've been through",
"That I'll be standing right here talking to you"};
public static void main(String[] args)
{
allLyrics = String.join(Character.toString(lineSeparator), lyric);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
boolean returnValue = Tick();
if (returnValue == false)
{
timer.cancel();
}
}
}, 0, millisecondsPerTick );
}
}
您可以使用Thread.sleep(delay)来在给定时间内停止线程
public static void main(String[] args) throws InterruptedException {
// change delay time as you require
final long sentance_delay=1000;
final long word_delay=100;
final long char_delay=50;
for (i = 0; i < lyric.length; i++) {
para = lyric[i].split(" ");
for (j = 0; j < para.length; j++) {
System.out.print(" "); // add space btween words
Thread.sleep(word_delay);
char [] word_chars=para[j].toString().toCharArray();
for(k=0;k<word_chars.length;k++){
System.out.print(word_chars[k]); //print chars one by one
Thread.sleep(word_delay);
}
}
System.out.println("");
Thread.sleep(sentance_delay);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.