我正在为一个学校作业制作一个简单的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;
}
}
答案 0 :(得分:2)
prompt()函数返回用户在文本框中输入的值,而不是用户单击的按钮的值。因此,除非用户在文本框中输入“false”,否则play
的值永远不会为“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()。
答案 1 :(得分: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.";
}
演示:Fiddle
答案 2 :(得分: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-statement:
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");
} */