人员猜测程序(Javascript)
所以我尝试用javascript制作一个猜测游戏类型的东西。我想要它,这样我就可以在不改变其他代码的情况下继续添加新的人员和问题,而且它会很好地工作。到目前为止,这就是我得到的,它似乎应该工作,但它的行为非常不可预测和怪异。有人能看看我的代码,看看我哪里出错了吗 在这里:人员猜测程序(Javascript),javascript,Javascript,所以我尝试用javascript制作一个猜测游戏类型的东西。我想要它,这样我就可以在不改变其他代码的情况下继续添加新的人员和问题,而且它会很好地工作。到目前为止,这就是我得到的,它似乎应该工作,但它的行为非常不可预测和怪异。有人能看看我的代码,看看我哪里出错了吗 在这里: // ORDER: Name, Age, Gender, Glasses var peopleremaining = [ ["Evan",17,"male",false], ["Liam",10,"male",false]
// ORDER: Name, Age, Gender, Glasses
var peopleremaining = [
["Evan",17,"male",false],
["Liam",10,"male",false],
["Logan",15,"female",false],
["Brynn",7,"female",false],
["Caz",37,"male",true]
];
var questions = [
["Is the person you're thinking of older than 16?",1,16],
["Is the person you're thinking of male?",2,"male"],
["Is the person you're thinking of older than 10?",1,10],
["Does the person you're thinking of wear eyeglasses?",3,true]
];
var randomquestion;
var randomquestionnumber;
function newquestion() {
randomquestionnumber = [Math.floor((Math.random()*questions.length))];
randomquestion = questions[randomquestionnumber];
document.getElementById("mainheading").innerHTML = randomquestion[0];
}
function buttonpressed(option) {
var questionsubject = randomquestion[1];
var questionvalue = randomquestion[2];
var peopleremaininglength = peopleremaining.length;
if (option == "yes") {
if (questionsubject == 1) {
for (var t=0;t<peopleremaininglength;t++) {
if (peopleremaining[t][questionsubject] < questionvalue) {
peopleremaining.splice(t, 1)
}
}
}
else {
for (var t=0;t<peopleremaininglength;t++) {
if (peopleremaining[t][questionsubject] != questionvalue) {
peopleremaining.splice(t, 1)
}
}
}
}
else {
if (questionsubject == 1) {
for (var t=0;t<peopleremaininglength;t++) {
if (peopleremaining[t][questionsubject] >= questionvalue) {
peopleremaining.splice(t, 1)
}
}
}
else {
for (var t=0;t<peopleremaininglength;t++) {
if (peopleremaining[t][questionsubject] == questionvalue) {
peopleremaining.splice(t, 1)
}
}
}
}
questions.splice(randomquestionnumber, 1);
if (peopleremaining.length == 1) {
alert("You are thinking of " + peopleremaining[0][0]);
}
else {
newquestion();
}
}
//顺序:姓名、年龄、性别、眼镜
var PeopleLeving=[
[“Evan”,17,“男性”,假],
[“利亚姆”,10,“男性”,假],
[“洛根”,15,“女性”,假],
[“Brynn”,7,“女性”,假],
[“Caz”,37,“男性”,对]
];
变量问题=[
[“你想的那个人是不是比16岁大?”,1,16],
[“你想的那个人是男性吗?”,2,“男性”],
[“你想的那个人比10岁大吗?”,1,10],
[“你想的那个人戴眼镜吗?”,3,是真的]
];
var问题;
变量随机数;
函数newquestion(){
randomquestionnumber=[Math.floor((Math.random()*questions.length));
随机问题=问题[随机问题编号];
document.getElementById(“mainheading”).innerHTML=randomquestion[0];
}
按下功能按钮(选项){
var questionsubject=随机问题[1];
var questionvalue=随机问题[2];
var peopleremaininglength=peopleremaining.length;
如果(选项=“是”){
如果(问题主题==1){
对于(var t=0;t您犯了一个典型的错误-您在迭代中修改了迭代对象。问题是您正在迭代“peopleremaining”数组的每个元素,但在该循环中您正在修改数组
解决这个问题的一个快速方法是像这样打破循环:
for (var t = 0; t < peopleremaininglength; t++) {
if (peopleremaining[t][questionsubject] < questionvalue) {
peopleremaining.splice(t, 1)
t = peopleremaininglength; // Add this line to break out of the loop
}
}
for(var t=0;t
请清楚地解释您的程序到底出了什么问题。谢谢您的回答。这对我来说是有意义的,但我仍然有点困惑。我尝试了这个,但似乎没有完全解决它。。。