我正在进行数学测验,但我遇到了一个大问题。 整个脚本运行良好,直到你选择分裂。总和本身工作正常,但我一直试图让它只用整数作为答案提出问题。
注意: - 未声明的所有变量已声明 - 这是一个错误的while循环(我认为)
//level 4
else if(level==4){
alert(name + ", welcome to level 4 - Division!\nI will give you a sum and you must give me the answer. It's simple!");
alert("For every one you get wrong, you lose one of your 3 lives. For every one you get right, you gain a point!");
while(lives>0){
var correctFloorAns = 1;
var correctAns = 2;
while(correctAns==correctFloorAns){
alert("hi")
}
{
primaryNumber = random(200);
primaryNumber = primaryNumber * 1;
secondaryNumber = random(200);
secondaryNumber = secondaryNumber * 1;
correctAns = primaryNumber / secondaryNumber;
correctAns = correctAns * 1;
correctFloorAns = Math.floor(correctAns);
}
playerAns = prompt(name + ", here is your sum: " + primaryNumber + " divided by " + secondaryNumber + "\n" + correctAns + "\n" + correctFloorAns);
if(playerAns==correctAns){
alert("Well done " + name + "! You got it right!");
points = points + 1;
}
else{
alert("Nope! You got it wrong");
lives = lives - 1;
}
alert("You are now on " + lives + " live(s) and " + points + " point(s)!");
}
{
alert("You died on " + points + " points! You are clearly not very good at maths, " + name + "!\nI got " + (points + 100) + " points!!!");
}
谢谢, Reece C。
答案 0 :(得分:0)
你写了这个:
while(correctAns==correctFloorAns){
alert("hi")
}
{
primaryNumber = random(200);
primaryNumber = primaryNumber * 1;
secondaryNumber = random(200);
secondaryNumber = secondaryNumber * 1;
correctAns = primaryNumber / secondaryNumber;
correctAns = correctAns * 1;
correctFloorAns = Math.floor(correctAns);
}
第二个块{}中的代码不会在while循环中执行,它只执行一次。
我认为你想写的是:
while(correctAns==correctFloorAns){
// I advise you to comment your alert once your debug is done, it will be very annoying
alert("hi");
primaryNumber = random(200);
primaryNumber = primaryNumber * 1;
secondaryNumber = random(200);
secondaryNumber = secondaryNumber * 1;
correctAns = primaryNumber / secondaryNumber;
correctAns = correctAns * 1;
correctFloorAns = Math.floor(correctAns);
}
干杯
答案 1 :(得分:0)
感谢大家的帮助!可悲的是,你给我的剧本没有用,但我找到了解决办法!这是:
var wrong = true
//creates a random sum and loops until answer is a whole number bigger that 0
while (wrong){
primaryNumber = random(200);
primaryNumber = (primaryNumber * 1) + 1;
secondaryNumber = random(200);
secondaryNumber = (secondaryNumber * 1) + 1;
correctAns = primaryNumber / secondaryNumber;
correctAns = correctAns * 1;
correctFloorAns = Math.floor(correctAns);
if(correctAns==correctFloorAns){
wrong = false
}
}