当我执行这部分代码时,我得到一个 StackOverflowError:
/**
* Determine the story based on the time.
*/
private void timeTell()
{
if(replay == 0){
long rndNum = System.currentTimeMillis() % 10;
chooseStory();
}
}
/**
* Randomly choose which story to tell based on the current system time.
*/
private void chooseStory()
{
if(rndNum == 1&& rndNum == 6){
storyOne();
}
else if(rndNum == 2&& rndNum == 7){
storyTwo();
}
else if(rndNum == 3&& rndNum == 8){
storyThree();
}
else if(rndNum == 4&& rndNum == 9){
storyFour();
}
else if(rndNum == 5&& rndNum == 0){
storyFive();
}
else{
timeTell();
}
}
我明白我不需要这个方法,我解决这个问题后将timeTell()
它添加到方法中。chooseStory()
这更容易进行测试。我试图找出问题出在哪里,所以我替换chooseStory();
为System.out.println(rndNum);
它并打印了两次。该变量replay
用于查看程序是否已经运行过一次。如果用户决定再次播放,replay
则从默认值 0 更改为 1 并跳过生成新的rndNum
. 我使用时间而不是随机数生成器的原因是因为每次我运行我的程序时,生成器每次都会给我相同的序列。任何帮助将不胜感激。