我正在尝试使用javascript创建一个非常简单的基于文本的冒险游戏。我很新,我觉得使用一些函数,变量和if-else语句会很棒。我写了一些代码来提示屏幕询问年龄,并给出结果。
confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
document.write(" ");
} else {
document.write("You're less than 18? You better just stay home.");
}
document.write("<button> Sounds like you're ready.</button>");
var gender = prompt("Male","Female");
if(gender = "Male") {
document.write("Sounds good!");
} else if(gender = "Female") {
document.write("Sounds good!");
} else if {
document.write("You're a damn liar");
};
};
如果我只运行代码的上半部分,一切都会正常运行。但是,如果我尝试运行性别提示,与原始代码一起运行,它将无法运行。有什么建议?
答案 0 :(得分:0)
此代码应该有效:
confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
document.write(" ");
} else {
document.write("You're less than 18? You better just stay home.");
}
document.write("<button> Sounds like you're ready.</button>");
var gender = prompt("Are you Male or Female?");
if(gender == "Male") {
document.write("Sounds good!");
} else if(gender == "Female") {
document.write("Sounds good!");
} else {
document.write("You lie...");
}
基本上,您需要将=
替换为==
或===
。而prompt()
只需要一个参数。