0

我正在为学校作业制作一个简单的 javascript 数字游戏,使用 math.random 生成一个随机数(var = randomDigit),然后要求用户考虑一个大于 1 的数字。基本上,他们的数字加倍,而 JS 将我的随机数加倍编号并输出。他们将数字相加,然后除以 2 并减去原始数字,留下 (var = randomDigit)

这是我被卡住的部分。我必须这样做,所以如果用户不想玩,它就不会继续下去……而且我只能使用一个“if”语句。我选择使用prompt() 方法......如果你输入YES,它就可以工作,就像对话框说的那样,如果你按下取消它就不起作用(好消息)。但是如果我输入“NO”就像我不想玩游戏一样,它仍然有效。

我想要它,因此该程序仅适用于在提示对话框中键入“YES”的情况。

这是我的 JS 小提琴:http: //jsfiddle.net/JrYAq/

 function dialog(){
var x;

var randomDigit = Math.floor((Math.random()*20)+1);

var doubleDigit = +randomDigit * 2;

var play = prompt("Would you like to play a game? If not, press cancel below.\n\nThink of a number greater than 1. Confirm you thought of a number by typing the word: YES","Type Here");

if (play==='false')
    {
    document.getElementById("pick").innerHTML="Sorry, maybe next time.";
    }       

else (play= "YES");
{
    x="1. Now double the number you thought of. I'm going to think of a number too:" + ' ' + doubleDigit +  '<br><br>' + "2. Now add my number and your doubled number together." + '<br><br>' + "Now divide the added numbers by 2, then subtract your original number. Did you get" + ' ' + randomDigit + "?" + '<br><br>' + "Pretty cool, right?"  ;
    document.getElementById("pick").innerHTML=x;
}
}
4

3 回答 3

2

prompt() 函数返回用户在文本框中输入的值,而不是用户单击的按钮的值。因此,play除非用户在文本框中输入“false”,否则 的值永远不会是“false”。

试试这个:

var play = prompt("Would you like to play a game? If not, press cancel below.\n\nThink of a number greater than 1. Confirm you thought of a number by typing the word: YES","Type Here");

if (play === "YES")
{
    x="1. Now double the number you thought of. I'm going to think of a number too:" + ' ' + doubleDigit +  '<br><br>' + "2. Now add my number and your doubled number together." + '<br><br>' + "Now divide the added numbers by 2, then subtract your original number. Did you get" + ' ' + randomDigit + "?" + '<br><br>' + "Pretty cool, right?"  ;
    document.getElementById("pick").innerHTML=x;

}       
else 
{
    document.getElementById("pick").innerHTML="Sorry, maybe next time.";
}

此外,由于这更像是一种是或否类型的情况,您可能希望使用 confirm() 而不是 prompt()。

于 2013-06-21T03:50:35.530 回答
1

小的调整就可以了

if (play== "YES")
{
    x="1. Now double the number you thought of. I'm going to think of a number too:" + ' ' + doubleDigit +  '<br><br>' + "2. Now add my number and your doubled number together." + '<br><br>' + "Now divide the added numbers by 2, then subtract your original number. Did you get" + ' ' + randomDigit + "?" + '<br><br>' + "Pretty cool, right?"  ;
    document.getElementById("pick").innerHTML=x;
} else {
    document.getElementById("pick").innerHTML="Sorry, maybe next time.";
}

演示:小提琴

于 2013-06-21T03:42:46.533 回答
1
else (play= "YES");

是一个赋值(不是比较),在括号中,后跟行分隔符。它后面的块将始终被执行。您的脚本当前可以正常工作,就好像它是编写的一样

if (play==='false') {
    document.getElementById("pick").innerHTML="Sorry, maybe next time.";
} else {
    play= "YES";
x="1. Now double the number you thought of. I'm going to think of a number too:" + ' ' + doubleDigit +  '<br><br>' + "2. Now add my number and your doubled number together." + '<br><br>' + "Now divide the added numbers by 2, then subtract your original number. Did you get" + ' ' + randomDigit + "?" + '<br><br>' + "Pretty cool, right?"  ;
document.getElementById("pick").innerHTML=x;

你似乎想要一个 else-if 语句:

if (play == "false") {
    document.getElementById("pick").innerHTML = "Sorry, maybe next time.";
} else if (play == "YES") {
    x="1. Now double the number you thought of. I'm going to think of a number too:" + ' ' + doubleDigit +  '<br><br>' + "2. Now add my number and your doubled number together." + '<br><br>' + "Now divide the added numbers by 2, then subtract your original number. Did you get" + ' ' + randomDigit + "?" + '<br><br>' + "Pretty cool, right?"  ;
    document.getElementById("pick").innerHTML = x;
} /* else {
    alert("neither 'false' nor 'YES' were entered");
} */
于 2013-06-21T03:54:20.837 回答