简体   繁体   English

Java:溢出和打印两次

[英]Java: Overflow and Printing Twice

When I execute this part of my code, I get a StackOverflowError: 当我执行我的代码的这部分时,我得到一个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();
    }
}

I understand that I do not need the timeTell() method, I'll add it into the chooseStory() method after I solve this problem. 我知道我不需要timeTell()方法,在解决这个问题后我会将它添加到chooseStory()方法中。 This was just easier for testing. 这对测试来说更容易。 I tried to figure out where the problem was occuring, so I replaced chooseStory(); 我试图找出问题发生的地方,所以我更换了chooseStory(); with System.out.println(rndNum); System.out.println(rndNum); and it prints the number twice. 它会打印两次数字。 The variable replay is used to see if the program has already been run once. 变量replay用于查看程序是否已经运行过一次。 If the user decides to play again, replay changes from the default, 0, to 1 and skip generating a new rndNum . 如果用户决定再次播放,则replay从默认值0更改为1并跳过生成新的rndNum The reason that I'm using the time instead of a random number generator is because each time I would run my program, the generator would give me the same sequence every time. 我使用时间而不是随机数生成器的原因是因为每次我运行程序时,生成器每次都会给我相同的序列。 Any help would be greatly appreciated. 任何帮助将不胜感激。

You are using the wrong boolean operator. 您正在使用错误的布尔运算符。

rndNum == x && rndNum == y

Will pass if and only if rndNum is equal to both x AND y - this will never happen if x and y are different values. 当且仅当rndNum等于x和y时才会传递 - 如果x和y是不同的值,这将永远不会发生。 You need to use the OR operator, ' || 您需要使用OR运算符' || ': “:

rndNum == X || rndNum == y

Also, as @Sotirios Delimanolis pointed out, you have variable masking going on as well. 此外,正如@Sotirios Delimanolis指出的那样,你也有可变的掩蔽。 He has also explained why the StackOverflowError is happening 他还解释了StackOverflowError发生的原因

If you only have one variable rndNum it can only be assigned one value at a time. 如果您只有一个变量rndNum则一次只能分配一个值。 So rndNum == 1&& rndNum == 6 and all the following if s will be false. 所以rndNum == 1&& rndNum == 6以及if s将是假的所有以下内容。 This is why timeTell() will always be called. 这就是为什么永远会调用timeTell()原因。

First rndNum is local to to timetell. 第一个rndNum是本地的到时间。 So probably instance level rndNum is not initialized. 因此,实例级别rndNum可能未初始化。

But even if it were initialized properly your conditional check has to be OR not AND. 但即使它被正确初始化,您的条件检查也必须是OR而不是AND。 A variable can have only one value at a 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();
}

So currently you have all condition getting wrong and control reaches else block everytime , and thus endless recursive call to timetell and hence the stackOverFlow. 所以目前你所有的条件都出错了,每次控制到达else块,因此无休止地调用timetell,从而调用stackOverFlow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM