我做了足够的研究,发现了一些类似的Q-A。但我无法找到确切的方法。
我有一个字符串数组:
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);
}
}
}
有人帮助我执行此操作。
答案 0 :(得分:0)
您可以将整个歌词集视为一个长字符串然后逐个字符,如果已经过了足够的时间,则使用定期计时器写下一个字符。
在这个例子中,我每5毫秒运行一次计时器,并假设每个字母在5ms后打印,每隔10ms打印一次,每隔20ms打印一次。
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 );
}
}
答案 1 :(得分:0)
您可以使用Thread.sleep(延迟)来停止线程达到给定时间
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);
}
}