0

我正在创建一个乒乓球游戏,并试图在分数的最后一位数字以 5 结尾时结束游戏,但我不知道如何实现这一点。到目前为止,这是我的代码:

if score >= 50 {show_message('ObiWan Wins'); game_end();}
if score <= 50 && score(ENDS IN DIGIT 5 NOT SURE WHAT CODE TO PLACE HERE) {show_message('Vader Wins'); game_end();}
4

2 回答 2

1

对于“重复”,您有模运算。您真正想要的是要在以下位置执行的代码:

score = 5
score = 15
score = 25
....

因此,在周期大小为“10”的情况下,当您使用“10”作为第二个参数进行模运算时,您确实会得到这样的周期。

0 % 10 = 0
1 % 10 = 1
2 % 10 = 2
3 % 10 = 3
4 % 10 = 4
5 % 10 = 5
6 % 10 = 6
7 % 10 = 7
8 % 10 = 8
9 % 10 = 9
10 % 10 = 0
11 % 10 = 1
12 % 10 = 2
...

由此应该可以看出。

if score >= 50 {
    show_message('ObiWan Wins'); 
    game_end();
} else if ( score % 10 = 5) {
    show_message('Vader Wins'); 
    game_end();
}

作为一个侧节点,我将其更改if score <= 50else if- 虽然在这种情况下没有区别,但当您在两个选项之间进行选择时,您不希望它们同时执行

于 2014-02-21T11:07:51.807 回答
1

只是另一种方法:

if score >= 50 {
    show_message('ObiWan Wins'); 
    game_end();
} else if ( score mod 5 = 0) && ( score mod 10 != 0 ) {
    show_message('Vader Wins'); 
    game_end();
}
于 2014-04-18T09:34:51.340 回答