【发布时间】:2020-06-12 16:56:28
【问题描述】:
对于措辞不佳的问题,我深表歉意。我真的不知道该问题如何措辞,尤其是因为我是新手。
我正在尝试制作一个小游戏,在其中从 6 种颜色列表中随机分配 4 种颜色,并且要拆除炸弹,您需要按照一些规则切断电线,例如,如果您有蓝线,则不能剪红色,或者如果你有 x 线,你需要剪 y,等等。
我遇到的第一个问题是我不知道如何得到它,所以如果用户输入“绿色”,例如,“绿色”将从 4 种颜色中消失。因此,例如,如果您有黄色、蓝色、绿色和黑色,并且用户选择剪切“绿色”,那么接下来的 3 种颜色将显示“黄色、蓝色和黑色”
谁能帮帮我?
var colors = ['Blue', 'Red', 'Yellow','White', 'Black', 'Green'];
var color = colors[Math.floor(Math.random()*colors.length)];
var color2 = colors[Math.floor(Math.random()*colors.length)];
var color3 = colors[Math.floor(Math.random()*colors.length)];
var color4 = colors[Math.floor(Math.random()*colors.length)];
console.log('The wires are: ' + color + ', ' + color2 + ', ' + color3 + ' and ' + color4);
const asdf = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
asdf.question('What will you cut?' , wire => {
console.log(`You cut ${wire}!`);
asdf.close();
if (color == 'Blue' | color2 == 'Blue' | color3 == 'Blue' | color4 == 'Blue' && wire == 'Red') {
console.log("Wrong wire!");
return;
}
else {
console.log("Nice!");
}
console.log('The remaining wires are: ' );
asdf.question("Next wire?", wire => {
console.log(`You cut ${wire}!`);
asdf.close()
})
})
【问题讨论】:
-
您正在使用按位的
|或-您想要逻辑或|| -
写条件的更优雅的方式是
[color, color2, color3, color4].includes('Blue') && wire === 'Red'而不是color == 'Blue' | color2 == 'Blue' | color3 == 'Blue' | color4 == 'Blue' && wire == 'Red'
标签: javascript